-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add and expose new function * change node version in release.yml * add customSpanProcessor * update version * rename function
- Loading branch information
Showing
5 changed files
with
127 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Context } from '@opentelemetry/api'; | ||
import { Span, ReadableSpan, SpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
|
||
/** | ||
* SpanProcessor for special operations | ||
*/ | ||
export class CustomSpanProcessor implements SpanProcessor { | ||
|
||
/** | ||
* Map of custom attributes that should be added to all spans | ||
*/ | ||
private customAttributes:Map<string,string>; | ||
|
||
constructor() { | ||
this.customAttributes = new Map<string, string>(); | ||
} | ||
|
||
forceFlush(): Promise<void> { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
onEnd(span: ReadableSpan): void { | ||
// No operation necessary | ||
} | ||
|
||
onStart(span: Span, parentContext: Context): void { | ||
if(this.customAttributes.size > 0) { | ||
this.customAttributes.forEach( | ||
// For some reason forEach() twists key and value | ||
(value:string, key:string) => span.setAttribute(key,value) | ||
) | ||
} | ||
} | ||
|
||
shutdown(): Promise<void> { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
public addCustomAttribute(key:string, value:string) { | ||
this.customAttributes.set(key, value); | ||
} | ||
} | ||
|
||
/** | ||
* Storage to allow the use of multiple SpanProcessors | ||
*/ | ||
export class MultiSpanProcessor implements SpanProcessor { | ||
|
||
private readonly spanProcessors: SpanProcessor[]; | ||
constructor(spanProcessors: SpanProcessor[]) { | ||
this.spanProcessors = spanProcessors; | ||
} | ||
|
||
forceFlush(): Promise<void> { | ||
return Promise.all( | ||
this.spanProcessors.map((processor) => { | ||
if (processor.forceFlush) { | ||
return processor.forceFlush(); | ||
} | ||
return Promise.resolve(); | ||
}) | ||
).then(() => {}); | ||
} | ||
|
||
onEnd(span: ReadableSpan): void { | ||
for (const processor of this.spanProcessors) { | ||
processor.onEnd(span); | ||
} | ||
} | ||
|
||
onStart(span: Span, parentContext: Context): void { | ||
for (const processor of this.spanProcessors) { | ||
processor.onStart(span, parentContext); | ||
} | ||
} | ||
|
||
shutdown(): Promise<void> { | ||
return Promise.all( | ||
this.spanProcessors.map((processor) => processor.shutdown()) | ||
).then(() => {}); | ||
} | ||
} | ||
|
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