UNPKG

solarwinds-apm

Version:

OpenTelemetry-based SolarWinds APM library

66 lines (63 loc) 2.71 kB
/* Copyright 2023-2025 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. */ var _a; import { metrics, SpanKind, SpanStatusCode, ValueType, } from "@opentelemetry/api"; import { hrTimeToMilliseconds } from "@opentelemetry/core"; import { NoopSpanProcessor, } from "@opentelemetry/sdk-trace-base"; import { ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, } from "@opentelemetry/semantic-conventions"; import { ATTR_HTTP_METHOD, ATTR_HTTP_STATUS_CODE } from "../semattrs.old.js"; import { componentLogger } from "../shared/logger.js"; import { isRootOrEntry } from "./parent-span.js"; import { TRANSACTION_NAME_ATTRIBUTE } from "./transaction-name.js"; /** * Processor that records response time metrics * * This should be registered after the transaction name processor * as it depends on the final transaction name being set on the span * for the recorded metrics to be correlated with it. */ export class ResponseTimeProcessor extends NoopSpanProcessor { #logger = componentLogger(_a); #responseTime = metrics .getMeter("sw.apm.request.metrics") .createHistogram("trace.service.response_time", { valueType: ValueType.DOUBLE, unit: "ms", }); onEnd(span) { if (!isRootOrEntry(span)) { return; } const time = hrTimeToMilliseconds(span.duration); const attributes = { "sw.is_error": span.status.code === SpanStatusCode.ERROR, }; const copy = [TRANSACTION_NAME_ATTRIBUTE]; if (span.kind === SpanKind.SERVER) { copy.push(ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_RESPONSE_STATUS_CODE, // eslint-disable-next-line @typescript-eslint/no-deprecated ATTR_HTTP_METHOD, // eslint-disable-next-line @typescript-eslint/no-deprecated ATTR_HTTP_STATUS_CODE); } for (const a of copy) { if (a in span.attributes) { attributes[a] = span.attributes[a]; } } this.#logger.debug("recording response time", time, attributes); this.#responseTime.record(time, attributes); } } _a = ResponseTimeProcessor; //# sourceMappingURL=response-time.js.map