-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5014 from novuhq/add-otel
feat(pkg): Add Open-Telemetry and Prometheus Montioring to Novu
- Loading branch information
Showing
17 changed files
with
1,725 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -545,6 +545,9 @@ | |
"Stdev", | ||
"openapi", | ||
"headerapikey", | ||
"isend", | ||
"Otel", | ||
"opentelemetry", | ||
"INITDB", | ||
"isend", | ||
"Idand" | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './tracing.module'; | ||
export * from './otel-wrapper'; |
100 changes: 100 additions & 0 deletions
100
packages/application-generic/src/tracing/otel-wrapper.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,100 @@ | ||
import { Span } from 'nestjs-otel'; | ||
import { TraceService as setTraceService } from 'nestjs-otel'; | ||
import { MetricService as setMetricService } from 'nestjs-otel'; | ||
import { OtelInstanceCounter as setOtelInstanceCounter } from 'nestjs-otel'; | ||
import { OtelUpDownCounter as setOtelUpDownCounter } from 'nestjs-otel'; | ||
import { OtelHistogram as setOtelHistogram } from 'nestjs-otel'; | ||
import { OtelObservableGauge as setOtelObservableGauge } from 'nestjs-otel'; | ||
import { OtelObservableCounter as setOtelObservableCounter } from 'nestjs-otel'; | ||
import { OtelObservableUpDownCounter as setOtelObservableUpDownCounter } from 'nestjs-otel'; | ||
import { OtelCounter as setOtelCounter } from 'nestjs-otel'; | ||
import { MetricOptions, SpanOptions } from '@opentelemetry/api'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { PipeTransform, Type } from '@nestjs/common'; | ||
|
||
export type OtelDataOrPipe = | ||
| string | ||
| PipeTransform<any, any> | ||
| Type<PipeTransform<any, any>>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelSpan(name?: string, options?: SpanOptions) { | ||
return Span(name, options); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelInstanceCounter(options?: MetricOptions) { | ||
return setOtelInstanceCounter(options); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelUpDownCounter(...dataOrPipes: OtelDataOrPipe[]) { | ||
return setOtelUpDownCounter(...dataOrPipes); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelHistogram(...dataOrPipes: OtelDataOrPipe[]) { | ||
return setOtelHistogram(...dataOrPipes); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelObservableGauge(...dataOrPipes: OtelDataOrPipe[]) { | ||
return setOtelObservableGauge(...dataOrPipes); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelObservableCounter(...dataOrPipes: OtelDataOrPipe[]) { | ||
return setOtelObservableCounter(...dataOrPipes); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelObservableUpDownCounter(...dataOrPipes: OtelDataOrPipe[]) { | ||
return setOtelObservableUpDownCounter(...dataOrPipes); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function OtelCounter(...dataOrPipes: OtelDataOrPipe[]) { | ||
return setOtelCounter(...dataOrPipes); | ||
} | ||
|
||
@Injectable() | ||
export class TraceService extends setTraceService { | ||
getTracer() { | ||
return super.getTracer(); | ||
} | ||
|
||
getSpan() { | ||
return super.getSpan(); | ||
} | ||
|
||
startSpan(name: string) { | ||
return super.startSpan(name); | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class MetricService extends setMetricService { | ||
getCounter(name, options) { | ||
return super.getCounter(name, options); | ||
} | ||
|
||
getUpDownCounter(name, options) { | ||
return super.getUpDownCounter(name, options); | ||
} | ||
|
||
getHistogram(name, options) { | ||
return super.getHistogram(name, options); | ||
} | ||
|
||
getObservableCounter(name, options) { | ||
return super.getObservableCounter(name, options); | ||
} | ||
|
||
getObservableGauge(name, options) { | ||
return super.getObservableGauge(name, options); | ||
} | ||
|
||
getObservableUpDownCounter(name, options) { | ||
return super.getObservableUpDownCounter(name, options); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/application-generic/src/tracing/tracing.module.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,34 @@ | ||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { TracingService } from './tracing.service'; | ||
import { OpenTelemetryModule } from 'nestjs-otel'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const OtelModule = OpenTelemetryModule.forRoot({ | ||
metrics: { | ||
hostMetrics: true, | ||
apiMetrics: { | ||
enable: true, | ||
ignoreRoutes: ['/favicon.ico', '/v1/health-check'], | ||
//Records metrics for all URLs, even undefined ones | ||
ignoreUndefinedRoutes: true, | ||
}, | ||
}, | ||
}); | ||
|
||
@Module({}) | ||
export class TracingModule { | ||
static register(serviceName: string): DynamicModule { | ||
return { | ||
module: TracingModule, | ||
imports: [OtelModule], | ||
providers: [ | ||
TracingService, | ||
{ provide: 'TRACING_SERVICE_NAME', useValue: serviceName }, | ||
{ | ||
provide: 'TRACING_ENABLE_OTEL', | ||
useValue: process.env.ENABLE_OTEL === 'true', | ||
}, | ||
], | ||
}; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/application-generic/src/tracing/tracing.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,34 @@ | ||
import { | ||
Inject, | ||
Injectable, | ||
OnModuleDestroy, | ||
OnModuleInit, | ||
} from '@nestjs/common'; | ||
import { initializeOtelSdk } from './tracing'; | ||
import { NodeSDK } from '@opentelemetry/sdk-node'; | ||
|
||
@Injectable() | ||
export class TracingService implements OnModuleInit, OnModuleDestroy { | ||
private otelSDKInstance: NodeSDK; | ||
|
||
constructor( | ||
@Inject('TRACING_SERVICE_NAME') private readonly serviceName: string, | ||
@Inject('TRACING_ENABLE_OTEL') private readonly otelEnabled: boolean | ||
) {} | ||
|
||
async onModuleDestroy() { | ||
if (this.otelSDKInstance) { | ||
await this.otelSDKInstance.shutdown(); | ||
} | ||
} | ||
onModuleInit() { | ||
if (!this.hasValidParameters()) return; | ||
|
||
this.otelSDKInstance = initializeOtelSdk(this.serviceName); | ||
this.otelSDKInstance.start(); | ||
} | ||
|
||
private hasValidParameters() { | ||
return this.serviceName && this.otelEnabled; | ||
} | ||
} |
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,37 @@ | ||
import { | ||
CompositePropagator, | ||
W3CBaggagePropagator, | ||
W3CTraceContextPropagator, | ||
} from '@opentelemetry/core'; | ||
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; | ||
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; | ||
import { JaegerPropagator } from '@opentelemetry/propagator-jaeger'; | ||
import { B3InjectEncoding, B3Propagator } from '@opentelemetry/propagator-b3'; | ||
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'; | ||
import { NodeSDK } from '@opentelemetry/sdk-node'; | ||
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function initializeOtelSdk(serviceName: string) { | ||
return new NodeSDK({ | ||
metricReader: new PrometheusExporter({ | ||
port: 9464, | ||
}), | ||
spanProcessor: new BatchSpanProcessor(new JaegerExporter()), | ||
contextManager: new AsyncLocalStorageContextManager(), | ||
serviceName: serviceName, | ||
textMapPropagator: new CompositePropagator({ | ||
propagators: [ | ||
new JaegerPropagator(), | ||
new W3CTraceContextPropagator(), | ||
new W3CBaggagePropagator(), | ||
new B3Propagator(), | ||
new B3Propagator({ | ||
injectEncoding: B3InjectEncoding.MULTI_HEADER, | ||
}), | ||
], | ||
}), | ||
instrumentations: [getNodeAutoInstrumentations()], | ||
}); | ||
} |
Oops, something went wrong.