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

Support for Synthetics-RunId #156

Merged
merged 3 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Support for Synthetic Run identification
([#156](https://github.com/signalfx/splunk-otel-js/pull/156))

## 0.10.0 (07-09-2021)

- Rename `SPLUNK_SERVICE_NAME` to `OTEL_SERVICE_NAME`
Expand Down
33 changes: 33 additions & 0 deletions src/SplunkBatchSpanProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Splunk Inc.
*
* 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.
*/
import { context, Context, propagation } from '@opentelemetry/api';
import { Span } from '@opentelemetry/tracing';
import { BatchSpanProcessor } from '@opentelemetry/tracing';

export const SYNTHETIC_RUN_ID_FIELD = 'Synthetics-RunId';

export class SplunkBatchSpanProcessor extends BatchSpanProcessor {
onStart(_span: Span, parentContext: Context = context.active()) {
super.onStart(_span);

const syntheticsId = propagation
.getBaggage(parentContext)
?.getEntry(SYNTHETIC_RUN_ID_FIELD)?.value;
if ((syntheticsId?.length ?? 0) > 0) {
_span.setAttribute(SYNTHETIC_RUN_ID_FIELD, syntheticsId);
}
}
}
11 changes: 5 additions & 6 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
*/

import { env } from 'process';
import {
BatchSpanProcessor,
SpanExporter,
SpanProcessor,
} from '@opentelemetry/tracing';
import { SpanExporter, SpanProcessor } from '@opentelemetry/tracing';
import { InstrumentationOption } from '@opentelemetry/instrumentation';
import { B3Propagator, B3InjectEncoding } from '@opentelemetry/propagator-b3';

Expand All @@ -32,8 +28,10 @@ import { TextMapPropagator } from '@opentelemetry/api';
import {
CompositePropagator,
getEnv,
HttpBaggagePropagator,
HttpTraceContextPropagator,
} from '@opentelemetry/core';
import { SplunkBatchSpanProcessor } from './SplunkBatchSpanProcessor';
import { Resource } from '@opentelemetry/resources';

const defaultEndpoint = 'http://localhost:9080/v1/trace';
Expand Down Expand Up @@ -156,14 +154,15 @@ export function defaultSpanExporterFactory(options: Options): JaegerExporter {
}

export function defaultSpanProcessorFactory(options: Options): SpanProcessor {
return new BatchSpanProcessor(options.spanExporterFactory(options));
return new SplunkBatchSpanProcessor(options.spanExporterFactory(options));
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function defaultPropagatorFactory(options: Options): TextMapPropagator {
return new CompositePropagator({
propagators: [
new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }),
new HttpBaggagePropagator(),
new HttpTraceContextPropagator(),
],
});
Expand Down
88 changes: 72 additions & 16 deletions test/propagation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@

import * as assert from 'assert';

import * as otel from '@opentelemetry/api';
import { B3Propagator } from '@opentelemetry/propagator-b3';
import { startTracing } from '../src/tracing';
import { CompositePropagator } from '@opentelemetry/core';
import {
propagation,
trace,
context,
defaultTextMapSetter,
} from '@opentelemetry/api';
import { startTracing, stopTracing } from '../src/tracing';
import { CompositePropagator, RandomIdGenerator } from '@opentelemetry/core';
import { InMemorySpanExporter, SpanProcessor } from '@opentelemetry/tracing';
import { SYNTHETIC_RUN_ID_FIELD } from '../src/SplunkBatchSpanProcessor';
import { defaultSpanProcessorFactory } from '../src/options';

describe('propagation', () => {
it('must be set to b3', done => {
startTracing();

const propagator = otel.propagation._getGlobalPropagator();
assert(
propagator instanceof CompositePropagator,
'propagator must be instance of B3Propagator'
);
assert(propagation.fields().includes('x-b3-traceid'));
assert(propagation.fields().includes('x-b3-spanid'));
assert(propagation.fields().includes('x-b3-sampled'));
assert(propagation.fields().includes('traceparent'));

const tracer = otel.trace.getTracer('test-tracer');
const tracer = trace.getTracer('test-tracer');
const span = tracer.startSpan('main');
otel.context.with(otel.trace.setSpan(otel.context.active(), span), () => {
context.with(trace.setSpan(context.active(), span), () => {
const carrier = {};
otel.propagation.inject(
otel.context.active(),
carrier,
otel.defaultTextMapSetter
);
propagation.inject(context.active(), carrier, defaultTextMapSetter);
span.end();

const traceId = span.spanContext().traceId;
Expand All @@ -50,5 +52,59 @@ describe('propagation', () => {
assert.strictEqual(carrier['traceparent'], `00-${traceId}-${spanId}-01`);
done();
});

stopTracing();
});

it('must extract synthetic run id', done => {
startTracing();
assert(propagation.fields().includes('baggage'));

const syntheticsTraceId = new RandomIdGenerator().generateTraceId();

const incomingCarrier = {
baggage: 'Synthetics-RunId=' + syntheticsTraceId,
};
const newContext = propagation.extract(context.active(), incomingCarrier);

const outgoingCarrier = {};
propagation.inject(newContext, outgoingCarrier);
assert.strictEqual(
outgoingCarrier['baggage'],
'Synthetics-RunId=' + syntheticsTraceId
);
done();
});

it('must extract synthetic run id', done => {
const exporter = new InMemorySpanExporter();
let spanProcessor: SpanProcessor;
startTracing({
spanExporterFactory: () => exporter,
spanProcessorFactory: options => {
spanProcessor = defaultSpanProcessorFactory(options);
return spanProcessor;
},
});

assert(propagation.fields().includes('baggage'));

const tracer = trace.getTracer('test-tracer');
const syntheticsTraceId = new RandomIdGenerator().generateTraceId();
const incomingCarrier = {
baggage: 'Synthetics-RunId=' + syntheticsTraceId,
};
const newContext = propagation.extract(context.active(), incomingCarrier);
tracer.startSpan('request handler', {}, newContext).end();

spanProcessor.forceFlush().then(() => {
assert(exporter.getFinishedSpans().length == 1);
assert.strictEqual(
exporter.getFinishedSpans()[0].attributes[SYNTHETIC_RUN_ID_FIELD],
syntheticsTraceId
);

done();
});
});
});