Skip to content

Commit

Permalink
NH-89872 - update semconv
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Aug 30, 2024
1 parent 4c4e826 commit 35ec3c4
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 41 deletions.
12 changes: 9 additions & 3 deletions packages/solarwinds-apm/src/processing/response-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import {
type SpanProcessor,
} from "@opentelemetry/sdk-trace-base"
import {
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_STATUS_CODE,
ATTR_HTTP_REQUEST_METHOD,
ATTR_HTTP_RESPONSE_STATUS_CODE,
} from "@opentelemetry/semantic-conventions"
import { lazy } from "@solarwinds-apm/lazy"

import { ATTR_HTTP_METHOD, ATTR_HTTP_STATUS_CODE } from "../semattrs.old.js"
import { isRootOrEntry } from "./parent-span.js"
import { TRANSACTION_NAME_ATTRIBUTE } from "./transaction-name.js"

Expand Down Expand Up @@ -73,7 +74,12 @@ export class ResponseTimeProcessor

const copy = [TRANSACTION_NAME_ATTRIBUTE]
if (span.kind === SpanKind.SERVER) {
copy.push(SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_STATUS_CODE)
copy.push(
ATTR_HTTP_REQUEST_METHOD,
ATTR_HTTP_RESPONSE_STATUS_CODE,
ATTR_HTTP_METHOD,
ATTR_HTTP_STATUS_CODE,
)
}
for (const a of copy) {
if (a in span.attributes) {
Expand Down
15 changes: 9 additions & 6 deletions packages/solarwinds-apm/src/processing/transaction-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import {
type SpanProcessor,
} from "@opentelemetry/sdk-trace-base"
import {
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_TARGET,
ATTR_HTTP_ROUTE,
ATTR_URL_PATH,
} from "@opentelemetry/semantic-conventions"
import { type SwConfiguration } from "@solarwinds-apm/sdk"

import { ATTR_HTTP_TARGET } from "../semattrs.old.js"
import { getRootOrEntry, isRootOrEntry } from "./parent-span.js"

export const TRANSACTION_NAME_ATTRIBUTE = "sw.transaction"
Expand Down Expand Up @@ -98,12 +99,14 @@ export class TransactionNameProcessor
export function computedTransactionName(span: ReadableSpan): string {
if (typeof process.env.AWS_LAMBDA_FUNCTION_NAME === "string") {
return process.env.AWS_LAMBDA_FUNCTION_NAME
} else if (typeof span.attributes[SEMATTRS_HTTP_ROUTE] === "string") {
return span.attributes[SEMATTRS_HTTP_ROUTE]
} else if (typeof span.attributes[SEMATTRS_HTTP_TARGET] === "string") {
} else if (typeof span.attributes[ATTR_HTTP_ROUTE] === "string") {
return span.attributes[ATTR_HTTP_ROUTE]
} else if (typeof span.attributes[ATTR_URL_PATH] === "string") {
// split on slashes and keep the first 3 segments
// where the first segment is an empty string before the first slash
return span.attributes[SEMATTRS_HTTP_TARGET].split("/", 3).join("/")
return span.attributes[ATTR_URL_PATH].split("/", 3).join("/")
} else if (typeof span.attributes[ATTR_HTTP_TARGET] === "string") {
return span.attributes[ATTR_HTTP_TARGET].split("/", 3).join("/")
} else {
return span.name
}
Expand Down
23 changes: 17 additions & 6 deletions packages/solarwinds-apm/src/sampling/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
SpanKind,
} from "@opentelemetry/api"
import {
SEMATTRS_HTTP_SCHEME,
SEMATTRS_HTTP_TARGET,
SEMATTRS_NET_HOST_NAME,
ATTR_SERVER_ADDRESS,
ATTR_URL_PATH,
ATTR_URL_SCHEME,
} from "@opentelemetry/semantic-conventions"
import {
type LocalSettings,
Expand All @@ -35,6 +35,11 @@ import {
import { type SwConfiguration } from "@solarwinds-apm/sdk"

import { HEADERS_STORAGE } from "../propagation/headers.js"
import {
ATTR_HTTP_SCHEME,
ATTR_HTTP_TARGET,
ATTR_NET_HOST_NAME,
} from "../semattrs.old.js"

/**
* Abstract core sampler to extend from other samplers
Expand Down Expand Up @@ -80,9 +85,15 @@ export abstract class Sampler extends OboeSampler {

const kind = SpanKind[spanKind]

const scheme = attributes[SEMATTRS_HTTP_SCHEME]?.toString()
const address = attributes[SEMATTRS_NET_HOST_NAME]?.toString()
const path = attributes[SEMATTRS_HTTP_TARGET]?.toString()
const scheme = (
attributes[ATTR_URL_SCHEME] ?? attributes[ATTR_HTTP_SCHEME]
)?.toString()
const address = (
attributes[ATTR_SERVER_ADDRESS] ?? attributes[ATTR_NET_HOST_NAME]
)?.toString()
const path = (
attributes[ATTR_URL_PATH] ?? attributes[ATTR_HTTP_TARGET]
)?.toString()

let identifier: string
if (scheme && address && path) {
Expand Down
23 changes: 23 additions & 0 deletions packages/solarwinds-apm/src/semattrs.old.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2023-2024 SolarWinds Worldwide, LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

export {
SEMATTRS_HTTP_METHOD as ATTR_HTTP_METHOD,
SEMATTRS_HTTP_SCHEME as ATTR_HTTP_SCHEME,
SEMATTRS_HTTP_STATUS_CODE as ATTR_HTTP_STATUS_CODE,
SEMATTRS_HTTP_TARGET as ATTR_HTTP_TARGET,
SEMATTRS_NET_HOST_NAME as ATTR_NET_HOST_NAME,
} from "@opentelemetry/semantic-conventions"
61 changes: 49 additions & 12 deletions packages/solarwinds-apm/test/processing/response-time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ import {
type HistogramMetricData,
} from "@opentelemetry/sdk-metrics"
import {
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_STATUS_CODE,
SEMATTRS_HTTP_TARGET,
ATTR_HTTP_REQUEST_METHOD,
ATTR_HTTP_RESPONSE_STATUS_CODE,
ATTR_HTTP_ROUTE,
ATTR_URL_PATH,
} from "@opentelemetry/semantic-conventions"
import { type SwConfiguration } from "@solarwinds-apm/sdk"
import { describe, expect, it, otel } from "@solarwinds-apm/test"

import { ParentSpanProcessor } from "../../src/processing/parent-span.js"
import { ResponseTimeProcessor } from "../../src/processing/response-time.js"
import { TransactionNameProcessor } from "../../src/processing/transaction-name.js"
import {
ATTR_HTTP_METHOD,
ATTR_HTTP_STATUS_CODE,
ATTR_HTTP_TARGET,
} from "../../src/semattrs.old.js"

const responseTime = async () => {
const metrics = await otel.metrics()
Expand Down Expand Up @@ -78,17 +83,49 @@ describe("ResponseTimeProcessor", () => {
{
kind: SpanKind.SERVER,
attributes: {
[SEMATTRS_HTTP_METHOD]: "GET",
[SEMATTRS_HTTP_ROUTE]: "/hello/:name",
[SEMATTRS_HTTP_TARGET]: "/hello/world",
[ATTR_HTTP_REQUEST_METHOD]: "GET",
[ATTR_HTTP_ROUTE]: "/hello/:name",
[ATTR_URL_PATH]: "/hello/world",
},
},
(span) => {
tracer.startActiveSpan("operation", (span) => {
span.end()
})

span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, 200)
span.end()
},
)

const { value, attributes } = await responseTime()
expect(attributes).to.deep.equal({
"sw.is_error": false,
"sw.transaction": "/hello/:name",
[ATTR_HTTP_REQUEST_METHOD]: "GET",
[ATTR_HTTP_RESPONSE_STATUS_CODE]: 200,
})
expect(value).to.be.greaterThan(0)
})

it("records response time for deprecated server spans", async () => {
const tracer = trace.getTracer("test")
tracer.startActiveSpan(
"GET /hello/:name",
{
kind: SpanKind.SERVER,
attributes: {
[ATTR_HTTP_METHOD]: "GET",
[ATTR_HTTP_ROUTE]: "/hello/:name",
[ATTR_HTTP_TARGET]: "/hello/world",
},
},
(span) => {
tracer.startActiveSpan("operation", (span) => {
span.end()
})

span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, 200)
span.setAttribute(ATTR_HTTP_STATUS_CODE, 200)
span.end()
},
)
Expand All @@ -97,8 +134,8 @@ describe("ResponseTimeProcessor", () => {
expect(attributes).to.deep.equal({
"sw.is_error": false,
"sw.transaction": "/hello/:name",
[SEMATTRS_HTTP_METHOD]: "GET",
[SEMATTRS_HTTP_STATUS_CODE]: 200,
[ATTR_HTTP_METHOD]: "GET",
[ATTR_HTTP_STATUS_CODE]: 200,
})
expect(value).to.be.greaterThan(0)
})
Expand All @@ -109,11 +146,11 @@ describe("ResponseTimeProcessor", () => {
{
kind: SpanKind.CLIENT,
attributes: {
[SEMATTRS_HTTP_METHOD]: "GET",
[ATTR_HTTP_REQUEST_METHOD]: "GET",
},
},
(span) => {
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, 404)
span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, 404)
span.setStatus({ code: SpanStatusCode.ERROR })
span.end()
},
Expand Down
47 changes: 39 additions & 8 deletions packages/solarwinds-apm/test/processing/transaction-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { setTimeout } from "node:timers/promises"
import { diag, trace } from "@opentelemetry/api"
import type * as sdk from "@opentelemetry/sdk-trace-base"
import {
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_TARGET,
ATTR_HTTP_ROUTE,
ATTR_URL_PATH,
} from "@opentelemetry/semantic-conventions"
import { type SwConfiguration } from "@solarwinds-apm/sdk"
import { describe, expect, it, otel } from "@solarwinds-apm/test"
Expand All @@ -32,6 +32,7 @@ import {
TransactionNamePool,
TransactionNameProcessor,
} from "../../src/processing/transaction-name.js"
import { ATTR_HTTP_TARGET } from "../../src/semattrs.old.js"

describe("TransactionNameProcessor", () => {
it("sets transaction name on entry spans", async () => {
Expand Down Expand Up @@ -217,8 +218,20 @@ describe("computedTransactionName", () => {
it("computes routed HTTP span name", () => {
const span = trace.getTracer("test").startSpan("GET", {
attributes: {
[SEMATTRS_HTTP_ROUTE]: "/hello/:name",
[SEMATTRS_HTTP_TARGET]: "/hello/world",
[ATTR_HTTP_ROUTE]: "/hello/:name",
[ATTR_URL_PATH]: "/hello/world",
},
}) as sdk.Span
span.end()

expect(computedTransactionName(span)).to.equal("/hello/:name")
})

it("computes deprecated routed HTTP span name", () => {
const span = trace.getTracer("test").startSpan("GET", {
attributes: {
[ATTR_HTTP_ROUTE]: "/hello/:name",
[ATTR_HTTP_TARGET]: "/hello/world",
},
}) as sdk.Span
span.end()
Expand All @@ -228,7 +241,16 @@ describe("computedTransactionName", () => {

it("computes short HTTP span name", () => {
const span = trace.getTracer("test").startSpan("GET", {
attributes: { [SEMATTRS_HTTP_TARGET]: "/cart" },
attributes: { [ATTR_URL_PATH]: "/cart" },
}) as sdk.Span
span.end()

expect(computedTransactionName(span)).to.equal("/cart")
})

it("computes deprecated short HTTP span name", () => {
const span = trace.getTracer("test").startSpan("GET", {
attributes: { [ATTR_HTTP_TARGET]: "/cart" },
}) as sdk.Span
span.end()

Expand All @@ -237,7 +259,16 @@ describe("computedTransactionName", () => {

it("computes long HTTP span name", () => {
const span = trace.getTracer("test").startSpan("GET", {
attributes: { [SEMATTRS_HTTP_TARGET]: "/shop/products/293/detail" },
attributes: { [ATTR_URL_PATH]: "/shop/products/293/detail" },
}) as sdk.Span
span.end()

expect(computedTransactionName(span)).to.equal("/shop/products")
})

it("computes deprecated long HTTP span name", () => {
const span = trace.getTracer("test").startSpan("GET", {
attributes: { [ATTR_HTTP_TARGET]: "/shop/products/293/detail" },
}) as sdk.Span
span.end()

Expand All @@ -250,8 +281,8 @@ describe("computedTransactionName", () => {

const span = trace.getTracer("test").startSpan("GET", {
attributes: {
[SEMATTRS_HTTP_ROUTE]: "/hello/:name",
[SEMATTRS_HTTP_TARGET]: "/hello/world",
[ATTR_HTTP_ROUTE]: "/hello/:name",
[ATTR_URL_PATH]: "/hello/world",
},
}) as sdk.Span
span.end()
Expand Down
Loading

0 comments on commit 35ec3c4

Please sign in to comment.