Skip to content

Commit 2596ca0

Browse files
authored
[frontend] reunite trace from loadgenerator (#1506)
1 parent 1e992ba commit 2596ca0

File tree

2 files changed

+5
-34
lines changed

2 files changed

+5
-34
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ the release.
99

1010
* [frauddetectionservice] use span links when consuming from Kafka
1111
([#1501](https://github.com/open-telemetry/opentelemetry-demo/pull/1501))
12+
* [frontend] reunite trace from loadgenerator
13+
([#1506](https://github.com/open-telemetry/opentelemetry-demo/pull/1506))
1214

1315
## 1.9.0
1416

src/frontend/utils/telemetry/InstrumentationMiddleware.ts

+3-34
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,19 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { NextApiHandler } from 'next';
5-
import { context, Exception, propagation, Span, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api';
5+
import {context, Exception, Span, SpanStatusCode, trace} from '@opentelemetry/api';
66
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
77
import { metrics } from '@opentelemetry/api';
8-
import { AttributeNames } from '../enums/AttributeNames';
98

109
const meter = metrics.getMeter('frontend');
1110
const requestCounter = meter.createCounter('app.frontend.requests');
1211

1312
const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => {
1413
return async (request, response) => {
15-
const { headers, method, url = '', httpVersion } = request;
14+
const {method, url = ''} = request;
1615
const [target] = url.split('?');
1716

18-
let span;
19-
const baggage = propagation.getBaggage(context.active());
20-
if (baggage?.getEntry('synthetic_request')?.value == 'true') {
21-
// if synthetic_request baggage is set, create a new trace linked to the span in context
22-
// this span will look similar to the auto-instrumented HTTP span
23-
const syntheticSpan = trace.getSpan(context.active()) as Span;
24-
const tracer = trace.getTracer(process.env.OTEL_SERVICE_NAME as string);
25-
span = tracer.startSpan(`HTTP ${method}`, {
26-
root: true,
27-
kind: SpanKind.SERVER,
28-
links: [{ context: syntheticSpan.spanContext() }],
29-
attributes: {
30-
'app.synthetic_request': true,
31-
[SemanticAttributes.HTTP_TARGET]: target,
32-
[SemanticAttributes.HTTP_METHOD]: method,
33-
[SemanticAttributes.HTTP_USER_AGENT]: headers['user-agent'] || '',
34-
[SemanticAttributes.HTTP_URL]: `${headers.host}${url}`,
35-
[SemanticAttributes.HTTP_FLAVOR]: httpVersion,
36-
},
37-
});
38-
} else {
39-
// continue current trace/span
40-
span = trace.getSpan(context.active()) as Span;
41-
}
42-
43-
if (request.query['sessionId'] != null) {
44-
span.setAttribute(AttributeNames.SESSION_ID, request.query['sessionId']);
45-
}
17+
const span = trace.getSpan(context.active()) as Span;
4618

4719
let httpStatus = 200;
4820
try {
@@ -56,9 +28,6 @@ const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => {
5628
} finally {
5729
requestCounter.add(1, { method, target, status: httpStatus });
5830
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, httpStatus);
59-
if (baggage?.getEntry('synthetic_request')?.value == 'true') {
60-
span.end();
61-
}
6231
}
6332
};
6433
};

0 commit comments

Comments
 (0)