Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: OpenTracing propagator #318

Merged
merged 21 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions propagators/opentelemetry-propagator-opentracing/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*!
* Copyright 2020, OpenTelemetry Authors
mwear marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const karmaWebpackConfig = require('../../karma.webpack');
const karmaBaseConfig = require('../../karma.base');

module.exports = config => {
config.set(
Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig,
})
);
};
14 changes: 12 additions & 2 deletions propagators/opentelemetry-propagator-opentracing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"compile": "tsc --build",
"clean": "tsc --build --clean",
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
mwear marked this conversation as resolved.
Show resolved Hide resolved
"test:browser": "nyc karma start --single-run",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
Expand All @@ -21,7 +22,8 @@
"tracing",
"profiling",
"monitoring",
"b3"
"opentracing",
"propagator"
],
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
Expand All @@ -44,14 +46,22 @@
"devDependencies": {
"@types/mocha": "8.2.0",
"@types/node": "14.14.12",
"@types/webpack-env": "1.16.0",
"codecov": "3.8.1",
"gts": "2.0.2",
"istanbul-instrumenter-loader": "3.0.1",
"karma": "5.2.3",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "4.0.2",
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-loader": "8.0.12",
"ts-mocha": "8.0.0",
"typescript": "3.9.7"
"typescript": "3.9.7",
"webpack": "4.46.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ function readHeader(
return header || '';
}

const VALID_HEADER_NAME_CHARS = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;

function isValidHeaderName(name: string): boolean {
return VALID_HEADER_NAME_CHARS.test(name);
}

const INVALID_HEADER_VALUE_CHARS = /[^\t\x20-\x7e\x80-\xff]/;

function isValidHeaderValue(value: string): boolean {
return !INVALID_HEADER_VALUE_CHARS.test(value);
}

/**
* Propagator for the OpenTracing HTTP format.
*/
Expand All @@ -69,6 +81,7 @@ export class OpenTracingPropagator implements TextMapPropagator {
if (!baggage) return;

Object.entries(baggage).forEach(([k, v]) => {
if (!isValidHeaderName(k) || !isValidHeaderValue(v.value)) return;
setter.set(carrier, `${OT_BAGGAGE_PREFIX}${k}`, v.value);
mwear marked this conversation as resolved.
Show resolved Hide resolved
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,48 @@ describe('OpenTracingPropagator', () => {
assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}foo`], 'bar');
assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}bar`], 'baz');
});

it('omits baggage items with invalid keys', () => {
const spanContext: SpanContext = {
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
spanId: 'e457b5a2e4d86bd1',
traceFlags: TraceFlags.SAMPLED,
};

let context = setSpanContext(ROOT_CONTEXT, spanContext);

const baggage: Baggage = {
fθθ: { value: 'bar' },
bar: { value: 'baz' },
};

context = setBaggage(context, baggage);

propagator.inject(context, carrier, defaultTextMapSetter);
assert.ok(!(`${OT_BAGGAGE_PREFIX}fθθ` in carrier));
assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}bar`], 'baz');
});

it('omits baggage items with invalid values', () => {
const spanContext: SpanContext = {
traceId: '80f198ee56343ba864fe8b2a57d3eff7',
spanId: 'e457b5a2e4d86bd1',
traceFlags: TraceFlags.SAMPLED,
};

let context = setSpanContext(ROOT_CONTEXT, spanContext);

const baggage: Baggage = {
foo: { value: 'bαr' },
bar: { value: 'baz' },
};

context = setBaggage(context, baggage);

propagator.inject(context, carrier, defaultTextMapSetter);
assert.ok(!(`${OT_BAGGAGE_PREFIX}foo` in carrier));
assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}bar`], 'baz');
});
});

describe('.extract', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const testsContext = require.context('.', true, /test$/);
testsContext.keys().forEach(testsContext);

const srcContext = require.context('.', true, /src$/);
srcContext.keys().forEach(srcContext);