-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
620 additions
and
13 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
17 changes: 16 additions & 1 deletion
17
services/workflows-service/src/common/app-logger/app-logger.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 |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import { AppLoggerService } from '@/common/app-logger/app-logger.service'; | ||
import { WinstonLogger } from '@/common/utils/winston-logger/winston-logger'; | ||
import { Global, Module } from '@nestjs/common'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [{ useClass: WinstonLogger, provide: 'LOGGER' }, AppLoggerService], | ||
exports: [AppLoggerService], | ||
providers: [ | ||
{ useClass: WinstonLogger, provide: 'LOGGER' }, | ||
AppLoggerService, | ||
{ | ||
provide: 'LOGGER_SETTINGS', | ||
useFactory: (config: ConfigService) => { | ||
return { | ||
logLevel: config.getOrThrow<string>('LOG_LEVEL'), | ||
envName: config.getOrThrow<string>('ENVIRONMENT_NAME'), | ||
}; | ||
}, | ||
inject: [ConfigService], | ||
}, | ||
], | ||
imports: [ConfigModule], | ||
}) | ||
export class AppLoggerModule {} |
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
31 changes: 31 additions & 0 deletions
31
services/workflows-service/src/tracing/http-instrumentation.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,31 @@ | ||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import { IncomingMessage } from 'http'; | ||
|
||
export function createFilter(filters: RegExp[] | string) { | ||
let f: RegExp[]; | ||
if (typeof filters === 'string') { | ||
f = filters.split(',').map(r => new RegExp(r)); | ||
} else { | ||
f = filters; | ||
} | ||
|
||
return (url: string) => { | ||
return f.some(reg => { | ||
return url.match(reg); | ||
}); | ||
}; | ||
} | ||
|
||
export class IgnoreByUrlHttpInstrumentation extends HttpInstrumentation { | ||
constructor(excludeUrls: RegExp[] | string = []) { | ||
const urlsFilter = createFilter(excludeUrls); | ||
super({ | ||
ignoreOutgoingRequestHook: () => true, | ||
ignoreIncomingRequestHook: (request: IncomingMessage) => { | ||
if (!request.url) return false; | ||
|
||
return urlsFilter(request.url); | ||
}, | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
services/workflows-service/src/tracing/trace-id.middleware.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,20 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { getTraceId } from './tracing-utils'; | ||
import * as opentelemetry from '@opentelemetry/api'; | ||
|
||
@Injectable() | ||
export class TraceIdMiddleware implements NestMiddleware { | ||
use(req: any, res: any, next: () => void) { | ||
if (req.url.includes('health')) { | ||
// TODO: add live/rediness | ||
return next(); | ||
} | ||
|
||
const span = opentelemetry.trace.getActiveSpan(); | ||
if (span && span.spanContext()) { | ||
const traceId = getTraceId(span); | ||
res.setHeader('x-trace-id', traceId); | ||
} | ||
return next(); | ||
} | ||
} |
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,55 @@ | ||
import * as api from '@opentelemetry/api'; | ||
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; | ||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
import { | ||
ConsoleSpanExporter, | ||
SimpleSpanProcessor, | ||
NodeTracerProvider, | ||
} from '@opentelemetry/sdk-trace-node'; | ||
import { PrismaInstrumentation } from '@prisma/instrumentation'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { IgnoreByUrlHttpInstrumentation } from './http-instrumentation'; | ||
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns'; | ||
import { NetInstrumentation } from '@opentelemetry/instrumentation-net'; | ||
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core'; | ||
|
||
const contextManager = new AsyncHooksContextManager(); | ||
contextManager.enable(); | ||
api.context.setGlobalContextManager(contextManager); | ||
|
||
const resource = Resource.default().merge( | ||
new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: 'workflow-service', | ||
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: | ||
process.env.ENVIRONMENT_NAME || process.env.NODE_ENV, | ||
}), | ||
); | ||
|
||
// Configure the trace provider | ||
const provider = new NodeTracerProvider({ | ||
resource, | ||
}); | ||
|
||
// Configure how spans are processed and exported. In this case we're sending spans | ||
// as we receive them to an OTLP-compatible collector (e.g. Jaeger). | ||
// provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
|
||
// Register your auto-instrumentors | ||
registerInstrumentations({ | ||
tracerProvider: provider, | ||
instrumentations: [ | ||
new DnsInstrumentation(), | ||
new NetInstrumentation(), | ||
new NestInstrumentation(), | ||
new IgnoreByUrlHttpInstrumentation([/favicon.ico/, /__vite_ping/, /health/]), | ||
new PrismaInstrumentation({ | ||
enabled: true, | ||
}), | ||
], | ||
}); | ||
|
||
// Register the provider globally | ||
provider.register(); |
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,8 @@ | ||
export function getTraceId(span: any): string | undefined { | ||
if (!span) { | ||
// TODO: Should log a warning? | ||
return; | ||
} | ||
|
||
return span.spanContext().traceId; | ||
} |