Skip to content

Commit

Permalink
[frontend] reunite trace from loadgenerator (open-telemetry#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
puckpuck authored and AlexPSplunk committed Jul 10, 2024
1 parent c4d221f commit 1310c46
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 3 additions & 34 deletions src/frontend/utils/telemetry/InstrumentationMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}
};
};
Expand Down

0 comments on commit 1310c46

Please sign in to comment.