Skip to content

Commit

Permalink
Just use @opentelemetry/propagator-jaeger
Browse files Browse the repository at this point in the history
  • Loading branch information
MTRNord committed Oct 23, 2023
1 parent d78fc6d commit 37b0d0b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 100 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@opentelemetry/auto-instrumentations-node": "^0.38.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.41.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.41.0",
"@opentelemetry/propagator-jaeger": "^1.17.1",
"@opentelemetry/sdk-metrics": "^1.15.0",
"@opentelemetry/sdk-node": "^0.41.0",
"@opentelemetry/sdk-trace-base": "^1.15.0",
Expand Down
102 changes: 2 additions & 100 deletions src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { NodeSDK } from "@opentelemetry/sdk-node";
import { AlwaysOnSampler, Sampler, SamplingDecision } from '@opentelemetry/sdk-trace-base';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { DiagConsoleLogger, DiagLogLevel, Attributes, SpanKind, diag, TextMapPropagator, Context, TextMapGetter, TextMapSetter } from '@opentelemetry/api';
import { DiagConsoleLogger, DiagLogLevel, Attributes, SpanKind, diag, } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import * as api from '@opentelemetry/api';

export enum DRAUPNIR_SYSTEM_TYPES {
APPSERVICE = "appservice",
Expand All @@ -22,103 +21,6 @@ export enum DRAUPNIR_TRACING_ATTRIBUTES {
PROVISION_OUTCOME = "draupnir.provision.outcome"
}

// Value is expected to be of form: `{trace_id}:{span_id}:{parent_id}:{flags}`
const SYNAPSE_TRACE_HEADER = "uber-trace-id";

const SYNAPSE_BAGGAGE_HEADER_PREFIX = "uberctx-";

const FIELDS = [SYNAPSE_TRACE_HEADER];

function readHeader(
carrier: unknown,
getter: TextMapGetter,
key: string
): string {
let header = getter.get(carrier, key);
if (Array.isArray(header)) [header] = header;
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);
}

class SynapseTracePropargator implements TextMapPropagator {
inject(context: Context, carrier: any, setter: TextMapSetter<any>): void {
const spanContext = api.trace.getSpan(context)?.spanContext();
if (!spanContext || !api.isSpanContextValid(spanContext)) return;

setter.set(carrier, SYNAPSE_TRACE_HEADER, `${spanContext.traceId}:${spanContext.spanId}:0:${spanContext.traceFlags}`);
const baggage = api.propagation.getBaggage(context);
if (!baggage) return;
baggage.getAllEntries().forEach(([k, v]) => {
if (!isValidHeaderName(k) || !isValidHeaderValue(v.value)) return;
setter.set(carrier, `${SYNAPSE_BAGGAGE_HEADER_PREFIX}${k}`, v.value);
});
}
extract(context: Context, carrier: any, getter: TextMapGetter<any>): Context {
const header = readHeader(carrier, getter, SYNAPSE_TRACE_HEADER);
if (header.split(':').length - 1 !== 3) {
return context;
}
const trace_data = header.split(':');
let traceId = parseInt(trace_data[0], 16);
const spanId = parseInt(trace_data[1], 16);
let parentId: number | null = parseInt(trace_data[2], 16);
if (parentId === 0) {
parentId = null;
}
const traceFlags = parseInt(trace_data[3], 16);
if (isNaN(traceId) || isNaN(spanId) || (parentId !== null && isNaN(parentId)) || isNaN(traceFlags)) {
return context;
}

context = api.trace.setSpan(
context,
api.trace.wrapSpanContext({
traceId: traceId.toString(16),
spanId: spanId.toString(16),
isRemote: true,
traceFlags,
})
);

let baggage: api.Baggage =
api.propagation.getBaggage(context) || api.propagation.createBaggage();

getter.keys(carrier).forEach(k => {
if (!k.startsWith(SYNAPSE_BAGGAGE_HEADER_PREFIX)) return;
const value = readHeader(carrier, getter, k);
baggage = baggage.setEntry(k.substr(SYNAPSE_BAGGAGE_HEADER_PREFIX.length), {
value,
});
});

if (baggage.getAllEntries().length > 0) {
context = api.propagation.setBaggage(context, baggage);
}


return context;
}
/**
* Note: fields does not include baggage headers as they are dependent on
* carrier instance. Attempting to reuse a carrier by clearing fields could
* result in a memory leak.
*/
fields(): string[] {
return FIELDS.slice();
}
}

