Skip to content

Commit

Permalink
refactor semantic attributes imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed May 27, 2024
1 parent 86ef50c commit 618518e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
20 changes: 13 additions & 7 deletions packages/otel/src/get-attributes-from-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { NextApiRequest } from "next";
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
import {
SEMATTRS_FAAS_EXECUTION,
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_TARGET,
SEMATTRS_HTTP_USER_AGENT,
SEMATTRS_NET_HOST_NAME,
} from "@opentelemetry/semantic-conventions";
import { SALEOR_API_URL_HEADER } from "@saleor/app-sdk/const";

const pruneEmptyKeys = (obj: Record<string, unknown>): Record<string, string> => {
Expand All @@ -18,16 +24,16 @@ const pruneEmptyKeys = (obj: Record<string, unknown>): Record<string, string> =>

export const getAttributesFromRequest = (request: NextApiRequest) => {
const attributes = {
[SemanticAttributes.FAAS_EXECUTION]: request.headers["x-vercel-proxy-signature-ts"] as string,
[SemanticAttributes.HTTP_USER_AGENT]: request.headers["user-agent"] as string,
[SemanticAttributes.HTTP_TARGET]: request.headers.referer as string,
[SemanticAttributes.NET_HOST_NAME]: request.headers.host as string,
[SemanticAttributes.HTTP_METHOD]: (request.method ?? "").toUpperCase(),
[SEMATTRS_FAAS_EXECUTION]: request.headers["x-vercel-proxy-signature-ts"] as string,
[SEMATTRS_HTTP_USER_AGENT]: request.headers["user-agent"] as string,
[SEMATTRS_HTTP_TARGET]: request.headers.referer as string,
[SEMATTRS_NET_HOST_NAME]: request.headers.host as string,
[SEMATTRS_HTTP_METHOD]: (request.method ?? "").toUpperCase(),
saleorApiUrl: request.headers[SALEOR_API_URL_HEADER] as string,
"url.path": request.url,
vercelRequestId: request.headers["x-vercel-id"],
requestId: request.headers["x-vercel-proxy-signature-ts"] as string,
} as const ;
} as const;

return pruneEmptyKeys(attributes);
};
19 changes: 11 additions & 8 deletions packages/otel/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { Resource } from "@opentelemetry/resources";
import { NodeSDK } from "@opentelemetry/sdk-node";
import {
SemanticAttributes,
SemanticResourceAttributes,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_STATUS_CODE,
SEMATTRS_HTTP_HOST,
} from "@opentelemetry/semantic-conventions";
import { type ClientRequest } from "node:http";
import { otelLogsProcessor } from "./otel-logs-setup";
Expand All @@ -18,14 +21,14 @@ if (process.env.ENABLE_DEBUG_OTEL_DIAG === "true") {
export const otelSdk = new NodeSDK({
resource: new Resource({
// eslint-disable-next-line turbo/no-undeclared-env-vars
[SemanticResourceAttributes.SERVICE_NAME]: process.env.OTEL_SERVICE_NAME,
[SEMRESATTRS_SERVICE_NAME]: process.env.OTEL_SERVICE_NAME,
// [SemanticResourceAttributes.SERVICE_VERSION]: pkg.version, TODO
// eslint-disable-next-line turbo/no-undeclared-env-vars
"commit-sha": process.env.VERCEL_GIT_COMMIT_SHA,
// eslint-disable-next-line turbo/no-undeclared-env-vars
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.ENV,
[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT]: process.env.ENV,
}),
spanProcessor: batchSpanProcessor,
spanProcessors: [batchSpanProcessor],
logRecordProcessor: otelLogsProcessor,
textMapPropagator: new W3CTraceContextPropagator(),
instrumentations: [
Expand All @@ -37,11 +40,11 @@ export const otelSdk = new NodeSDK({
* TODO Fix this.
*/
applyCustomAttributesOnSpan: (span, req, response) => {
span.setAttribute(SemanticAttributes.HTTP_ROUTE, (req as ClientRequest)?.path);
span.setAttribute(SemanticAttributes.HTTP_HOST, (req as ClientRequest)?.host);
span.setAttribute(SEMATTRS_HTTP_ROUTE, (req as ClientRequest)?.path);
span.setAttribute(SEMATTRS_HTTP_HOST, (req as ClientRequest)?.host);

if (response.statusCode) {
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.statusCode);
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, response.statusCode);
}

if (response.statusCode && response.statusCode >= 400) {
Expand Down
4 changes: 2 additions & 2 deletions packages/otel/src/otel-exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type CombinedError, type Operation, makeOperation, mapExchange } from "
import { getOtelTracer } from "./otel-tracer";
import { GraphQLAttributeNames, ObservabilityAttributes } from "./lib/observability-attributes";
import { addInputVariableAttributes, addRequestHeaderAttributes } from "./otel-graphql-utils";
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
import { SEMATTRS_HTTP_URL } from "@opentelemetry/semantic-conventions";

type Definition = {
name: {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const otelExchange = mapExchange({

span.setAttribute(ObservabilityAttributes.SALEOR_API_URL, operation.context.url);

span.setAttribute(SemanticAttributes.HTTP_URL, operation.context.url);
span.setAttribute(SEMATTRS_HTTP_URL, operation.context.url);

addRequestHeaderAttributes(span, operation.context.fetchOptions?.headers);
if (operation.variables) {
Expand Down
14 changes: 9 additions & 5 deletions packages/otel/src/otel-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SpanKind, SpanStatusCode, type Span } from "@opentelemetry/api";
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
import {
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_STATUS_CODE,
} from "@opentelemetry/semantic-conventions";
import { type NextApiHandler, type NextApiRequest, type NextApiResponse } from "next";
import { otelSdk } from "./instrumentation";

Expand Down Expand Up @@ -63,13 +67,13 @@ export const withOtel = (handler: NextApiHandler, staticRouteName: string): Next
const attributesFromRequest = getAttributesFromRequest(req);

return tracer.startActiveSpan(
`${attributesFromRequest[SemanticAttributes.HTTP_METHOD]} ${staticRouteName}`,
`${attributesFromRequest[SEMATTRS_HTTP_METHOD]} ${staticRouteName}`,
{
kind: SpanKind.SERVER,
attributes: attributesFromRequest,
},
async (span) => {
span.setAttribute(SemanticAttributes.HTTP_ROUTE, staticRouteName);
span.setAttribute(SEMATTRS_HTTP_ROUTE, staticRouteName);

const originalResEnd = res.end;

Expand All @@ -78,7 +82,7 @@ export const withOtel = (handler: NextApiHandler, staticRouteName: string): Next
*/
// @ts-expect-error - this is a hack to get around Vercel freezing lambda's
res.end = async function (this: unknown, ...args: unknown[]) {
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, res.statusCode);
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, res.statusCode);
span.end();

try {
Expand All @@ -97,7 +101,7 @@ export const withOtel = (handler: NextApiHandler, staticRouteName: string): Next

wrappingTarget.apply(thisArg, [req, res]);
} catch (error) {
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, 500);
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, 500);

setErrorOnSpan(error, span);

Expand Down

0 comments on commit 618518e

Please sign in to comment.