-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#14): If possible, set serverName as per General SDK Configurati…
…on, use process.env.OTEL_SERVICE_NAME; (#17) Co-authored-by: James Sumners <[email protected]>
- Loading branch information
Showing
4 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { expectAssignable } from 'tsd' | ||
import { InstrumentationBase, InstrumentationConfig } from '@opentelemetry/instrumentation' | ||
import { fastify as Fastify } from 'fastify' | ||
|
||
import { FastifyOtelInstrumentation, FastifyOtelInstrumentationOpts } from '..' | ||
|
||
expectAssignable<InstrumentationBase>(new FastifyOtelInstrumentation()) | ||
expectAssignable<InstrumentationConfig>({ servername: 'server', enabled: true } as FastifyOtelInstrumentationOpts) | ||
expectAssignable<InstrumentationConfig>({} as FastifyOtelInstrumentationOpts) | ||
|
||
const app = Fastify() | ||
app.register(new FastifyOtelInstrumentation().plugin()) | ||
app.register((nested, _opts, done) => { | ||
nested.register(new FastifyOtelInstrumentation().plugin()) | ||
done() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict' | ||
|
||
const { | ||
test, | ||
describe, | ||
after, | ||
afterEach, | ||
beforeEach | ||
} = require('node:test') | ||
const assert = require('node:assert') | ||
const Fastify = require(process.env.FASTIFY_VERSION || 'fastify') | ||
|
||
const { | ||
AsyncHooksContextManager | ||
} = require('@opentelemetry/context-async-hooks') | ||
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node') | ||
const { | ||
InMemorySpanExporter, | ||
SimpleSpanProcessor | ||
} = require('@opentelemetry/sdk-trace-base') | ||
const { context } = require('@opentelemetry/api') | ||
|
||
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http') | ||
|
||
const FastifyInstrumentation = require('..') | ||
|
||
// OTEL_SERVICE_NAME | ||
// https://opentelemetry.io/docs/languages/sdk-configuration/general/ | ||
describe('Environment variable aware FastifyInstrumentation', () => { | ||
process.env.OTEL_SERVICE_NAME = 'my_app' | ||
|
||
const httpInstrumentation = new HttpInstrumentation() | ||
const instrumentation = new FastifyInstrumentation() | ||
const contextManager = new AsyncHooksContextManager() | ||
const memoryExporter = new InMemorySpanExporter() | ||
const provider = new NodeTracerProvider() | ||
const spanProcessor = new SimpleSpanProcessor(memoryExporter) | ||
|
||
provider.addSpanProcessor(spanProcessor) | ||
context.setGlobalContextManager(contextManager) | ||
httpInstrumentation.setTracerProvider(provider) | ||
instrumentation.setTracerProvider(provider) | ||
|
||
describe('Instrumentation#enabled', () => { | ||
beforeEach(() => { | ||
instrumentation.enable() | ||
httpInstrumentation.enable() | ||
contextManager.enable() | ||
}) | ||
|
||
afterEach(() => { | ||
contextManager.disable() | ||
instrumentation.disable() | ||
httpInstrumentation.disable() | ||
spanProcessor.forceFlush() | ||
memoryExporter.reset() | ||
}) | ||
|
||
test('should create span with service.name equal to general sdk configuration', async t => { | ||
const app = Fastify() | ||
const plugin = instrumentation.plugin() | ||
|
||
await app.register(plugin) | ||
|
||
app.get('/', async (request, reply) => 'hello world') | ||
|
||
await app.listen() | ||
|
||
after(() => app.close()) | ||
|
||
const response = await fetch( | ||
`http://localhost:${app.server.address().port}/` | ||
) | ||
|
||
const spans = memoryExporter | ||
.getFinishedSpans() | ||
.filter(span => span.instrumentationLibrary.name === '@fastify/otel') | ||
|
||
const [end, start] = spans | ||
|
||
assert.equal(spans.length, 2) | ||
assert.deepStrictEqual(start.attributes, { | ||
'fastify.root': '@fastify/otel', | ||
'http.route': '/', | ||
'service.name': 'my_app', | ||
'http.request.method': 'GET', | ||
'http.response.status_code': 200 | ||
}) | ||
assert.deepStrictEqual(end.attributes, { | ||
'hook.name': 'fastify -> @fastify/otel - route-handler', | ||
'fastify.type': 'request-handler', | ||
'http.route': '/', | ||
'service.name': 'my_app', | ||
'hook.callback.name': 'anonymous' | ||
}) | ||
assert.equal(response.status, 200) | ||
assert.equal(await response.text(), 'hello world') | ||
}) | ||
}) | ||
}) |