@platformatic/telemetry 2.0.0-alpha.2 → 2.0.0-alpha.21
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.
- package/eslint.config.js +3 -0
- package/index.js +3 -1
- package/lib/fastify-text-map.js +3 -3
- package/lib/node-http-telemetry.js +33 -0
- package/lib/platformatic-trace-provider.js +4 -4
- package/lib/schema.js +19 -19
- package/lib/telemetry-config.js +81 -0
- package/lib/telemetry.js +51 -101
- package/package.json +13 -10
package/eslint.config.js
ADDED
package/index.js
CHANGED
package/lib/fastify-text-map.js
CHANGED
|
@@ -7,16 +7,16 @@ const fastifyTextMapGetter = {
|
|
|
7
7
|
/* istanbul ignore next */
|
|
8
8
|
keys (request) {
|
|
9
9
|
return Object.keys(request.headers)
|
|
10
|
-
}
|
|
10
|
+
},
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
const fastifyTextMapSetter = {
|
|
14
14
|
set (reply, key, value) {
|
|
15
15
|
reply.headers({ [key]: value })
|
|
16
|
-
}
|
|
16
|
+
},
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
module.exports = {
|
|
20
20
|
fastifyTextMapGetter,
|
|
21
|
-
fastifyTextMapSetter
|
|
21
|
+
fastifyTextMapSetter,
|
|
22
22
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const process = require('node:process')
|
|
3
|
+
const opentelemetry = require('@opentelemetry/sdk-node')
|
|
4
|
+
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http')
|
|
5
|
+
const { Resource } = require('@opentelemetry/resources')
|
|
6
|
+
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions')
|
|
7
|
+
const setupTelemetry = require('./telemetry-config')
|
|
8
|
+
|
|
9
|
+
const setupNodeHTTPTelemetry = (opts, logger) => {
|
|
10
|
+
const { serviceName } = opts
|
|
11
|
+
logger.info(`Setting up Node.js HTTP telemetry for service: ${serviceName}`)
|
|
12
|
+
// We setup the telemetry to init the spanProcessors, then we pass them to the SDK
|
|
13
|
+
const { spanProcessors } = setupTelemetry(opts, logger)
|
|
14
|
+
const sdk = new opentelemetry.NodeSDK({
|
|
15
|
+
spanProcessors, // https://github.com/open-telemetry/opentelemetry-js/issues/4881#issuecomment-2358059714
|
|
16
|
+
instrumentations: [
|
|
17
|
+
new HttpInstrumentation(),
|
|
18
|
+
],
|
|
19
|
+
resource: new Resource({
|
|
20
|
+
[SemanticResourceAttributes.SERVICE_NAME]: serviceName
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
sdk.start()
|
|
24
|
+
|
|
25
|
+
// gracefully shut down the SDK on process exit
|
|
26
|
+
process.on('SIGTERM', () => {
|
|
27
|
+
sdk.shutdown()
|
|
28
|
+
.then(() => console.log('Tracing terminated'))
|
|
29
|
+
.catch((error) => console.log('Error terminating tracing', error))
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = setupNodeHTTPTelemetry
|
|
@@ -16,13 +16,13 @@ class PlatformaticTracerProvider {
|
|
|
16
16
|
const mergedConfig = merge(
|
|
17
17
|
{},
|
|
18
18
|
{
|
|
19
|
-
sampler: new AlwaysOnSampler()
|
|
19
|
+
sampler: new AlwaysOnSampler(),
|
|
20
20
|
},
|
|
21
21
|
config
|
|
22
22
|
)
|
|
23
23
|
this.resource = mergedConfig.resource ?? Resource.empty()
|
|
24
24
|
this._config = Object.assign({}, mergedConfig, {
|
|
25
|
-
resource: this.resource
|
|
25
|
+
resource: this.resource,
|
|
26
26
|
})
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -49,8 +49,8 @@ class PlatformaticTracerProvider {
|
|
|
49
49
|
getPropagator () {
|
|
50
50
|
return new CompositePropagator({
|
|
51
51
|
propagators: [
|
|
52
|
-
new W3CTraceContextPropagator() // see: https://www.w3.org/TR/trace-context/
|
|
53
|
-
]
|
|
52
|
+
new W3CTraceContextPropagator(), // see: https://www.w3.org/TR/trace-context/
|
|
53
|
+
],
|
|
54
54
|
})
|
|
55
55
|
}
|
|
56
56
|
|
package/lib/schema.js
CHANGED
|
@@ -6,7 +6,7 @@ const ExporterSchema = {
|
|
|
6
6
|
type: {
|
|
7
7
|
type: 'string',
|
|
8
8
|
enum: ['console', 'otlp', 'zipkin', 'memory'],
|
|
9
|
-
default: 'console'
|
|
9
|
+
default: 'console',
|
|
10
10
|
},
|
|
11
11
|
options: {
|
|
12
12
|
type: 'object',
|
|
@@ -14,16 +14,16 @@ const ExporterSchema = {
|
|
|
14
14
|
properties: {
|
|
15
15
|
url: {
|
|
16
16
|
type: 'string',
|
|
17
|
-
description: 'The URL to send the traces to. Not used for console or memory exporters.'
|
|
17
|
+
description: 'The URL to send the traces to. Not used for console or memory exporters.',
|
|
18
18
|
},
|
|
19
19
|
headers: {
|
|
20
20
|
type: 'object',
|
|
21
|
-
description: 'Headers to send to the exporter. Not used for console or memory exporters.'
|
|
22
|
-
}
|
|
23
|
-
}
|
|
21
|
+
description: 'Headers to send to the exporter. Not used for console or memory exporters.',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
24
|
},
|
|
25
|
-
additionalProperties: false
|
|
26
|
-
}
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
},
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const TelemetrySchema = {
|
|
@@ -32,11 +32,11 @@ const TelemetrySchema = {
|
|
|
32
32
|
properties: {
|
|
33
33
|
serviceName: {
|
|
34
34
|
type: 'string',
|
|
35
|
-
description: 'The name of the service. Defaults to the folder name if not specified.'
|
|
35
|
+
description: 'The name of the service. Defaults to the folder name if not specified.',
|
|
36
36
|
},
|
|
37
37
|
version: {
|
|
38
38
|
type: 'string',
|
|
39
|
-
description: 'The version of the service (optional)'
|
|
39
|
+
description: 'The version of the service (optional)',
|
|
40
40
|
},
|
|
41
41
|
skip: {
|
|
42
42
|
type: 'array',
|
|
@@ -46,28 +46,28 @@ const TelemetrySchema = {
|
|
|
46
46
|
properties: {
|
|
47
47
|
path: {
|
|
48
48
|
type: 'string',
|
|
49
|
-
description: 'The path to skip. Can be a string or a regex.'
|
|
49
|
+
description: 'The path to skip. Can be a string or a regex.',
|
|
50
50
|
},
|
|
51
51
|
method: {
|
|
52
52
|
description: 'HTTP method to skip',
|
|
53
53
|
type: 'string',
|
|
54
|
-
enum: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
54
|
+
enum: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'],
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
58
|
},
|
|
59
59
|
exporter: {
|
|
60
60
|
anyOf: [
|
|
61
61
|
{
|
|
62
62
|
type: 'array',
|
|
63
|
-
items: ExporterSchema
|
|
63
|
+
items: ExporterSchema,
|
|
64
64
|
},
|
|
65
|
-
ExporterSchema
|
|
66
|
-
]
|
|
67
|
-
}
|
|
65
|
+
ExporterSchema,
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
68
|
},
|
|
69
69
|
required: ['serviceName'],
|
|
70
|
-
additionalProperties: false
|
|
70
|
+
additionalProperties: false,
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
module.exports = TelemetrySchema
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
ConsoleSpanExporter,
|
|
5
|
+
BatchSpanProcessor,
|
|
6
|
+
SimpleSpanProcessor,
|
|
7
|
+
InMemorySpanExporter,
|
|
8
|
+
} = require('@opentelemetry/sdk-trace-base')
|
|
9
|
+
const {
|
|
10
|
+
SemanticResourceAttributes,
|
|
11
|
+
} = require('@opentelemetry/semantic-conventions')
|
|
12
|
+
const { Resource } = require('@opentelemetry/resources')
|
|
13
|
+
const { PlatformaticTracerProvider } = require('./platformatic-trace-provider')
|
|
14
|
+
|
|
15
|
+
const { name: moduleName, version: moduleVersion } = require('../package.json')
|
|
16
|
+
|
|
17
|
+
const setupTelemetry = (opts, logger) => {
|
|
18
|
+
const { serviceName, version } = opts
|
|
19
|
+
let exporter = opts.exporter
|
|
20
|
+
if (!exporter) {
|
|
21
|
+
logger.warn('No exporter configured, defaulting to console.')
|
|
22
|
+
exporter = { type: 'console' }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const exporters = Array.isArray(exporter) ? exporter : [exporter]
|
|
26
|
+
|
|
27
|
+
logger.debug(
|
|
28
|
+
`Setting up telemetry for service: ${serviceName}${version ? ' version: ' + version : ''} with exporter of type ${exporter.type}`
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const provider = new PlatformaticTracerProvider({
|
|
32
|
+
resource: new Resource({
|
|
33
|
+
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
|
|
34
|
+
[SemanticResourceAttributes.SERVICE_VERSION]: version,
|
|
35
|
+
}),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const exporterObjs = []
|
|
39
|
+
const spanProcessors = []
|
|
40
|
+
for (const exporter of exporters) {
|
|
41
|
+
// Exporter config:
|
|
42
|
+
// https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_exporter_zipkin.ExporterConfig.html
|
|
43
|
+
const exporterOptions = { ...exporter.options, serviceName }
|
|
44
|
+
|
|
45
|
+
let exporterObj
|
|
46
|
+
if (exporter.type === 'console') {
|
|
47
|
+
exporterObj = new ConsoleSpanExporter(exporterOptions)
|
|
48
|
+
} else if (exporter.type === 'otlp') {
|
|
49
|
+
// We require here because this require (and only the require!) creates some issue with c8 on some mjs tests on other modules. Since we need an assignemet here, we don't use a switch.
|
|
50
|
+
const {
|
|
51
|
+
OTLPTraceExporter,
|
|
52
|
+
} = require('@opentelemetry/exporter-trace-otlp-proto')
|
|
53
|
+
exporterObj = new OTLPTraceExporter(exporterOptions)
|
|
54
|
+
} else if (exporter.type === 'zipkin') {
|
|
55
|
+
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')
|
|
56
|
+
exporterObj = new ZipkinExporter(exporterOptions)
|
|
57
|
+
} else if (exporter.type === 'memory') {
|
|
58
|
+
exporterObj = new InMemorySpanExporter()
|
|
59
|
+
} else {
|
|
60
|
+
logger.warn(
|
|
61
|
+
`Unknown exporter type: ${exporter.type}, defaulting to console.`
|
|
62
|
+
)
|
|
63
|
+
exporterObj = new ConsoleSpanExporter(exporterOptions)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// We use a SimpleSpanProcessor for the console/memory exporters and a BatchSpanProcessor for the others.
|
|
67
|
+
const spanProcessor = ['memory', 'console'].includes(exporter.type)
|
|
68
|
+
? new SimpleSpanProcessor(exporterObj)
|
|
69
|
+
: new BatchSpanProcessor(exporterObj)
|
|
70
|
+
spanProcessors.push(spanProcessor)
|
|
71
|
+
exporterObjs.push(exporterObj)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
provider.addSpanProcessor(spanProcessors)
|
|
75
|
+
const tracer = provider.getTracer(moduleName, moduleVersion)
|
|
76
|
+
const propagator = provider.getPropagator()
|
|
77
|
+
|
|
78
|
+
return { tracer, exporters: exporterObjs, propagator, provider, spanProcessors }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = setupTelemetry
|
package/lib/telemetry.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
const fp = require('fastify-plugin')
|
|
4
4
|
const { SpanStatusCode, SpanKind } = require('@opentelemetry/api')
|
|
5
|
-
const { ConsoleSpanExporter, BatchSpanProcessor, SimpleSpanProcessor, InMemorySpanExporter } = require('@opentelemetry/sdk-trace-base')
|
|
6
|
-
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions')
|
|
7
|
-
const { Resource } = require('@opentelemetry/resources')
|
|
8
|
-
const { PlatformaticTracerProvider } = require('./platformatic-trace-provider')
|
|
9
5
|
const { PlatformaticContext } = require('./platformatic-context')
|
|
10
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
fastifyTextMapGetter,
|
|
8
|
+
fastifyTextMapSetter,
|
|
9
|
+
} = require('./fastify-text-map')
|
|
10
|
+
const setup = require('./telemetry-config')
|
|
11
11
|
const { formatParamUrl } = require('@fastify/swagger')
|
|
12
12
|
const fastUri = require('fast-uri')
|
|
13
13
|
|
|
@@ -25,9 +25,6 @@ const fastUri = require('fast-uri')
|
|
|
25
25
|
// 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:
|
|
26
26
|
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
27
27
|
// 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.
|
|
28
|
-
|
|
29
|
-
const { name: moduleName, version: moduleVersion } = require('../package.json')
|
|
30
|
-
|
|
31
28
|
const extractPath = (request) => {
|
|
32
29
|
// We must user RouterPath, because otherwise `/test/123` will be considered as
|
|
33
30
|
// a different operation than `/test/321`. In case is not set (this should actually happen only for HTTP/404) we fallback to the path.
|
|
@@ -58,104 +55,53 @@ const formatSpanAttributes = {
|
|
|
58
55
|
'server.port': urlData.port,
|
|
59
56
|
'http.request.method': method,
|
|
60
57
|
'url.path': path,
|
|
61
|
-
'url.scheme': protocol
|
|
58
|
+
'url.scheme': protocol,
|
|
62
59
|
}
|
|
63
60
|
},
|
|
64
61
|
reply (reply) {
|
|
65
62
|
return {
|
|
66
|
-
'http.response.status_code': reply.statusCode
|
|
63
|
+
'http.response.status_code': reply.statusCode,
|
|
67
64
|
}
|
|
68
65
|
},
|
|
69
66
|
error (error) {
|
|
70
67
|
return {
|
|
71
68
|
'error.name': error.name,
|
|
72
69
|
'error.message': error.message,
|
|
73
|
-
'error.stack': error.stack
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const setupProvider = (app, opts) => {
|
|
79
|
-
const { serviceName, version } = opts
|
|
80
|
-
let exporter = opts.exporter
|
|
81
|
-
if (!exporter) {
|
|
82
|
-
app.log.warn('No exporter configured, defaulting to console.')
|
|
83
|
-
exporter = { type: 'console' }
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const exporters = Array.isArray(exporter) ? exporter : [exporter]
|
|
87
|
-
|
|
88
|
-
app.log.info(`Setting up telemetry for service: ${serviceName}${version ? ' version: ' + version : ''} with exporter of type ${exporter.type}`)
|
|
89
|
-
|
|
90
|
-
const provider = new PlatformaticTracerProvider({
|
|
91
|
-
resource: new Resource({
|
|
92
|
-
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
|
|
93
|
-
[SemanticResourceAttributes.SERVICE_VERSION]: version
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
const exporterObjs = []
|
|
98
|
-
const spanProcessors = []
|
|
99
|
-
for (const exporter of exporters) {
|
|
100
|
-
// Exporter config:
|
|
101
|
-
// https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_exporter_zipkin.ExporterConfig.html
|
|
102
|
-
const exporterOptions = { ...exporter.options, serviceName }
|
|
103
|
-
|
|
104
|
-
let exporterObj
|
|
105
|
-
if (exporter.type === 'console') {
|
|
106
|
-
exporterObj = new ConsoleSpanExporter(exporterOptions)
|
|
107
|
-
} else if (exporter.type === 'otlp') {
|
|
108
|
-
// We require here because this require (and only the require!) creates some issue with c8 on some mjs tests on other modules. Since we need an assignemet here, we don't use a switch.
|
|
109
|
-
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto')
|
|
110
|
-
exporterObj = new OTLPTraceExporter(exporterOptions)
|
|
111
|
-
} else if (exporter.type === 'zipkin') {
|
|
112
|
-
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')
|
|
113
|
-
exporterObj = new ZipkinExporter(exporterOptions)
|
|
114
|
-
} else if (exporter.type === 'memory') {
|
|
115
|
-
exporterObj = new InMemorySpanExporter()
|
|
116
|
-
} else {
|
|
117
|
-
app.log.warn(`Unknown exporter type: ${exporter.type}, defaulting to console.`)
|
|
118
|
-
exporterObj = new ConsoleSpanExporter(exporterOptions)
|
|
70
|
+
'error.stack': error.stack,
|
|
119
71
|
}
|
|
120
|
-
|
|
121
|
-
// We use a SimpleSpanProcessor for the console/memory exporters and a BatchSpanProcessor for the others.
|
|
122
|
-
const spanProcessor = ['memory', 'console'].includes(exporter.type) ? new SimpleSpanProcessor(exporterObj) : new BatchSpanProcessor(exporterObj)
|
|
123
|
-
spanProcessors.push(spanProcessor)
|
|
124
|
-
exporterObjs.push(exporterObj)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
provider.addSpanProcessor(spanProcessors)
|
|
128
|
-
const tracer = provider.getTracer(moduleName, moduleVersion)
|
|
129
|
-
const propagator = provider.getPropagator()
|
|
130
|
-
return { tracer, exporters: exporterObjs, propagator, provider }
|
|
72
|
+
},
|
|
131
73
|
}
|
|
132
74
|
|
|
133
75
|
async function setupTelemetry (app, opts) {
|
|
134
|
-
const openTelemetryAPIs =
|
|
76
|
+
const openTelemetryAPIs = setup(opts, app.log)
|
|
135
77
|
const { tracer, propagator, provider } = openTelemetryAPIs
|
|
136
|
-
const skipOperations =
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
78
|
+
const skipOperations =
|
|
79
|
+
opts?.skip?.map((skip) => {
|
|
80
|
+
const { method, path } = skip
|
|
81
|
+
return `${method}${path}`
|
|
82
|
+
}) || []
|
|
140
83
|
|
|
141
84
|
// expose the span as a request decorator
|
|
142
85
|
app.decorateRequest('span')
|
|
143
86
|
|
|
144
|
-
const
|
|
87
|
+
const startHTTPSpan = async (request) => {
|
|
145
88
|
if (skipOperations.includes(`${request.method}${request.url}`)) {
|
|
146
|
-
request.log.debug(
|
|
89
|
+
request.log.debug(
|
|
90
|
+
{ operation: `${request.method}${request.url}` },
|
|
91
|
+
'Skipping telemetry'
|
|
92
|
+
)
|
|
147
93
|
return
|
|
148
94
|
}
|
|
149
95
|
|
|
150
96
|
// We populate the context with the incoming request headers
|
|
151
|
-
let context = propagator.extract(
|
|
97
|
+
let context = propagator.extract(
|
|
98
|
+
new PlatformaticContext(),
|
|
99
|
+
request,
|
|
100
|
+
fastifyTextMapGetter
|
|
101
|
+
)
|
|
152
102
|
|
|
153
103
|
const path = extractPath(request)
|
|
154
|
-
const span = tracer.startSpan(
|
|
155
|
-
formatSpanName(request, path),
|
|
156
|
-
{},
|
|
157
|
-
context
|
|
158
|
-
)
|
|
104
|
+
const span = tracer.startSpan(formatSpanName(request, path), {}, context)
|
|
159
105
|
span.kind = SpanKind.SERVER
|
|
160
106
|
// Next 2 lines are needed by W3CTraceContextPropagator
|
|
161
107
|
context = context.setSpan(span)
|
|
@@ -164,7 +110,7 @@ async function setupTelemetry (app, opts) {
|
|
|
164
110
|
request.span = span
|
|
165
111
|
}
|
|
166
112
|
|
|
167
|
-
const setErrorInSpan = async (request,
|
|
113
|
+
const setErrorInSpan = async (request, _reply, error) => {
|
|
168
114
|
const span = request.span
|
|
169
115
|
span.setAttributes(formatSpanAttributes.error(error))
|
|
170
116
|
}
|
|
@@ -176,7 +122,7 @@ async function setupTelemetry (app, opts) {
|
|
|
176
122
|
}
|
|
177
123
|
}
|
|
178
124
|
|
|
179
|
-
const
|
|
125
|
+
const endHTTPSpan = async (request, reply) => {
|
|
180
126
|
const span = request.span
|
|
181
127
|
if (span) {
|
|
182
128
|
const spanStatus = { code: SpanStatusCode.OK }
|
|
@@ -189,9 +135,9 @@ async function setupTelemetry (app, opts) {
|
|
|
189
135
|
}
|
|
190
136
|
}
|
|
191
137
|
|
|
192
|
-
app.addHook('onRequest',
|
|
138
|
+
app.addHook('onRequest', startHTTPSpan)
|
|
193
139
|
app.addHook('onSend', injectPropagationHeadersInReply)
|
|
194
|
-
app.addHook('onResponse',
|
|
140
|
+
app.addHook('onResponse', endHTTPSpan)
|
|
195
141
|
app.addHook('onError', setErrorInSpan)
|
|
196
142
|
app.addHook('onClose', async function () {
|
|
197
143
|
try {
|
|
@@ -206,9 +152,9 @@ async function setupTelemetry (app, opts) {
|
|
|
206
152
|
const context = span.context
|
|
207
153
|
const headers = {}
|
|
208
154
|
propagator.inject(context, headers, {
|
|
209
|
-
set (
|
|
155
|
+
set (_carrier, key, value) {
|
|
210
156
|
headers[key] = value
|
|
211
|
-
}
|
|
157
|
+
},
|
|
212
158
|
})
|
|
213
159
|
return headers
|
|
214
160
|
}
|
|
@@ -218,12 +164,15 @@ async function setupTelemetry (app, opts) {
|
|
|
218
164
|
// So the client caller is responible of:
|
|
219
165
|
// - setting the propagatorHeaders in the client request
|
|
220
166
|
// - closing the span
|
|
221
|
-
const
|
|
167
|
+
const startHTTPSpanClient = (url, method, ctx) => {
|
|
222
168
|
let context = ctx || new PlatformaticContext()
|
|
223
169
|
const urlObj = fastUri.parse(url)
|
|
224
170
|
|
|
225
171
|
if (skipOperations.includes(`${method}${urlObj.path}`)) {
|
|
226
|
-
app.log.debug(
|
|
172
|
+
app.log.debug(
|
|
173
|
+
{ operation: `${method}${urlObj.path}` },
|
|
174
|
+
'Skipping telemetry'
|
|
175
|
+
)
|
|
227
176
|
return
|
|
228
177
|
}
|
|
229
178
|
|
|
@@ -247,7 +196,7 @@ async function setupTelemetry (app, opts) {
|
|
|
247
196
|
'http.request.method': method,
|
|
248
197
|
'url.full': url,
|
|
249
198
|
'url.path': urlObj.path,
|
|
250
|
-
'url.scheme': urlObj.scheme
|
|
199
|
+
'url.scheme': urlObj.scheme,
|
|
251
200
|
}
|
|
252
201
|
: {}
|
|
253
202
|
span.setAttributes(attributes)
|
|
@@ -260,7 +209,7 @@ async function setupTelemetry (app, opts) {
|
|
|
260
209
|
return { span, telemetryHeaders }
|
|
261
210
|
}
|
|
262
211
|
|
|
263
|
-
const
|
|
212
|
+
const endHTTPSpanClient = (span, response) => {
|
|
264
213
|
/* istanbul ignore next */
|
|
265
214
|
if (!span) {
|
|
266
215
|
return
|
|
@@ -271,13 +220,13 @@ async function setupTelemetry (app, opts) {
|
|
|
271
220
|
spanStatus.code = SpanStatusCode.ERROR
|
|
272
221
|
}
|
|
273
222
|
span.setAttributes({
|
|
274
|
-
'http.response.status_code': response.statusCode
|
|
223
|
+
'http.response.status_code': response.statusCode,
|
|
275
224
|
})
|
|
276
225
|
span.setStatus(spanStatus)
|
|
277
226
|
} else {
|
|
278
227
|
span.setStatus({
|
|
279
228
|
code: SpanStatusCode.ERROR,
|
|
280
|
-
message: 'No response received'
|
|
229
|
+
message: 'No response received',
|
|
281
230
|
})
|
|
282
231
|
}
|
|
283
232
|
span.end()
|
|
@@ -291,17 +240,17 @@ async function setupTelemetry (app, opts) {
|
|
|
291
240
|
span.setAttributes(formatSpanAttributes.error(error))
|
|
292
241
|
}
|
|
293
242
|
|
|
294
|
-
//
|
|
295
|
-
const
|
|
243
|
+
// In the generic "startSpan" the attributes here are specified by the caller
|
|
244
|
+
const startSpan = (name, ctx, attributes = {}, kind = SpanKind.INTERNAL) => {
|
|
296
245
|
const context = ctx || new PlatformaticContext()
|
|
297
246
|
const span = tracer.startSpan(name, {}, context)
|
|
298
|
-
span.kind =
|
|
247
|
+
span.kind = kind
|
|
299
248
|
span.setAttributes(attributes)
|
|
300
249
|
span.context = context
|
|
301
250
|
return span
|
|
302
251
|
}
|
|
303
252
|
|
|
304
|
-
const
|
|
253
|
+
const endSpan = (span, error) => {
|
|
305
254
|
/* istanbul ignore next */
|
|
306
255
|
if (!span) {
|
|
307
256
|
return
|
|
@@ -309,7 +258,7 @@ async function setupTelemetry (app, opts) {
|
|
|
309
258
|
if (error) {
|
|
310
259
|
span.setStatus({
|
|
311
260
|
code: SpanStatusCode.ERROR,
|
|
312
|
-
message: error.message
|
|
261
|
+
message: error.message,
|
|
313
262
|
})
|
|
314
263
|
} else {
|
|
315
264
|
const spanStatus = { code: SpanStatusCode.OK }
|
|
@@ -320,11 +269,12 @@ async function setupTelemetry (app, opts) {
|
|
|
320
269
|
|
|
321
270
|
app.decorate('openTelemetry', {
|
|
322
271
|
...openTelemetryAPIs,
|
|
323
|
-
|
|
324
|
-
|
|
272
|
+
startHTTPSpanClient,
|
|
273
|
+
endHTTPSpanClient,
|
|
325
274
|
setErrorInSpanClient,
|
|
326
|
-
|
|
327
|
-
|
|
275
|
+
startSpan,
|
|
276
|
+
endSpan,
|
|
277
|
+
SpanKind,
|
|
328
278
|
})
|
|
329
279
|
}
|
|
330
280
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/telemetry",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.21",
|
|
4
4
|
"description": "OpenTelemetry integration for Platformatic",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Marco Piraccini <marco.piraccini@gmail.com>",
|
|
@@ -10,25 +10,28 @@
|
|
|
10
10
|
},
|
|
11
11
|
"license": "Apache-2.0",
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"borp": "^0.
|
|
14
|
-
"fastify": "^
|
|
15
|
-
"
|
|
16
|
-
"
|
|
13
|
+
"borp": "^0.17.0",
|
|
14
|
+
"fastify": "^5.0.0",
|
|
15
|
+
"express": "^4.19.2",
|
|
16
|
+
"neostandard": "^0.11.1",
|
|
17
|
+
"typescript": "^5.5.4"
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
|
19
|
-
"@fastify/swagger": "^
|
|
20
|
+
"@fastify/swagger": "^9.0.0",
|
|
20
21
|
"@opentelemetry/api": "^1.8.0",
|
|
22
|
+
"@opentelemetry/auto-instrumentations-node": "^0.50.0",
|
|
21
23
|
"@opentelemetry/core": "^1.22.0",
|
|
22
|
-
"@opentelemetry/exporter-trace-otlp-proto": "^0.
|
|
24
|
+
"@opentelemetry/exporter-trace-otlp-proto": "^0.53.0",
|
|
23
25
|
"@opentelemetry/exporter-zipkin": "^1.22.0",
|
|
24
26
|
"@opentelemetry/resources": "^1.22.0",
|
|
27
|
+
"@opentelemetry/sdk-node": "^0.53.0",
|
|
25
28
|
"@opentelemetry/sdk-trace-base": "^1.22.0",
|
|
26
29
|
"@opentelemetry/semantic-conventions": "^1.22.0",
|
|
27
30
|
"fast-uri": "^2.3.0",
|
|
28
|
-
"fastify-plugin": "^
|
|
31
|
+
"fastify-plugin": "^5.0.0"
|
|
29
32
|
},
|
|
30
33
|
"scripts": {
|
|
31
|
-
"test": "npm run lint
|
|
32
|
-
"lint": "
|
|
34
|
+
"test": "npm run lint && borp",
|
|
35
|
+
"lint": "eslint"
|
|
33
36
|
}
|
|
34
37
|
}
|