Skip to content

Commit

Permalink
Merge branch 'master' into feat/batch-observer
Browse files Browse the repository at this point in the history
  • Loading branch information
obecny authored Dec 14, 2020
2 parents b5485fc + 74d6e45 commit 7119c17
Show file tree
Hide file tree
Showing 85 changed files with 622 additions and 439 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Last updated March 2020
| Metrics | Beta | v0.3 | Beta |
| Context | Beta | v0.3 | Beta |
| Propagation | Beta | v0.3 | Beta |
| Correlation Context | Alpha | v0.3 | Development |
| Baggage | Alpha | v0.3 | Development |
| OpenTracing Bridge | N/A | v0.3 | Beta |
| Resources | N/A | v0.3 | Beta |

Expand Down
2 changes: 1 addition & 1 deletion getting-started/ts-example/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/express": "4.17.9",
"@types/node": "14.14.10",
"@types/node": "14.14.12",
"ts-node": "8.10.2"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion getting-started/ts-example/monitored-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/express": "4.17.9",
"@types/node": "14.14.10",
"@types/node": "14.14.12",
"ts-node": "8.10.2"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions getting-started/ts-example/traced-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/express": "4.17.9",
"@types/node": "14.14.10",
"ts-node": "9.0.0"
"@types/node": "14.14.12",
"ts-node": "9.1.1"
},
"dependencies": {
"@opentelemetry/core": "^0.13.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const axios = require("axios");
const { HttpTraceContext } = require("@opentelemetry/core");
const { BasicTracerProvider } = require("@opentelemetry/tracing");
const { context, propagation, trace } = require("@opentelemetry/api");
const { context, propagation, trace, ROOT_CONTEXT } = require("@opentelemetry/api");
const {
AsyncHooksContextManager,
} = require("@opentelemetry/context-async-hooks");
Expand Down Expand Up @@ -31,14 +31,14 @@ app.use(bodyParser.json());

// Mount our demo route
app.post("/verify-tracecontext", (req, res) => {
context.with(propagation.extract(req.headers), () => {
context.with(propagation.extract(ROOT_CONTEXT, req.headers), () => {
Promise.all(
req.body.map((action) => {
const span = tracer.startSpan("propagate-w3c");
let promise;
tracer.withSpan(span, () => {
const headers = {};
propagation.inject(headers);
propagation.inject(context.active(), headers);
promise = axios
.post(
action.url,
Expand All @@ -57,8 +57,8 @@ app.post("/verify-tracecontext", (req, res) => {
return promise;
})
)
.then(() => res.send("hello"))
.catch((err) => res.status(500).send(err));
.then(() => res.send("hello"))
.catch((err) => res.status(500).send(err));
});
});

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@
"devDependencies": {
"@commitlint/cli": "11.0.0",
"@commitlint/config-conventional": "11.0.0",
"@typescript-eslint/eslint-plugin": "4.9.0",
"@typescript-eslint/parser": "4.9.0",
"@typescript-eslint/eslint-plugin": "4.9.1",
"@typescript-eslint/parser": "4.9.1",
"beautify-benchmark": "0.2.4",
"benchmark": "2.1.4",
"eslint": "7.14.0",
"eslint": "7.15.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-header": "3.1.0",
"eslint-plugin-import": "2.22.1",
"gh-pages": "3.1.0",
"gts": "2.0.2",
"husky": "4.3.0",
"husky": "4.3.5",
"lerna": "3.22.1",
"lerna-changelog": "1.0.1",
"markdownlint-cli": "0.25.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/opentelemetry-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"@opentelemetry/context-base": "^0.13.0"
},
"devDependencies": {
"@types/mocha": "8.0.4",
"@types/node": "14.14.10",
"@types/mocha": "8.2.0",
"@types/node": "14.14.12",
"@types/sinon": "9.0.9",
"@types/webpack-env": "1.16.0",
"codecov": "3.8.1",
Expand All @@ -68,11 +68,11 @@
"karma-mocha": "2.0.1",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "4.0.2",
"linkinator": "2.4.0",
"linkinator": "2.8.0",
"mocha": "7.2.0",
"nyc": "15.1.0",
"sinon": "9.2.1",
"ts-loader": "8.0.11",
"sinon": "9.2.2",
"ts-loader": "8.0.12",
"ts-mocha": "8.0.0",
"typedoc": "0.19.2",
"typescript": "3.9.7",
Expand Down
15 changes: 6 additions & 9 deletions packages/opentelemetry-api/src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ import {
TextMapPropagator,
TextMapSetter,
} from '../context/propagation/TextMapPropagator';
import { ContextAPI } from './context';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_PROPAGATION_API_KEY,
makeGetter,
_global,
} from './global-utils';

const contextApi = ContextAPI.getInstance();

/**
* Singleton object which represents the entry point to the OpenTelemetry Propagation API
*/
Expand Down Expand Up @@ -72,29 +69,29 @@ export class PropagationAPI {
/**
* Inject context into a carrier to be propagated inter-process
*
* @param context Context carrying tracing data to inject
* @param carrier carrier to inject context into
* @param setter Function used to set values on the carrier
* @param context Context carrying tracing data to inject. Defaults to the currently active context.
*/
public inject<Carrier>(
context: Context,
carrier: Carrier,
setter: TextMapSetter<Carrier> = defaultTextMapSetter,
context: Context = contextApi.active()
setter: TextMapSetter<Carrier> = defaultTextMapSetter
): void {
return this._getGlobalPropagator().inject(context, carrier, setter);
}

/**
* Extract context from a carrier
*
* @param context Context which the newly created context will inherit from
* @param carrier Carrier to extract context from
* @param getter Function used to extract keys from a carrier
* @param context Context which the newly created context will inherit from. Defaults to the currently active context.
*/
public extract<Carrier>(
context: Context,
carrier: Carrier,
getter: TextMapGetter<Carrier> = defaultTextMapGetter,
context: Context = contextApi.active()
getter: TextMapGetter<Carrier> = defaultTextMapGetter
): Context {
return this._getGlobalPropagator().extract(context, carrier, getter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import { EntryValue } from './EntryValue';

/**
* CorrelationContext represents collection of entries. Each key of
* CorrelationContext is associated with exactly one value. CorrelationContext
* Baggage represents collection of entries. Each key of
* Baggage is associated with exactly one value. Baggage
* is serializable, to facilitate propagating it not only inside the process
* but also across process boundaries. CorrelationContext is used to annotate
* but also across process boundaries. Baggage is used to annotate
* telemetry with the name:value pair Entry. Those values can be used to add
* dimension to the metric or additional contest properties to logs and traces.
*/
export interface CorrelationContext {
export interface Baggage {
[entryKey: string]: EntryValue;
}
23 changes: 22 additions & 1 deletion packages/opentelemetry-api/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { NoopSpan, Span, SpanContext } from '../';
import { Context, createContextKey } from '@opentelemetry/context-base';
import { Baggage, NoopSpan, Span, SpanContext } from '../';

/**
* Active span key
Expand All @@ -32,6 +32,11 @@ const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
);

/**
* Baggage key
*/
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');

/**
* Return the active span if one exists
*
Expand Down Expand Up @@ -107,3 +112,19 @@ export function unsuppressInstrumentation(context: Context): Context {
export function isInstrumentationSuppressed(context: Context): boolean {
return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY));
}

/**
* @param {Context} Context that manage all context values
* @returns {Baggage} Extracted baggage from the context
*/
export function getBaggage(context: Context): Baggage | undefined {
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
}

/**
* @param {Context} Context that manage all context values
* @param {Baggage} baggage that will be set in the actual context
*/
export function setBaggage(context: Context, baggage: Baggage): Context {
return context.setValue(BAGGAGE_KEY, baggage);
}
4 changes: 2 additions & 2 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export * from './common/Time';
export * from './context/context';
export * from './context/propagation/TextMapPropagator';
export * from './context/propagation/NoopTextMapPropagator';
export * from './correlation_context/CorrelationContext';
export * from './correlation_context/EntryValue';
export * from './baggage/Baggage';
export * from './baggage/EntryValue';
export * from './metrics/BatchObserverResult';
export * from './metrics/BoundInstrument';
export * from './metrics/Meter';
Expand Down
8 changes: 2 additions & 6 deletions packages/opentelemetry-api/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
BoundCounter,
BoundBaseObserver,
} from './BoundInstrument';
import { CorrelationContext } from '../correlation_context/CorrelationContext';
import { Baggage } from '../baggage/Baggage';
import { SpanContext } from '../trace/span_context';
import { ObserverResult } from './ObserverResult';

Expand Down Expand Up @@ -194,11 +194,7 @@ export class NoopBoundCounter implements BoundCounter {
}

export class NoopBoundValueRecorder implements BoundValueRecorder {
record(
_value: number,
_correlationContext?: CorrelationContext,
_spanContext?: SpanContext
): void {
record(_value: number, _baggage?: Baggage, _spanContext?: SpanContext): void {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/trace/span_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TraceState } from './trace_state';

/**
* A SpanContext represents the portion of a {@link Span} which must be
* serialized and propagated along side of a {@link CorrelationContext}.
* serialized and propagated along side of a {@link Baggage}.
*/
export interface SpanContext {
/**
Expand Down
83 changes: 83 additions & 0 deletions packages/opentelemetry-api/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import api, {
trace,
propagation,
metrics,
TextMapPropagator,
Context,
TextMapSetter,
TextMapGetter,
ROOT_CONTEXT,
defaultTextMapSetter,
defaultTextMapGetter,
} from '../../src';

describe('API', () => {
Expand Down Expand Up @@ -84,5 +91,81 @@ describe('API', () => {
return new TestTracer();
}
}

describe('should use the global propagation', () => {
const testKey = Symbol('kTestKey');

interface Carrier {
context?: Context;
setter?: TextMapSetter;
}

class TestTextMapPropagation implements TextMapPropagator<Carrier> {
inject(
context: Context,
carrier: Carrier,
setter: TextMapSetter
): void {
carrier.context = context;
carrier.setter = setter;
}

extract(
context: Context,
carrier: Carrier,
getter: TextMapGetter
): Context {
return context.setValue(testKey, {
context,
carrier,
getter,
});
}

fields(): string[] {
return ['TestField'];
}
}

it('inject', () => {
api.propagation.setGlobalPropagator(new TestTextMapPropagation());

const context = ROOT_CONTEXT.setValue(testKey, 15);
const carrier: Carrier = {};
api.propagation.inject(context, carrier);
assert.strictEqual(carrier.context, context);
assert.strictEqual(carrier.setter, defaultTextMapSetter);

const setter: TextMapSetter = {
set: () => {},
};
api.propagation.inject(context, carrier, setter);
assert.strictEqual(carrier.context, context);
assert.strictEqual(carrier.setter, setter);
});

it('extract', () => {
api.propagation.setGlobalPropagator(new TestTextMapPropagation());

const carrier: Carrier = {};
let context = api.propagation.extract(ROOT_CONTEXT, carrier);
let data: any = context.getValue(testKey);
assert.ok(data != null);
assert.strictEqual(data.context, ROOT_CONTEXT);
assert.strictEqual(data.carrier, carrier);
assert.strictEqual(data.getter, defaultTextMapGetter);

const getter: TextMapGetter = {
keys: () => [],
get: () => undefined,
};
context = api.propagation.extract(ROOT_CONTEXT, carrier, getter);
data = context.getValue(testKey);
assert.ok(data != null);
assert.strictEqual(data.context, ROOT_CONTEXT);
assert.strictEqual(data.carrier, carrier);
assert.strictEqual(data.getter, getter);
});
});
});
});
6 changes: 3 additions & 3 deletions packages/opentelemetry-context-async-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
"access": "public"
},
"devDependencies": {
"@types/mocha": "8.0.4",
"@types/node": "14.14.10",
"@types/mocha": "8.2.0",
"@types/node": "14.14.12",
"@types/shimmer": "1.0.1",
"codecov": "3.8.1",
"gts": "2.0.2",
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"ts-mocha": "8.0.0",
"ts-node": "9.0.0",
"ts-node": "9.1.1",
"typescript": "3.9.7"
},
"dependencies": {
Expand Down
Loading

0 comments on commit 7119c17

Please sign in to comment.