export default function initTracer(serviceName: string) {
/**
* This starts instrumentation for the app
Expand Down Expand Up @@ -182,7 +84,7 @@ export default function initTracer(serviceName: string) {
// exporter: metrics_exporter
// }),
serviceName: serviceName,
textMapPropagator: new SynapseTracePropargator(),
textMapPropagator: new JaegerPropagator(),

Check failure on line 87 in src/tracer.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

Cannot find name 'JaegerPropagator'.
instrumentations: [getNodeAutoInstrumentations({
// This just prints an error
'@opentelemetry/instrumentation-grpc': {
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@
dependencies:
"@opentelemetry/semantic-conventions" "1.15.2"

"@opentelemetry/core@1.17.1":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@opentelemetry/core/-/core-1.17.1.tgz#10c5e09c63aeb1836b34d80baf7113760fb19d96"
integrity sha512-I6LrZvl1FF97FQXPR0iieWQmKnGxYtMbWA1GrAXnLUR+B1Hn2m8KqQNEIlZAucyv00GBgpWkpllmULmZfG8P3g==
dependencies:
"@opentelemetry/semantic-conventions" "1.17.1"

"@opentelemetry/exporter-jaeger@1.15.2":
version "1.15.2"
resolved "https://registry.npmjs.org/@opentelemetry/exporter-jaeger/-/exporter-jaeger-1.15.2.tgz"
Expand Down Expand Up @@ -780,6 +787,13 @@
dependencies:
"@opentelemetry/core" "1.15.2"

"@opentelemetry/propagator-jaeger@^1.17.1":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@opentelemetry/propagator-jaeger/-/propagator-jaeger-1.17.1.tgz#31cc43045a059d1ed3651b9f21d0fd6db817b02f"
integrity sha512-p+P4lf2pbqd3YMfZO15QCGsDwR2m1ke2q5+dq6YBLa/q0qiC2eq4cD/qhYBBed5/X4PtdamaVGHGsp+u3GXHDA==
dependencies:
"@opentelemetry/core" "1.17.1"

"@opentelemetry/redis-common@^0.36.1":
version "0.36.1"
resolved "https://registry.npmjs.org/@opentelemetry/redis-common/-/redis-common-0.36.1.tgz"
Expand Down Expand Up @@ -891,6 +905,11 @@
resolved "https://registry.npmjs.org/@opentelemetry/semantic-conventions/-/semantic-conventions-1.15.2.tgz"
integrity sha512-CjbOKwk2s+3xPIMcd5UNYQzsf+v94RczbdNix9/kQh38WiQkM90sUOi3if8eyHFgiBjBjhwXrA7W3ydiSQP9mw==

"@opentelemetry/semantic-conventions@1.17.1":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@opentelemetry/semantic-conventions/-/semantic-conventions-1.17.1.tgz#93d219935e967fbb9aa0592cc96b2c0ec817a56f"
integrity sha512-xbR2U+2YjauIuo42qmE8XyJK6dYeRMLJuOlUP5SO4auET4VtOHOzgkRVOq+Ik18N+Xf3YPcqJs9dZMiDddz1eQ==

"@opentelemetry/shim-opentracing@^1.15.2":
version "1.15.2"
resolved "https://registry.yarnpkg.com/@opentelemetry/shim-opentracing/-/shim-opentracing-1.15.2.tgz#c18895b7eb88745d96e9fefb4805688e46e3aeca"
Expand Down

0 comments on commit 37b0d0b

Please sign in to comment.