@platformatic/telemetry 2.51.0 → 2.51.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,45 +30,47 @@ const { name: moduleName, version: moduleVersion } = require('../package.json')
30
30
  // - zipkin (https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-exporter-zipkin/README.md)
31
31
  // - memory: for testing
32
32
 
33
- // This has been partially copied and modified from @autotelic/opentelemetry: https://github.com/autotelic/fastify-opentelemetry/blob/main/index.js
34
- // , according with [MIT license](https://github.com/autotelic/fastify-opentelemetry/blob/main/LICENSE.md):
35
- // MIT License
36
- // Copyright (c) 2020 autotelic
37
- // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
38
- // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
39
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40
- const extractPath = (request) => {
41
- // We must user RouterPath, because otherwise `/test/123` will be considered as
42
- // a different operation than `/test/321`. In case is not set (this should actually happen only for HTTP/404) we fallback to the path.
43
- const { routeOptions, url } = request
44
- let path
45
- const routerPath = routeOptions && routeOptions.url
46
- if (routerPath) {
47
- path = formatParamUrl(routerPath)
48
- } else {
49
- path = url
33
+ const extractRoute = (request) => {
34
+ if (request.routeOptions?.url) {
35
+ return formatParamUrl(request.routeOptions.url)
50
36
  }
51
- return path
37
+ return null
52
38
  }
53
39
 
54
- function formatSpanName (request, path) {
55
- const { method } = request
56
- /* istanbul ignore next */
57
- return path ? `${method} ${path}` : method
40
+ function formatSpanName (request, route) {
41
+ const { method, url } = request
42
+ return `${method} ${route ?? url}`
58
43
  }
59
44
 
60
45
  const formatSpanAttributes = {
61
- request (request, path) {
46
+ request (request, route) {
62
47
  const { hostname, method, url, protocol = 'http' } = request
63
48
  // Inspired by: https://github.com/fastify/fastify-url-data/blob/master/plugin.js#L11
64
- const urlData = fastUri.parse(`${protocol}://${hostname}${url}`)
65
- return {
49
+ const fullUrl = `${protocol}://${hostname}${url}`
50
+ const urlData = fastUri.parse(fullUrl)
51
+
52
+ const attributes = {
66
53
  'server.address': hostname,
67
54
  'server.port': urlData.port,
68
55
  'http.request.method': method,
69
- 'url.path': path,
56
+ 'url.full': fullUrl,
57
+ 'url.path': urlData.path,
70
58
  'url.scheme': protocol,
71
59
  }
60
+
61
+ if (route) {
62
+ attributes['http.route'] = route
63
+ }
64
+
65
+ if (urlData.query) {
66
+ attributes['url.query'] = urlData.query
67
+ }
68
+
69
+ if (urlData.fragment) {
70
+ attributes['url.fragment'] = urlData.fragment
71
+ }
72
+
73
+ return attributes
72
74
  },
73
75
  reply (reply) {
74
76
  return {
@@ -179,12 +181,12 @@ function setupTelemetry (opts, logger) {
179
181
  fastifyTextMapGetter
180
182
  )
181
183
 
182
- const path = extractPath(request)
183
- const span = tracer.startSpan(formatSpanName(request, path), {}, context)
184
+ const route = extractRoute(request)
185
+ const span = tracer.startSpan(formatSpanName(request, route), {}, context)
184
186
  span.kind = SpanKind.SERVER
185
187
  // Next 2 lines are needed by W3CTraceContextPropagator
186
188
  context = context.setSpan(span)
187
- span.setAttributes(formatSpanAttributes.request(request, path))
189
+ span.setAttributes(formatSpanAttributes.request(request, route))
188
190
  span.context = context
189
191
  // Inject the propagation headers
190
192
  propagator.inject(context, reply, fastifyTextMapSetter)
@@ -367,5 +369,5 @@ module.exports = {
367
369
  setupTelemetry,
368
370
  formatSpanName,
369
371
  formatSpanAttributes,
370
- extractPath
372
+ extractRoute
371
373
  }
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { SpanStatusCode, SpanKind } = require('@opentelemetry/api')
4
- const { formatSpanName, formatSpanAttributes, extractPath } = require('./telemetry-config')
4
+ const { formatSpanName, formatSpanAttributes, extractRoute } = require('./telemetry-config')
5
5
  const api = require('@opentelemetry/api')
6
6
  const fastUri = require('fast-uri')
7
7
  const packageJson = require('../package.json')
@@ -12,9 +12,9 @@ const createTelemetryThreadInterceptorHooks = () => {
12
12
  const onServerRequest = (req, cb) => {
13
13
  const activeContext = api.propagation.extract(api.context.active(), req.headers)
14
14
 
15
- const path = extractPath(req)
16
- const span = tracer.startSpan(formatSpanName(req, path), {
17
- attributes: formatSpanAttributes.request(req, path),
15
+ const route = extractRoute(req)
16
+ const span = tracer.startSpan(formatSpanName(req, route), {
17
+ attributes: formatSpanAttributes.request(req, route),
18
18
  kind: SpanKind.SERVER
19
19
  }, activeContext)
20
20
  const ctx = api.trace.setSpan(activeContext, span)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/telemetry",
3
- "version": "2.51.0",
3
+ "version": "2.51.1-alpha.0",
4
4
  "description": "OpenTelemetry integration for Platformatic",
5
5
  "main": "index.js",
6
6
  "author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
@@ -36,7 +36,7 @@
36
36
  "abstract-logging": "^2.0.1",
37
37
  "fast-uri": "^3.0.0",
38
38
  "fastify-plugin": "^5.0.0",
39
- "@platformatic/config": "2.51.0"
39
+ "@platformatic/config": "2.51.1-alpha.0"
40
40
  },
41
41
  "scripts": {
42
42
  "test": "npm run lint && borp --timeout=120000 --concurrency=1",