Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] reunite trace from loadgenerator #1506

Merged
merged 7 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:/open-telemetry/opentelemetry-demo/pull/1501))
* [frontend] reunite trace from loadgenerator
([#1506](https:/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
Loading