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 ### :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 ### :house: Internal
* test(sdk-metrics): fix multiple problematic assertRejects() calls [#5611](https://github.com/open-telemetry/opentelemetry-js/pull/5611) @cjihrig * 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. // You can optionally detect resources asynchronously from the environment.
// Detected resources are merged with the resources provided in the SDK configuration. // Detected resources are merged with the resources provided in the SDK configuration.
sdk.start().then(() => { sdk.start();
// Resources have been detected and SDK is started // Resources have been detected and SDK is started
console.log(`SDK started`) console.log(`SDK started`)
// Start the http server // Start the http server
const fastify = require('fastify')({ const fastify = require('fastify')({
logger: true logger: true
}) })
fastify.get('/', function (request, reply) { fastify.get('/', function (request, reply) {
reply.send({ hello: 'world' }) reply.send({ hello: 'world' })
}) })
fastify.listen({ port: 3000 }, function (err, address) { fastify.listen({ port: 3000 }, function (err, address) {
if (err) { if (err) {
fastify.log.error(err) fastify.log.error(err)
process.exit(1) process.exit(1)
} }
console.log(`Server is now listening on ${address}`) console.log(`Server is now listening on ${address}`)
}) })
});
// You can also use the shutdown method to gracefully shut down the SDK before process shutdown // You can also use the shutdown method to gracefully shut down the SDK before process shutdown
// or on some operating system signal. // or on some operating system signal.
@ -159,35 +158,34 @@ const sdk = new opentelemetry.NodeSDK({
// You can optionally detect resources asynchronously from the environment. // You can optionally detect resources asynchronously from the environment.
// Detected resources are merged with the resources provided in the SDK configuration. // Detected resources are merged with the resources provided in the SDK configuration.
sdk.start().then(() => { sdk.start();
// Resources have been detected and SDK is started // Resources have been detected and SDK is started
console.log(`SDK started`) console.log(`SDK started`)
// Create Meter with the name `http-server` // Create Meter with the name `http-server`
const appMeter = api.metrics.getMeter('http-server') const appMeter = api.metrics.getMeter('http-server')
// Use the created Meter to create a counter instrument // Use the created Meter to create a counter instrument
const numberOfRequests = appMeter.createCounter('request-counter') const numberOfRequests = appMeter.createCounter('request-counter')
// Start the http server // Start the http server
const fastify = require('fastify')({ const fastify = require('fastify')({
logger: true logger: true
}) })
fastify.get('/', function (request, reply) { fastify.get('/', function (request, reply) {
// Increase the counter by 1 each time the `/` endpoint is requested // Increase the counter by 1 each time the `/` endpoint is requested
numberOfRequests.add(1) numberOfRequests.add(1)
reply.send({ hello: 'world' }) reply.send({ hello: 'world' })
}) })
fastify.listen({ port: 3000 }, function (err, address) { fastify.listen({ port: 3000 }, function (err, address) {
if (err) { if (err) {
fastify.log.error(err) fastify.log.error(err)
process.exit(1) process.exit(1)
} }
console.log(`Server is now listening on ${address}`) console.log(`Server is now listening on ${address}`)
}) })
});
// You can also use the shutdown method to gracefully shut down the SDK before process shutdown // You can also use the shutdown method to gracefully shut down the SDK before process shutdown
// or on some operating system signal. // or on some operating system signal.