From 126f0e8af1f1237729e5b3a203b5f7e3885b24b9 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Sat, 26 Nov 2022 09:27:45 +0100 Subject: [PATCH] [frontend] Format InstrumentationMiddleware.ts (#607) --- .../telemetry/InstrumentationMiddleware.ts | 101 +++++++++--------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/src/frontend/utils/telemetry/InstrumentationMiddleware.ts b/src/frontend/utils/telemetry/InstrumentationMiddleware.ts index 88f040ea53..460a22254d 100644 --- a/src/frontend/utils/telemetry/InstrumentationMiddleware.ts +++ b/src/frontend/utils/telemetry/InstrumentationMiddleware.ts @@ -1,62 +1,61 @@ -import {NextApiHandler} from 'next'; -import {context, Exception, propagation, Span, SpanKind, SpanStatusCode, trace} from '@opentelemetry/api'; -import {SemanticAttributes} from '@opentelemetry/semantic-conventions'; +import { NextApiHandler } from 'next'; +import { context, Exception, propagation, Span, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'; +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => { - return async (request, response) => { - const {headers, method, url = '', httpVersion} = request; - const [target] = url.split('?'); - - 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; - const tracer = trace.getTracer(process.env.OTEL_SERVICE_NAME as string); - span = tracer.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_METHOD]: method, - [SemanticAttributes.HTTP_USER_AGENT]: headers['user-agent'] || '', - [SemanticAttributes.HTTP_URL]: `${headers.host}${url}`, - [SemanticAttributes.HTTP_FLAVOR]: httpVersion, - } - }); - - } else { - // continue current trace/span - span = trace.getSpan(context.active()) as Span; - } + return async (request, response) => { + const { headers, method, url = '', httpVersion } = request; + const [target] = url.split('?'); + + 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; + const tracer = trace.getTracer(process.env.OTEL_SERVICE_NAME as string); + span = tracer.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_METHOD]: method, + [SemanticAttributes.HTTP_USER_AGENT]: headers['user-agent'] || '', + [SemanticAttributes.HTTP_URL]: `${headers.host}${url}`, + [SemanticAttributes.HTTP_FLAVOR]: httpVersion, + }, + }); + } else { + // continue current trace/span + span = trace.getSpan(context.active()) as Span; + } - try { - await runWithSpan(span, async () => handler(request, response)); - } catch (error) { - span.recordException(error as Exception); - span.setStatus({code: SpanStatusCode.ERROR}); - throw error; - } finally { - span.end(); - } - }; + try { + await runWithSpan(span, async () => handler(request, response)); + } catch (error) { + span.recordException(error as Exception); + span.setStatus({ code: SpanStatusCode.ERROR }); + throw error; + } finally { + span.end(); + } + }; }; async function runWithSpan(parentSpan: Span, fn: () => Promise) { - const ctx = trace.setSpan(context.active(), parentSpan); + const ctx = trace.setSpan(context.active(), parentSpan); - try { - return await context.with(ctx, fn); - } catch (error) { - parentSpan.recordException(error as Exception); - parentSpan.setStatus({ code: SpanStatusCode.ERROR }); + try { + return await context.with(ctx, fn); + } catch (error) { + parentSpan.recordException(error as Exception); + parentSpan.setStatus({ code: SpanStatusCode.ERROR }); - throw error; - } + throw error; + } } export default InstrumentationMiddleware;