From 86474f827f6c67b22e213310d5db04db76f2c9c8 Mon Sep 17 00:00:00 2001 From: sjvans <30337871+sjvans@users.noreply.github.com> Date: Thu, 28 Nov 2024 23:26:50 +0100 Subject: [PATCH] fix: use url.path || http.target for sampling decision (#258) closes https://github.com/cap-js/telemetry/issues/255 --- CHANGELOG.md | 6 ++++++ lib/tracing/index.js | 6 +++--- lib/tracing/trace.js | 8 +++++++- package.json | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d7fe9b..ad5a3be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). The format is based on [Keep a Changelog](http://keepachangelog.com/). +## Version 1.1.1 - 2024-11-28 + +### Fixed + +- Use attribute `url.path` (with fallback to deprecated `http.target`) for sampling decision + ## Version 1.1.0 - 2024-11-27 ### Added diff --git a/lib/tracing/index.js b/lib/tracing/index.js index 313667b..053d189 100644 --- a/lib/tracing/index.js +++ b/lib/tracing/index.js @@ -24,9 +24,9 @@ function _getSampler() { else { // eslint-disable-next-line no-unused-vars _shouldSample = (_context, _traceId, _name, _spanKind, attributes, _links) => { - const originalUrl = attributes?.['http.originalUrl'] - if (!originalUrl) return true - return !ignoreIncomingPaths.some(p => originalUrl.startsWith(p)) + const url_path = attributes?.['url.path'] || attributes?.['http.target'] //> http.target is deprecated + if (!url_path) return true + return !ignoreIncomingPaths.some(p => url_path.startsWith(p)) } } diff --git a/lib/tracing/trace.js b/lib/tracing/trace.js index a449a05..f9296c2 100644 --- a/lib/tracing/trace.js +++ b/lib/tracing/trace.js @@ -203,7 +203,13 @@ function trace(name, fn, targetObj, args, options = {}) { kind: _determineKind(targetObj, name?.phase, isAsync, options) } // needed for sampling decision (cf. shouldSample) - if (cds.context?.http?.req) spanOptions.attributes = { 'http.originalUrl': cds.context.http?.req.originalUrl } + if (cds.context?.http?.req) { + const url_path = cds.context.http.req.baseUrl + cds.context.http.req.path + spanOptions.attributes = { + 'url.path': url_path, + 'http.target': url_path //> http.target is deprecated + } + } if (HRTIME) spanOptions.startTime = _hrnow() if (isAsync) { spanOptions.links = [{ context: parentSpan.spanContext() }] diff --git a/package.json b/package.json index 794adc7..c27e967 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cap-js/telemetry", - "version": "1.1.0", + "version": "1.1.1", "description": "CDS plugin providing observability features, incl. automatic OpenTelemetry instrumentation.", "repository": { "type": "git",