-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SimpleSampledSpanProcessor
- Loading branch information
1 parent
b208b2b
commit d8c68ae
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/opentelemetry-basic-tracer/src/export/SimpleSampledSpanProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
packages/opentelemetry-basic-tracer/test/export/SimpleSampledSpanProcessor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |