Fix sdk.start() as Not Async (#5617)

This commit is contained in:
Jackson Weber 2025-04-23 01:11:03 -07:00 committed by GitHub
parent 8efceed28c
commit 9f5ae79b1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 29 deletions

View File

@ -24,6 +24,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
### :books: Documentation
* refactor(metrics): Updated metrics samples to no longer treat `sdk.start()` as async [#5617](https://github.com/open-telemetry/opentelemetry-js/pull/5617) @JacksonWeber
### :house: Internal
* test(sdk-metrics): fix multiple problematic assertRejects() calls [#5611](https://github.com/open-telemetry/opentelemetry-js/pull/5611) @cjihrig

View File

@ -71,28 +71,27 @@ const sdk = new opentelemetry.NodeSDK({
// You can optionally detect resources asynchronously from the environment.
// Detected resources are merged with the resources provided in the SDK configuration.
sdk.start().then(() => {
// Resources have been detected and SDK is started
console.log(`SDK started`)
sdk.start();
// Resources have been detected and SDK is started
console.log(`SDK started`)
// Start the http server
const fastify = require('fastify')({
const fastify = require('fastify')({
logger: true
})
})
fastify.get('/', function (request, reply) {
fastify.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})
})
fastify.listen({ port: 3000 }, function (err, address) {
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
fastify.log.error(err)
process.exit(1)
}
console.log(`Server is now listening on ${address}`)
})
});
})
// You can also use the shutdown method to gracefully shut down the SDK before process shutdown
// or on some operating system signal.
@ -159,35 +158,34 @@ const sdk = new opentelemetry.NodeSDK({
// You can optionally detect resources asynchronously from the environment.
// Detected resources are merged with the resources provided in the SDK configuration.
sdk.start().then(() => {
// Resources have been detected and SDK is started
console.log(`SDK started`)
sdk.start();
// Resources have been detected and SDK is started
console.log(`SDK started`)
// Create Meter with the name `http-server`
const appMeter = api.metrics.getMeter('http-server')
// Use the created Meter to create a counter instrument
const numberOfRequests = appMeter.createCounter('request-counter')
// Create Meter with the name `http-server`
const appMeter = api.metrics.getMeter('http-server')
// Use the created Meter to create a counter instrument
const numberOfRequests = appMeter.createCounter('request-counter')
// Start the http server
const fastify = require('fastify')({
// Start the http server
const fastify = require('fastify')({
logger: true
})
})
fastify.get('/', function (request, reply) {
fastify.get('/', function (request, reply) {
// Increase the counter by 1 each time the `/` endpoint is requested
numberOfRequests.add(1)
reply.send({ hello: 'world' })
})
})
fastify.listen({ port: 3000 }, function (err, address) {
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
fastify.log.error(err)
process.exit(1)
}
console.log(`Server is now listening on ${address}`)
})
});
})
// You can also use the shutdown method to gracefully shut down the SDK before process shutdown
// or on some operating system signal.