Skip to content

Commit 16109d3

Browse files
Merge branch 'main' into esm-support
2 parents 1f017b0 + b8b6308 commit 16109d3

14 files changed

Lines changed: 423 additions & 9 deletions

File tree

experimental/packages/opentelemetry-shim-opencensus/.eslintignore renamed to experimental/packages/shim-opencensus/.eslintignore

File renamed without changes.

experimental/packages/opentelemetry-shim-opencensus/.eslintrc.js renamed to experimental/packages/shim-opencensus/.eslintrc.js

File renamed without changes.

experimental/packages/opentelemetry-shim-opencensus/.npmignore renamed to experimental/packages/shim-opencensus/.npmignore

File renamed without changes.
File renamed without changes.

experimental/packages/opentelemetry-shim-opencensus/README.md renamed to experimental/packages/shim-opencensus/README.md

File renamed without changes.

experimental/packages/opentelemetry-shim-opencensus/package.json renamed to experimental/packages/shim-opencensus/package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"prepublishOnly": "npm run compile",
1111
"compile": "tsc --build",
1212
"clean": "tsc --build --clean",
13+
"tdd": "npm run test -- --extension ts --watch",
1314
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",
1415
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../",
1516
"lint": "eslint . --ext .ts",
@@ -44,28 +45,30 @@
4445
"access": "public"
4546
},
4647
"devDependencies": {
47-
"@opentelemetry/core": "1.11.0",
48-
"@opentelemetry/context-async-hooks": "1.11.0",
48+
"@opentelemetry/core": "1.12.0",
49+
"@opentelemetry/context-async-hooks": "1.12.0",
4950
"@opencensus/core": "0.1.0",
50-
"@opentelemetry/api": ">=1.0.0 <1.5.0",
51+
"@opentelemetry/api": "1.4.1",
5152
"@types/mocha": "10.0.0",
5253
"@types/node": "18.6.5",
5354
"codecov": "3.8.3",
5455
"mocha": "10.0.0",
5556
"nyc": "15.1.0",
57+
"sinon": "15.0.0",
58+
"@types/sinon": "10.0.13",
5659
"ts-mocha": "10.0.0",
5760
"typescript": "4.4.4"
5861
},
5962
"peerDependencies": {
6063
"@opencensus/core": "^0.1.0",
61-
"@opentelemetry/api": ">=1.0.0 <1.5.0"
64+
"@opentelemetry/api": "^1.0.0"
6265
},
6366
"dependencies": {
6467
"@opentelemetry/core": "^1.0.0",
6568
"@opentelemetry/context-async-hooks": "^1.0.0",
6669
"semver": "^7.3.5",
6770
"require-in-the-middle": "^6.0.0"
6871
},
69-
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-shim-opencensus",
72+
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/shim-opencensus",
7073
"sideEffects": false
71-
}
74+
}

experimental/packages/opentelemetry-shim-opencensus/src/index.ts renamed to experimental/packages/shim-opencensus/src/index.ts

