diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d437a8704..bb45909690 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ the release. * [frauddetectionservice] use span links when consuming from Kafka ([#1501](https://github.com/open-telemetry/opentelemetry-demo/pull/1501)) +* [frontend] reunite trace from loadgenerator + ([#1506](https://github.com/open-telemetry/opentelemetry-demo/pull/1506)) ## 1.9.0 diff --git a/src/frontend/utils/telemetry/InstrumentationMiddleware.ts b/src/frontend/utils/telemetry/InstrumentationMiddleware.ts index 1c1fde12c5..ed389aff8d 100644 --- a/src/frontend/utils/telemetry/InstrumentationMiddleware.ts +++ b/src/frontend/utils/telemetry/InstrumentationMiddleware.ts @@ -2,47 +2,19 @@ // SPDX-License-Identifier: Apache-2.0 import { NextApiHandler } from 'next'; -import { context, Exception, propagation, Span, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'; +import {context, Exception, Span, SpanStatusCode, trace} from '@opentelemetry/api'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { metrics } from '@opentelemetry/api'; -import { AttributeNames } from '../enums/AttributeNames'; const meter = metrics.getMeter('frontend'); const requestCounter = meter.createCounter('app.frontend.requests'); const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => { return async (request, response) => { - const { headers, method, url = '', httpVersion } = request; + const {method, url = ''} = 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_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; - } - - if (request.query['sessionId'] != null) { - span.setAttribute(AttributeNames.SESSION_ID, request.query['sessionId']); - } + const span = trace.getSpan(context.active()) as Span; let httpStatus = 200; try { @@ -56,9 +28,6 @@ const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => { } finally { requestCounter.add(1, { method, target, status: httpStatus }); span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, httpStatus); - if (baggage?.getEntry('synthetic_request')?.value == 'true') { - span.end(); - } } }; };