-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: span processors prevent exporter span generation
- Loading branch information
1 parent
207dfdc
commit f5d351c
Showing
7 changed files
with
240 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
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
53 changes: 53 additions & 0 deletions
53
packages/opentelemetry-tracing/test/export/TestStackContextManager.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,53 @@ | ||
/* | ||
* Copyright The 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 { ContextManager, Context } from '@opentelemetry/context-base'; | ||
|
||
/** | ||
* A test-only ContextManager that uses an in-memory stack to keep track of | ||
* the active context. | ||
* | ||
* This is not intended for advanced or asynchronous use cases. | ||
*/ | ||
export class TestStackContextManager implements ContextManager { | ||
private _contextStack: Context[] = []; | ||
|
||
active(): Context { | ||
return this._contextStack[this._contextStack.length - 1] ?? Context.ROOT_CONTEXT; | ||
} | ||
|
||
with<T extends (...args: unknown[]) => ReturnType<T>>(context: Context, fn: T): ReturnType<T> { | ||
this._contextStack.push(context); | ||
try { | ||
return fn(); | ||
} | ||
finally { | ||
this._contextStack.pop(); | ||
} | ||
} | ||
|
||
bind<T>(target: T, context?: Context): T { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
enable(): this { | ||
return this; | ||
} | ||
|
||
disable(): this { | ||
return this; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.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,93 @@ | ||
/* | ||
* Copyright The 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 { | ||
BasicTracerProvider, | ||
SpanExporter, | ||
ReadableSpan, | ||
Tracer, | ||
SpanProcessor | ||
} from '../../src'; | ||
import { ExportResult, NoopLogger, AlwaysOnSampler } from '@opentelemetry/core'; | ||
|
||
/** | ||
* A test-only span exporter that naively simulates triggering instrumentation | ||
* (creating new spans) during export. | ||
*/ | ||
export class TestTracingSpanExporter implements SpanExporter { | ||
private _processedSpans: ReadableSpan[] = []; | ||
private _exporterCreatedSpans: ReadableSpan[] = []; | ||
private _stopped = false; | ||
private _tracer: Tracer; | ||
|
||
constructor() { | ||
const tracerProvider = new BasicTracerProvider({ | ||
logger: new NoopLogger(), | ||
}); | ||
|
||
const spanProcessor: SpanProcessor = { | ||
forceFlush: () => {}, | ||
onStart: () => {}, | ||
shutdown: () => {}, | ||
onEnd: (span) => { | ||
this._exporterCreatedSpans.push(span); | ||
} | ||
} | ||
|
||
tracerProvider.addSpanProcessor(spanProcessor) | ||
|
||
this._tracer = new Tracer( | ||
{ name: 'default', version: '0.0.1' }, | ||
{ sampler: new AlwaysOnSampler() }, | ||
tracerProvider | ||
); | ||
} | ||
|
||
export( | ||
spans: ReadableSpan[], | ||
resultCallback: (result: ExportResult) => void | ||
): void { | ||
if (this._stopped) { | ||
return resultCallback(ExportResult.FAILED_NOT_RETRYABLE); | ||
} | ||
|
||
// Simulates an instrumented exporter by creating a span on the tracer. | ||
const createdSpan = this._tracer.startSpan('exporter-created-span'); | ||
createdSpan.end() | ||
|
||
this._processedSpans.push(...spans); | ||
return resultCallback(ExportResult.SUCCESS); | ||
} | ||
|
||
shutdown(): void { | ||
this._stopped = true; | ||
this._processedSpans = []; | ||
this._exporterCreatedSpans = []; | ||
} | ||
|
||
reset() { | ||
this._processedSpans = []; | ||
this._exporterCreatedSpans = []; | ||
} | ||
|
||
getExporterCreatedSpans(): ReadableSpan[] { | ||
return this._exporterCreatedSpans; | ||
} | ||
|
||
getProcessedSpans(): ReadableSpan[] { | ||
return this._processedSpans; | ||
} | ||
} |