File renamed without changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as oc from '@opencensus/core';
18+
19+
import {
20+
context,
21+
propagation,
22+
trace,
23+
TextMapGetter,
24+
TextMapSetter,
25+
} from '@opentelemetry/api';
26+
import { mapSpanContext, reverseMapSpanContext } from './transform';
27+
28+
class Getter implements TextMapGetter<void> {
29+
constructor(private ocGetter: oc.HeaderGetter) {}
30+
keys(): string[] {
31+
return [];
32+
}
33+
get(carrier: void, key: string) {
34+
return this.ocGetter.getHeader(key);
35+
}
36+
}
37+
38+
class Setter implements TextMapSetter<void> {
39+
constructor(private ocSetter: oc.HeaderSetter) {}
40+
set(carrier: void, key: string, value: string): void {
41+
this.ocSetter.setHeader(key, value);
42+
}
43+
}
44+
45+
/**
46+
* Bridges OpenTelemetry propagation API into OpenCensus. The global OTel propagator is called
47+
* to implement the OpenCensus propagation API.
48+
*/
49+
export const shimPropagation: oc.Propagation = {
50+
extract(getter: oc.HeaderGetter): oc.SpanContext | null {
51+
const extracted = propagation.extract(
52+
context.active(),
53+
null,
54+
new Getter(getter)
55+
);
56+
57+
const otelSc = trace.getSpanContext(extracted);
58+
return otelSc ? reverseMapSpanContext(otelSc) : null;
59+
},
60+
61+
inject(setter: oc.HeaderSetter, spanContext: oc.SpanContext): void {
62+
const ctx = trace.setSpanContext(
63+
context.active(),
64+
mapSpanContext(spanContext)
65+
);
66+
propagation.inject(ctx, null, new Setter(setter));
67+
},
68+
69+
generate(): oc.SpanContext {
70+
// Reading OpenCensus code, it looks like this should generate a new random span context.
71+
// However, it doesn't appear to be used based on my testing. Options for implementing:
72+
//
73+
// - Return the invalid span context
74+
// - Use the OTel ID generator, however this package should be an API-only bridge
75+
// - Copy implementation from OpenCensus noop-propagation.ts
76+
throw new Error('shimPropagation.generate() is not yet implemented');
77+
},
78+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as oc from '@opencensus/core';
18+
import {
19+
Attributes,
20+
SpanContext,
21+
SpanKind,
22+
TimeInput,
23+
diag,
24+
} from '@opentelemetry/api';
25+
import { TraceState } from '@opentelemetry/core';
26+
27+
function exhaust(value: never) {
28+
diag.warn('Could not handle enum value %s', value);
29+
}
30+
31+
export function mapSpanKind(
32+
kind: oc.SpanKind | undefined
33+
): SpanKind | undefined {
34+
switch (kind) {
35+
case undefined:
36+
return undefined;
37+
case oc.SpanKind.UNSPECIFIED:
38+
return SpanKind.INTERNAL;
39+
case oc.SpanKind.CLIENT:
40+
return SpanKind.CLIENT;
41+
case oc.SpanKind.SERVER:
42+
return SpanKind.SERVER;
43+
default:
44+
exhaust(kind);
45+
return undefined;
46+
}
47+
}
48+
49+
export function mapSpanContext({
50+
spanId,
51+
traceId,
52+
options,
53+
traceState,
54+
}: oc.SpanContext): SpanContext {
55+
return {
56+
spanId,
57+
traceId,
58+
traceFlags: options ?? 0,
59+
traceState:
60+
traceState === undefined ? undefined : new TraceState(traceState),
61+
};
62+
}
63+
64+
export function reverseMapSpanContext({
65+
spanId,
66+
traceId,
67+
traceFlags,
68+
traceState,
69+
}: SpanContext): oc.SpanContext {
70+
return {
71+
spanId: spanId,
72+
traceId: traceId,
73+
options: traceFlags,
74+
traceState: traceState?.serialize(),
75+
};
76+
}
77+
78+
// Copied from Java
79+
// https://github.com/open-telemetry/opentelemetry-java/blob/0d3a04669e51b33ea47b29399a7af00012d25ccb/opencensus-shim/src/main/java/io/opentelemetry/opencensusshim/SpanConverter.java#L24-L27
80+
const MESSAGE_EVENT_ATTRIBUTE_KEY_TYPE = 'message.event.type';
81+
const MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_UNCOMPRESSED =
82+
'message.event.size.uncompressed';
83+
const MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_COMPRESSED =
84+
'message.event.size.compressed';
85+
86+
export function mapMessageEvent(
87+
type: oc.MessageEventType,
88+
id: number,
89+
timestamp?: number,
90+
uncompressedSize?: number,
91+
compressedSize?: number
92+
): [string, Attributes, TimeInput | undefined] {
93+
const attributes: Attributes = {
94+
[MESSAGE_EVENT_ATTRIBUTE_KEY_TYPE]: oc.MessageEventType[type],
95+
};
96+
if (uncompressedSize !== undefined) {
97+
attributes[MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_UNCOMPRESSED] =
98+
uncompressedSize;
99+
}
100+
if (compressedSize !== undefined) {
101+
attributes[MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_COMPRESSED] = compressedSize;
102+
}
103+
104+
return [id.toString(), attributes, timestamp];
105+
}

experimental/packages/opentelemetry-shim-opencensus/test/ShimTracer.test.ts renamed to experimental/packages/shim-opencensus/test/ShimTracer.test.ts

File renamed without changes.

0 commit comments

Comments
 (0)