-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stateful implementation of metric and trace service
- Loading branch information
1 parent
39efad1
commit 066b789
Showing
11 changed files
with
418 additions
and
115 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
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
82 changes: 82 additions & 0 deletions
82
...ages/nestjs-shared/src/lib/module/open-telemetry/service/open-telemetry-metric.service.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,82 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { | ||
Attributes, | ||
BatchObservableCallback, | ||
Counter, | ||
Gauge, | ||
Histogram, | ||
Meter, | ||
MetricOptions, | ||
Observable, | ||
ObservableCounter, | ||
ObservableGauge, | ||
ObservableUpDownCounter, | ||
UpDownCounter, | ||
metrics | ||
} from '@opentelemetry/api' | ||
import { OPEN_TELEMETRY_MODULE_OPTION } from '../open-telemetry.constant' | ||
import { OpenTelemetryModuleOption } from '../open-telemetry.type' | ||
import { MetricService } from './metric.service' | ||
|
||
@Injectable() | ||
export class OpenTelemetryMetricService implements MetricService { | ||
constructor(@Inject(OPEN_TELEMETRY_MODULE_OPTION) private readonly config: OpenTelemetryModuleOption) {} | ||
|
||
public getMeter(): Meter { | ||
return metrics.getMeter(this.config.serviceName) | ||
} | ||
|
||
public createCounter<T extends Attributes = Attributes>(name: string, options?: MetricOptions): Counter<T> { | ||
return this.getMeter().createCounter(name, options) | ||
} | ||
|
||
public createHistogram<T extends Attributes = Attributes>(name: string, options?: MetricOptions): Histogram<T> { | ||
return this.getMeter().createHistogram(name, options) | ||
} | ||
|
||
public createGauge<T extends Attributes = Attributes>(name: string, options?: MetricOptions): Gauge<T> { | ||
return this.getMeter().createGauge(name, options) | ||
} | ||
|
||
public createUpDownCounter<T extends Attributes = Attributes>( | ||
name: string, | ||
options?: MetricOptions | ||
): UpDownCounter<T> { | ||
return this.getMeter().createUpDownCounter(name, options) | ||
} | ||
|
||
public createObservableGauge<T extends Attributes = Attributes>( | ||
name: string, | ||
options?: MetricOptions | ||
): ObservableGauge<T> { | ||
return this.getMeter().createObservableGauge(name, options) | ||
} | ||
|
||
public createObservableCounter<T extends Attributes = Attributes>( | ||
name: string, | ||
options?: MetricOptions | ||
): ObservableCounter<T> { | ||
return this.getMeter().createObservableCounter(name, options) | ||
} | ||
|
||
public createObservableUpDownCounter<T extends Attributes = Attributes>( | ||
name: string, | ||
options?: MetricOptions | ||
): ObservableUpDownCounter<T> { | ||
return this.getMeter().createObservableUpDownCounter(name, options) | ||
} | ||
|
||
public addBatchObservableCallback<T extends Attributes = Attributes>( | ||
callback: BatchObservableCallback<T>, | ||
observables: Observable<T>[] | ||
): void { | ||
this.getMeter().addBatchObservableCallback(callback, observables) | ||
} | ||
|
||
public removeBatchObservableCallback<T extends Attributes = Attributes>( | ||
callback: BatchObservableCallback<T>, | ||
observables: Observable<T>[] | ||
): void { | ||
this.getMeter().removeBatchObservableCallback(callback, observables) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
packages/nestjs-shared/src/lib/module/open-telemetry/service/open-telemetry-trace.service.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,69 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
import { Context, Span, SpanOptions, context, trace } from '@opentelemetry/api' | ||
import { OPEN_TELEMETRY_MODULE_OPTION } from '../open-telemetry.constant' | ||
import { OpenTelemetryException } from '../open-telemetry.exception' | ||
import { OpenTelemetryModuleOption } from '../open-telemetry.type' | ||
import { TraceService } from './trace.service' | ||
|
||
@Injectable() | ||
export class OpenTelemetryTraceService implements TraceService { | ||
constructor(@Inject(OPEN_TELEMETRY_MODULE_OPTION) private readonly config: OpenTelemetryModuleOption) {} | ||
|
||
public getTracer() { | ||
return trace.getTracer(this.config.serviceName) | ||
} | ||
|
||
public getSpan(context: Context): Span | undefined { | ||
return trace.getSpan(context) | ||
} | ||
|
||
public getActiveSpan(): Span | undefined { | ||
return this.getSpan(context.active()) | ||
} | ||
|
||
public startSpan(name: string, options?: SpanOptions, context?: Context): Span { | ||
const tracer = this.getTracer() | ||
|
||
return tracer.startSpan(name, options, context) | ||
} | ||
|
||
public startActiveSpan<F extends (span: Span) => ReturnType<F>>(name: string, fn: F): ReturnType<F> | ||
public startActiveSpan<F extends (span: Span) => ReturnType<F>>( | ||
name: string, | ||
options: SpanOptions, | ||
fn: F | ||
): ReturnType<F> | ||
public startActiveSpan<F extends (span: Span) => ReturnType<F>>( | ||
name: string, | ||
options: SpanOptions, | ||
context: Context, | ||
fn: F | ||
): ReturnType<F> | ||
public startActiveSpan<F extends (span: Span) => ReturnType<F>>( | ||
name: string, | ||
optionsOrFnOrContext?: SpanOptions | F | Context, | ||
fnOrContextOrNothing?: F | Context | undefined, | ||
fnOrNothing?: F | undefined | ||
): ReturnType<F> { | ||
const tracer = this.getTracer() | ||
|
||
if (typeof optionsOrFnOrContext === 'function') { | ||
return tracer.startActiveSpan(name, optionsOrFnOrContext) | ||
} | ||
|
||
if (typeof fnOrContextOrNothing === 'function') { | ||
return tracer.startActiveSpan(name, optionsOrFnOrContext as SpanOptions, fnOrContextOrNothing) | ||
} | ||
|
||
if (fnOrNothing) { | ||
return tracer.startActiveSpan( | ||
name, | ||
optionsOrFnOrContext as SpanOptions, | ||
fnOrContextOrNothing as Context, | ||
fnOrNothing | ||
) | ||
} | ||
|
||
throw new OpenTelemetryException('Invalid arguments provided to startActiveSpan') | ||
} | ||
} |
Oops, something went wrong.