-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend] - add span links on synthetic requests (#332)
* add span links * add span links to frontend
- Loading branch information
Showing
5 changed files
with
48 additions
and
46 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
72 changes: 42 additions & 30 deletions
72
src/frontend/utils/telemetry/InstrumentationMiddleware.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,39 +1,51 @@ | ||
import { NextApiHandler } from 'next'; | ||
import {NextApiHandler} from 'next'; | ||
import Tracer from './BackendTracer'; | ||
import { context, propagation, SpanKind, SpanStatusCode, Exception } from '@opentelemetry/api'; | ||
import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; | ||
import {context, Exception, propagation, SpanKind, SpanStatusCode, trace} from '@opentelemetry/api'; | ||
import {SemanticAttributes} from '@opentelemetry/semantic-conventions'; | ||
import {Span} from '@opentelemetry/sdk-trace-base'; | ||
|
||
const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => { | ||
const wrapper: NextApiHandler = async (request, response) => { | ||
const { headers, method, url = '', httpVersion } = request; | ||
const [target] = url.split('?'); | ||
return async (request, response) => { | ||
const {headers, method, url = '', httpVersion} = request; | ||
const [target] = url.split('?'); | ||
|
||
const parentContext = propagation.extract(context.active(), headers); | ||
const span = await Tracer.createSpanFromContext(`API HTTP ${method}`, parentContext, { kind: SpanKind.SERVER }); | ||
let span; | ||
const baggage = propagation.getBaggage(context.active()); | ||
if (baggage?.getEntry("synthetic_request")?.value == "true") { | ||
// if synthetic_request baggage is set, create a new trace linked to the span in context | ||
// this span will look similar to the auto-instrumented HTTP span | ||
const syntheticSpan = trace.getSpan(context.active()) as Span; | ||
span = Tracer.getTracer().startSpan(`HTTP ${method}`, { | ||
root: true, | ||
kind: SpanKind.SERVER, | ||
links: [{context: syntheticSpan.spanContext()}], | ||
attributes: { | ||
"app.synthetic_request": true, | ||
[SemanticAttributes.HTTP_TARGET]: target, | ||
[SemanticAttributes.HTTP_STATUS_CODE]: response.statusCode, | ||
[SemanticAttributes.HTTP_ROUTE]: url, | ||
[SemanticAttributes.HTTP_METHOD]: method, | ||
[SemanticAttributes.HTTP_USER_AGENT]: headers['user-agent'] || '', | ||
[SemanticAttributes.HTTP_URL]: `${headers.host}${url}`, | ||
[SemanticAttributes.HTTP_FLAVOR]: httpVersion, | ||
} | ||
}); | ||
|
||
try { | ||
await Tracer.runWithSpan(span, async () => handler(request, response)); | ||
} catch (error) { | ||
span.recordException(error as Exception); | ||
span.setStatus({ code: SpanStatusCode.ERROR }); | ||
} else { | ||
// continue current trace/span | ||
span = trace.getSpan(context.active()) as Span; | ||
} | ||
|
||
throw error; | ||
} finally { | ||
span.setAttributes({ | ||
[SemanticAttributes.HTTP_TARGET]: target, | ||
[SemanticAttributes.HTTP_STATUS_CODE]: response.statusCode, | ||
[SemanticAttributes.HTTP_ROUTE]: url, | ||
[SemanticAttributes.HTTP_METHOD]: method, | ||
[SemanticAttributes.HTTP_USER_AGENT]: headers['user-agent'] || '', | ||
[SemanticAttributes.HTTP_URL]: `${headers.host}${url}`, | ||
[SemanticAttributes.HTTP_FLAVOR]: httpVersion, | ||
}); | ||
|
||
span.end(); | ||
} | ||
}; | ||
|
||
return wrapper; | ||
try { | ||
await Tracer.runWithSpan(span, async () => handler(request, response)); | ||
} catch (error) { | ||
span.recordException(error as Exception); | ||
span.setStatus({code: SpanStatusCode.ERROR}); | ||
throw error; | ||
} finally { | ||
span.end(); | ||
} | ||
}; | ||
}; | ||
|
||
export default InstrumentationMiddleware; |