Skip to content

Commit

Permalink
feat: add SimpleSampledSpanProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Aug 22, 2019
1 parent b208b2b commit d8c68ae
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* 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
*
* https://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 { TraceOptions } from '@opentelemetry/types';
import { SpanProcessor } from '../SpanProcessor';
import { SpanExporter } from './SpanExporter';
import { Span } from '../Span';

/**
* An implementation of the {@link SpanProcessor} that converts the {@link Span}
* to {@link ReadableSpan} and passes it to the configured exporter.
*
* Only spans that are sampled are converted.
*/
export class SimpleSampledSpanProcessor implements SpanProcessor {
constructor(private readonly exporter: SpanExporter) {}

// does nothing.
onStart(span: Span): void {}

onEnd(span: Span): void {
if (span.context().traceOptions !== TraceOptions.SAMPLED) return;
this.exporter.export([span.toReadableSpan()], () => {});
}

shutdown(): void {
this.exporter.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* 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
*
* https://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 * as assert from 'assert';
import { SimpleSampledSpanProcessor } from '../../src/export/SimpleSampledSpanProcessor';
import { NoopTracer, NoopLogger } from '@opentelemetry/core';
import { Span } from '../../src';
import { SpanExporter } from '../../src/export/SpanExporter';
import { ReadableSpan } from '../../src/export/ReadableSpan';
import { SpanContext, SpanKind, TraceOptions } from '@opentelemetry/types';

class TestExporter implements SpanExporter {
spansDataList: ReadableSpan[] = [];
export(spans: ReadableSpan[]): void {
this.spansDataList.push(...spans);
}

shutdown(): void {
this.spansDataList = [];
}
}

describe('SimpleSampledSpanProcessor', () => {
const tracer = new NoopTracer();
const logger = new NoopLogger();
const exporter = new TestExporter();

describe('constructor', () => {
it('should create a SimpleSampledSpanProcessor instance', () => {
const processor = new SimpleSampledSpanProcessor(exporter);
assert.ok(processor instanceof SimpleSampledSpanProcessor);
});
});

describe('.onStart/.onEnd/.shutdown', () => {
it('should handle span started and ended when SAMPLED', () => {
const processor = new SimpleSampledSpanProcessor(exporter);
const spanContext: SpanContext = {
traceId: 'a3cda95b652f4a1592b449d5929fda1b',
spanId: '5e0c63257de34c92',
traceOptions: TraceOptions.SAMPLED,
};
const span = new Span(
tracer,
logger,
'span-name',
spanContext,
SpanKind.CLIENT
);
processor.onStart(span);
assert.strictEqual(exporter.spansDataList.length, 0);

processor.onEnd(span);
assert.strictEqual(exporter.spansDataList.length, 1);

processor.shutdown();
assert.strictEqual(exporter.spansDataList.length, 0);
});

it('should handle span started and ended when UNSAMPLED', () => {
const processor = new SimpleSampledSpanProcessor(exporter);
const spanContext: SpanContext = {
traceId: 'a3cda95b652f4a1592b449d5929fda1b',
spanId: '5e0c63257de34c92',
traceOptions: TraceOptions.UNSAMPLED,
};
const span = new Span(
tracer,
logger,
'span-name',
spanContext,
SpanKind.CLIENT
);
processor.onStart(span);
assert.strictEqual(exporter.spansDataList.length, 0);

processor.onEnd(span);
assert.strictEqual(exporter.spansDataList.length, 0);

processor.shutdown();
assert.strictEqual(exporter.spansDataList.length, 0);
});
});
});

0 comments on commit d8c68ae

Please sign in to comment.