@platformatic/telemetry 2.0.0-alpha.8 → 2.0.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.
- package/index.js +2 -0
- package/lib/node-http-telemetry.js +33 -0
- package/lib/telemetry-config.js +81 -0
- package/lib/telemetry.js +2 -79
- package/package.json +7 -4
package/index.js
CHANGED
|
@@ -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
|
|
@@ -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,22 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
const fp = require('fastify-plugin')
|
|
4
4
|
const { SpanStatusCode, SpanKind } = require('@opentelemetry/api')
|
|
5
|
-
const {
|
|
6
|
-
ConsoleSpanExporter,
|
|
7
|
-
BatchSpanProcessor,
|
|
8
|
-
SimpleSpanProcessor,
|
|
9
|
-
InMemorySpanExporter,
|
|
10
|
-
} = require('@opentelemetry/sdk-trace-base')
|
|
11
|
-
const {
|
|
12
|
-
SemanticResourceAttributes,
|
|
13
|
-
} = require('@opentelemetry/semantic-conventions')
|
|
14
|
-
const { Resource } = require('@opentelemetry/resources')
|
|
15
|
-
const { PlatformaticTracerProvider } = require('./platformatic-trace-provider')
|
|
16
5
|
const { PlatformaticContext } = require('./platformatic-context')
|
|
17
6
|
const {
|
|
18
7
|
fastifyTextMapGetter,
|
|
19
8
|
fastifyTextMapSetter,
|
|
20
9
|
} = require('./fastify-text-map')
|
|
10
|
+
const setup = require('./telemetry-config')
|
|
21
11
|
const { formatParamUrl } = require('@fastify/swagger')
|
|
22
12
|
const fastUri = require('fast-uri')
|
|
23
13
|
|
|
@@ -35,9 +25,6 @@ const fastUri = require('fast-uri')
|
|
|
35
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:
|
|
36
26
|
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
37
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.
|
|
38
|
-
|
|
39
|
-
const { name: moduleName, version: moduleVersion } = require('../package.json')
|
|
40
|
-
|
|
41
28
|
const extractPath = (request) => {
|
|
42
29
|
// We must user RouterPath, because otherwise `/test/123` will be considered as
|
|
43
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.
|
|
@@ -85,72 +72,8 @@ const formatSpanAttributes = {
|
|
|
85
72
|
},
|
|
86
73
|
}
|
|
87
74
|
|
|
88
|
-
const setupProvider = (app, opts) => {
|
|
89
|
-
const { serviceName, version } = opts
|
|
90
|
-
let exporter = opts.exporter
|
|
91
|
-
if (!exporter) {
|
|
92
|
-
app.log.warn('No exporter configured, defaulting to console.')
|
|
93
|
-
exporter = { type: 'console' }
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const exporters = Array.isArray(exporter) ? exporter : [exporter]
|
|
97
|
-
|
|
98
|
-
app.log.info(
|
|
99
|
-
`Setting up telemetry for service: ${serviceName}${version ? ' version: ' + version : ''} with exporter of type ${exporter.type}`
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
const provider = new PlatformaticTracerProvider({
|
|
103
|
-
resource: new Resource({
|
|
104
|
-
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
|
|
105
|
-
[SemanticResourceAttributes.SERVICE_VERSION]: version,
|
|
106
|
-
}),
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
const exporterObjs = []
|
|
110
|
-
const spanProcessors = []
|
|
111
|
-
for (const exporter of exporters) {
|
|
112
|
-
// Exporter config:
|
|
113
|
-
// https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_exporter_zipkin.ExporterConfig.html
|
|
114
|
-
const exporterOptions = { ...exporter.options, serviceName }
|
|
115
|
-
|
|
116
|
-
let exporterObj
|
|
117
|
-
if (exporter.type === 'console') {
|
|
118
|
-
exporterObj = new ConsoleSpanExporter(exporterOptions)
|
|
119
|
-
} else if (exporter.type === 'otlp') {
|
|
120
|
-
// 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.
|
|
121
|
-
const {
|
|
122
|
-
OTLPTraceExporter,
|
|
123
|
-
} = require('@opentelemetry/exporter-trace-otlp-proto')
|
|
124
|
-
exporterObj = new OTLPTraceExporter(exporterOptions)
|
|
125
|
-
} else if (exporter.type === 'zipkin') {
|
|
126
|
-
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')
|
|
127
|
-
exporterObj = new ZipkinExporter(exporterOptions)
|
|
128
|
-
} else if (exporter.type === 'memory') {
|
|
129
|
-
exporterObj = new InMemorySpanExporter()
|
|
130
|
-
} else {
|
|
131
|
-
app.log.warn(
|
|
132
|
-
`Unknown exporter type: ${exporter.type}, defaulting to console.`
|
|
133
|
-
)
|
|
134
|
-
exporterObj = new ConsoleSpanExporter(exporterOptions)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// We use a SimpleSpanProcessor for the console/memory exporters and a BatchSpanProcessor for the others.
|
|
138
|
-
const spanProcessor = ['memory', 'console'].includes(exporter.type)
|
|
139
|
-
? new SimpleSpanProcessor(exporterObj)
|
|
140
|
-
: new BatchSpanProcessor(exporterObj)
|
|
141
|
-
spanProcessors.push(spanProcessor)
|
|
142
|
-
exporterObjs.push(exporterObj)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
provider.addSpanProcessor(spanProcessors)
|
|
146
|
-
const tracer = provider.getTracer(moduleName, moduleVersion)
|
|
147
|
-
const propagator = provider.getPropagator()
|
|
148
|
-
|
|
149
|
-
return { tracer, exporters: exporterObjs, propagator, provider }
|
|
150
|
-
}
|
|
151
|
-
|
|
152
75
|
async function setupTelemetry (app, opts) {
|
|
153
|
-
const openTelemetryAPIs =
|
|
76
|
+
const openTelemetryAPIs = setup(opts, app.log)
|
|
154
77
|
const { tracer, propagator, provider } = openTelemetryAPIs
|
|
155
78
|
const skipOperations =
|
|
156
79
|
opts?.skip?.map((skip) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/telemetry",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "OpenTelemetry integration for Platformatic",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Marco Piraccini <marco.piraccini@gmail.com>",
|
|
@@ -11,21 +11,24 @@
|
|
|
11
11
|
"license": "Apache-2.0",
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"borp": "^0.17.0",
|
|
14
|
-
"fastify": "^
|
|
14
|
+
"fastify": "^5.0.0",
|
|
15
|
+
"express": "^4.19.2",
|
|
15
16
|
"neostandard": "^0.11.1",
|
|
16
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
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
34
|
"test": "npm run lint && borp",
|