-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(extension-tracing): start to use opentelemetry
- Loading branch information
1 parent
2af06b7
commit 0181c3a
Showing
8 changed files
with
415 additions
and
141 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
73 changes: 0 additions & 73 deletions
73
extensions/tracing/src/interceptors/tracing.interceptor.ts
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/extension-tracing | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {bind, Provider, ValueOrPromise} from '@loopback/core'; | ||
import {asMiddleware, Middleware, MiddlewareContext} from '@loopback/express'; | ||
import openTelemetry, {propagation, Tracer} from '@opentelemetry/api'; | ||
import {JaegerExporter} from '@opentelemetry/exporter-jaeger'; | ||
import {BasicTracerProvider, SimpleSpanProcessor} from '@opentelemetry/tracing'; | ||
import debugFactory from 'debug'; | ||
import {TracingBindings} from '../keys'; | ||
|
||
const debug = debugFactory('loopback:tracing'); | ||
|
||
@bind(asMiddleware({group: 'tracing'})) | ||
export class TracingMiddleware implements Provider<Middleware> { | ||
private tracer: Tracer; | ||
|
||
constructor() { | ||
const provider = new BasicTracerProvider(); | ||
|
||
// Configure span processor to send spans to the exporter | ||
const exporter = new JaegerExporter({serviceName: 'basic-service'}); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
||
/** | ||
* Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings. | ||
* | ||
* This registers the tracer provider with the OpenTelemetry API as the global | ||
* tracer provider. This means when you call API methods like | ||
* `opentelemetry.trace.getTracer`, they will use this tracer provider. If you | ||
* do not register a global tracer provider, instrumentation which calls these | ||
* methods will recieve no-op implementations. | ||
*/ | ||
provider.register(); | ||
this.tracer = openTelemetry.trace.getTracer('loopback:tracer'); | ||
} | ||
|
||
value() { | ||
return this.intercept.bind(this); | ||
} | ||
|
||
async intercept<T>(reqCtx: MiddlewareContext, next: () => ValueOrPromise<T>) { | ||
let reqSpanCtx = propagation.extract( | ||
FORMAT_HTTP_HEADERS, | ||
reqCtx.request.headers, | ||
); | ||
debug('Extracted span context: %s', reqSpanCtx); | ||
let span; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
if (!reqSpanCtx || !(reqSpanCtx as any).traceId) { | ||
span = this.tracer.startSpan(reqCtx.targetName); | ||
reqSpanCtx = span.context(); | ||
debug('Inject a new span context: %s', reqSpanCtx); | ||
this.tracer.inject( | ||
reqSpanCtx, | ||
FORMAT_HTTP_HEADERS, | ||
reqCtx.request.headers, | ||
); | ||
} else { | ||
const parentSpan = reqCtx.getSync(TracingBindings.SPAN, { | ||
optional: true, | ||
}); | ||
span = this.tracer.startSpan(reqCtx.targetName, { | ||
references: [followsFrom(parentSpan?.context() ?? reqSpanCtx)], | ||
}); | ||
if (debug.enabled) debug('A new span is started: %s', span.context()); | ||
} | ||
debug('Span context at %s: %s', reqCtx.targetName, reqSpanCtx); | ||
|
||
try { | ||
reqCtx.bind(TracingBindings.SPAN).to(span); | ||
return await next(); | ||
} finally { | ||
span.finish(); | ||
if (debug.enabled) debug('The span is finished: %s', span.context()); | ||
} | ||
} | ||
} |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"extends": "@loopback/build/config/tsconfig.common.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"composite": true | ||
}, | ||
"include": [ | ||
"src" | ||
], | ||
"references": [ | ||
{ | ||
"path": "../../packages/core/tsconfig.json" | ||
}, | ||
{ | ||
"path": "../../packages/express/tsconfig.json" | ||
}, | ||
{ | ||
"path": "../../packages/rest/tsconfig.json" | ||
}, | ||
{ | ||
"path": "../../packages/testlab/tsconfig.json" | ||
} | ||
] | ||
} |
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