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

chore: fixing and unifying the shutdown across all pipeline #3

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ The `opentelemetry-js` project is written in TypeScript.
- Maintainers may close, block, or put on hold pull requests even if they have strictly met these requirements.
- No “changes requested” reviews.
- No unresolved conversations.
- 4 approvals, including the approvals of both maintainers
- A pull request opened by an approver may be merged with only 3 reviews.
- 3 approvals, including the approvals of at least 2 maintainers
- A pull request opened by an approver may be merged with only 2 reviews.
- Small (simple typo, URL, update docs, or grammatical fix) or high-priority changes may be merged more quickly or with fewer reviewers at the discretion of the maintainers. This is typically indicated with the express label.
- For plugins, exporters, and propagators approval of the original code module author is preferred but not required.
- New or changed functionality is tested by unit tests.
- New or changed functionality is documented.
- Substantial changes should not be merged within 24 hours of opening in order to allow reviewers from all time zones to have a chance to review.

### Generating API documentation

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[otel-contrib-plugin-ioredis]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-ioredis
[otel-contrib-plugin-mongodb]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-mongodb
[otel-contrib-plugin-mysql]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-mysql
[otel-contrib-plugin-pg-pool]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-postgres/opentelemetry-plugin-pg-pool
[otel-contrib-plugin-pg]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-postgres/opentelemetry-plugin-pg
[otel-contrib-plugin-pg-pool]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-pg-pool
[otel-contrib-plugin-pg]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-pg
[otel-contrib-plugin-redis]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-redis
[otel-contrib-plugin-express]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/node/opentelemetry-plugin-express
[otel-contrib-plugin-user-interaction]: https:/open-telemetry/opentelemetry-js-contrib/tree/master/plugins/web/opentelemetry-plugin-user-interaction
Expand Down
4 changes: 2 additions & 2 deletions doc/batcher-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {

export class CustomBatcher extends UngroupedBatcher {
aggregatorFor (metricDescriptor: MetricDescriptor) {
if (metricDescriptor.labels === 'metricToBeAveraged') {
if (metricDescriptor.name === 'requests') {
return new AverageAggregator(10);
}
// this is exactly what the "UngroupedBatcher" does, we will re-use it
Expand Down Expand Up @@ -138,7 +138,7 @@ const meter = new MeterProvider({
interval: 1000,
}).getMeter('example-custom-batcher');

const requestsLatency = meter.createMeasure('requests', {
const requestsLatency = meter.createValueRecorder('requests', {
monotonic: true,
description: 'Average latency'
});
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/propagation-validation-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"@opentelemetry/context-async-hooks": "^0.10.2",
"@opentelemetry/core": "^0.10.2",
"@opentelemetry/tracing": "^0.10.2",
"axios": "^0.19.2",
"body-parser": "^1.19.0",
"express": "^4.17.1"
"axios": "0.19.2",
"body-parser": "1.19.0",
"express": "4.17.1"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"devDependencies": {
"@commitlint/cli": "9.1.1",
"@commitlint/config-conventional": "9.1.1",
"@typescript-eslint/eslint-plugin": "3.8.0",
"@typescript-eslint/parser": "3.8.0",
"@typescript-eslint/eslint-plugin": "3.9.0",
"@typescript-eslint/parser": "3.9.0",
"beautify-benchmark": "0.2.4",
"benchmark": "2.1.4",
"eslint": "7.6.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@opentelemetry/context-base": "^0.10.2"
},
"devDependencies": {
"@types/mocha": "8.0.1",
"@types/mocha": "8.0.2",
"@types/node": "14.0.27",
"@types/webpack-env": "1.15.2",
"codecov": "3.7.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-context-async-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"access": "public"
},
"devDependencies": {
"@types/mocha": "8.0.1",
"@types/mocha": "8.0.2",
"@types/node": "14.0.27",
"@types/shimmer": "1.0.1",
"codecov": "3.7.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import { ContextManager, Context } from '@opentelemetry/context-base';
import { EventEmitter } from 'events';

const kOtListeners = Symbol('OtListeners');

type Func<T> = (...args: unknown[]) => T;

type PatchedEventEmitter = {
Expand All @@ -25,7 +27,7 @@ type PatchedEventEmitter = {
* version so when the listener is removed by the user, we remove the
* corresponding "patched" function.
*/
__ot_listeners?: { [name: string]: WeakMap<Func<void>, Func<void>> };
[kOtListeners]?: { [name: string]: WeakMap<Func<void>, Func<void>> };
} & EventEmitter;

const ADD_LISTENER_METHODS = [
Expand Down Expand Up @@ -91,8 +93,8 @@ export abstract class AbstractAsyncHooksContextManager
context: Context
): T {
const ee = (target as unknown) as PatchedEventEmitter;
if (ee.__ot_listeners !== undefined) return target;
ee.__ot_listeners = {};
if (ee[kOtListeners] !== undefined) return target;
ee[kOtListeners] = {};

// patch methods that add a listener to propagate context
ADD_LISTENER_METHODS.forEach(methodName => {
Expand Down Expand Up @@ -124,13 +126,10 @@ export abstract class AbstractAsyncHooksContextManager
*/
private _patchRemoveListener(ee: PatchedEventEmitter, original: Function) {
return function (this: {}, event: string, listener: Func<void>) {
if (
ee.__ot_listeners === undefined ||
ee.__ot_listeners[event] === undefined
) {
const events = ee[kOtListeners]?.[event];
if (events === undefined) {
return original.call(this, event, listener);
}
const events = ee.__ot_listeners[event];
const patchedListener = events.get(listener);
return original.call(this, event, patchedListener || listener);
};
Expand All @@ -147,13 +146,9 @@ export abstract class AbstractAsyncHooksContextManager
original: Function
) {
return function (this: {}, event: string) {
if (
ee.__ot_listeners === undefined ||
ee.__ot_listeners[event] === undefined
) {
return original.call(this, event);
if (ee[kOtListeners]?.[event] !== undefined) {
delete ee[kOtListeners]![event];
}
delete ee.__ot_listeners[event];
return original.call(this, event);
};
}
Expand All @@ -172,11 +167,11 @@ export abstract class AbstractAsyncHooksContextManager
) {
const contextManager = this;
return function (this: {}, event: string, listener: Func<void>) {
if (ee.__ot_listeners === undefined) ee.__ot_listeners = {};
let listeners = ee.__ot_listeners[event];
if (ee[kOtListeners] === undefined) ee[kOtListeners] = {};
let listeners = ee[kOtListeners]![event];
if (listeners === undefined) {
listeners = new WeakMap();
ee.__ot_listeners[event] = listeners;
ee[kOtListeners]![event] = listeners;
}
const patchedListener = contextManager.bind(listener, context);
// store a weak reference of the user listener to ours
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-context-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"access": "public"
},
"devDependencies": {
"@types/mocha": "8.0.1",
"@types/mocha": "8.0.2",
"@types/node": "14.0.27",
"codecov": "3.7.2",
"gts": "2.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-context-zone-peer-dep/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"devDependencies": {
"@babel/core": "7.11.1",
"@types/mocha": "8.0.1",
"@types/mocha": "8.0.2",
"@types/node": "14.0.27",
"@types/sinon": "9.0.4",
"@types/webpack-env": "1.15.2",
Expand All @@ -61,7 +61,7 @@
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "9.0.2",
"sinon": "9.0.3",
"ts-loader": "8.0.2",
"ts-mocha": "7.0.0",
"ts-node": "8.10.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-context-zone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"devDependencies": {
"@babel/core": "7.11.1",
"@types/mocha": "8.0.1",
"@types/mocha": "8.0.2",
"@types/node": "14.0.27",
"@types/sinon": "9.0.4",
"@types/webpack-env": "1.15.2",
Expand All @@ -55,7 +55,7 @@
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "9.0.2",
"sinon": "9.0.3",
"ts-loader": "8.0.2",
"ts-mocha": "7.0.0",
"ts-node": "8.10.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"access": "public"
},
"devDependencies": {
"@types/mocha": "8.0.1",
"@types/mocha": "8.0.2",
"@types/node": "14.0.27",
"@types/semver": "7.3.1",
"@types/semver": "7.3.2",
"@types/sinon": "9.0.4",
"@types/webpack-env": "1.15.2",
"codecov": "3.7.2",
Expand All @@ -69,7 +69,7 @@
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "9.0.2",
"sinon": "9.0.3",
"ts-loader": "8.0.2",
"ts-mocha": "7.0.0",
"ts-node": "8.10.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ import { getParentSpanContext, setExtractedSpanContext } from '../context';

export const TRACE_PARENT_HEADER = 'traceparent';
export const TRACE_STATE_HEADER = 'tracestate';
const VALID_TRACE_PARENT_REGEX = /^(?!ff)[\da-f]{2}-([\da-f]{32})-([\da-f]{16})-([\da-f]{2})(-|$)/;

const VERSION = '00';
const VERSION_PART_COUNT = 4; // Version 00 only allows the specific 4 fields.

const VERSION_REGEX = /^(?!ff)[\da-f]{2}$/;
const TRACE_ID_REGEX = /^(?![0]{32})[\da-f]{32}$/;
const PARENT_ID_REGEX = /^(?![0]{16})[\da-f]{16}$/;
const FLAGS_REGEX = /^[\da-f]{2}$/;

/**
* Parses information from the [traceparent] span tag and converts it into {@link SpanContext}
Expand All @@ -41,19 +47,33 @@ const VERSION = '00';
* For more information see {@link https://www.w3.org/TR/trace-context/}
*/
export function parseTraceParent(traceParent: string): SpanContext | null {
const match = traceParent.match(VALID_TRACE_PARENT_REGEX);
const trimmed = traceParent.trim();
const traceParentParts = trimmed.split('-');

// Current version must be structured correctly.
// For future versions, we can grab just the parts we do support.
if (
!match ||
match[1] === '00000000000000000000000000000000' ||
match[2] === '0000000000000000'
traceParentParts[0] === VERSION &&
traceParentParts.length !== VERSION_PART_COUNT
) {
return null;
}

const [version, traceId, parentId, flags] = traceParentParts;
const isValidParent =
VERSION_REGEX.test(version) &&
TRACE_ID_REGEX.test(traceId) &&
PARENT_ID_REGEX.test(parentId) &&
FLAGS_REGEX.test(flags);

if (!isValidParent) {
return null;
}

return {
traceId: match[1],
spanId: match[2],
traceFlags: parseInt(match[3], 16),
traceId: traceId,
spanId: parentId,
traceFlags: parseInt(flags, 16),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ export class HttpCorrelationContext implements HttpTextPropagator {
return context;
}
const pairs = headerValue.split(ITEMS_SEPARATOR);
if (pairs.length == 1) return context;
pairs.forEach(entry => {
const keyPair = this._parsePairKeyValue(entry);
if (keyPair) {
correlationContext[keyPair.key] = { value: keyPair.value };
}
});
if (Object.entries(correlationContext).length === 0) {
return context;
}
return setCorrelationContext(context, correlationContext);
}

Expand All @@ -107,7 +109,7 @@ export class HttpCorrelationContext implements HttpTextPropagator {
const keyPairPart = valueProps.shift();
if (!keyPairPart) return;
const keyPair = keyPairPart.split(KEY_PAIR_SEPARATOR);
if (keyPair.length <= 1) return;
if (keyPair.length != 2) return;
const key = decodeURIComponent(keyPair[0].trim());
let value = decodeURIComponent(keyPair[1].trim());
if (valueProps.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,36 @@
*/
import { IdGenerator } from '../../trace/IdGenerator';

type WindowWithMsCrypto = Window & {
msCrypto?: Crypto;
};
const cryptoLib = window.crypto || (window as WindowWithMsCrypto).msCrypto;
const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;
const randomBytesArray = new Uint8Array(TRACE_ID_BYTES);

export class RandomIdGenerator implements IdGenerator {
/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits.
*/
generateTraceId(): string {
cryptoLib.getRandomValues(randomBytesArray);
return this.toHex(randomBytesArray.slice(0, TRACE_ID_BYTES));
}
generateTraceId = getIdGenerator(TRACE_ID_BYTES);

/**
* Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
* characters corresponding to 64 bits.
*/
generateSpanId(): string {
cryptoLib.getRandomValues(randomBytesArray);
return this.toHex(randomBytesArray.slice(0, SPAN_ID_BYTES));
}

/**
* Get the hex string representation of a byte array
*
* @param byteArray
*/
private toHex(byteArray: Uint8Array) {
const chars: number[] = new Array(byteArray.length * 2);
const alpha = 'a'.charCodeAt(0) - 10;
const digit = '0'.charCodeAt(0);
generateSpanId = getIdGenerator(SPAN_ID_BYTES);
}

let p = 0;
for (let i = 0; i < byteArray.length; i++) {
let nibble = (byteArray[i] >>> 4) & 0xf;
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
nibble = byteArray[i] & 0xf;
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
const SHARED_CHAR_CODES_ARRAY = Array(32);
function getIdGenerator(bytes: number): () => string {
return function generateId() {
for (let i = 0; i < bytes * 2; i++) {
SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48;
// valid hex characters in the range 48-57 and 97-102
if (SHARED_CHAR_CODES_ARRAY[i] >= 58) {
SHARED_CHAR_CODES_ARRAY[i] += 39;
}
}
return String.fromCharCode.apply(null, chars);
}
return String.fromCharCode.apply(
null,
SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2)
);
};
}
Loading