Skip to content

Make README example less pseudo-y #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

bcomnes
Copy link
Contributor

@bcomnes bcomnes commented Jun 23, 2025

Still WIP (see the one question), but I took a stab at improving the README example.

  • Breaks out the otel.js and the app.js into distinct code blocks
  • Set up development trace/span providers.
  • Set up basic host metrics and runtime metrics (stuff you you usually want)
  • Default to the auto instrumentation for the fastify plugin, and clarify how to use the loader flags
  • Make the manual plugin loading process a secondary example (seems more esoteric, please correct me if I'm wrong)
  • Graceful shutdown example

Checklist

@bcomnes
Copy link
Contributor Author

bcomnes commented Jun 23, 2025

The one open question I ran into was, do you also need to hook the sdk.stop() method into a fastify lifecycle, so that metrics/spans/traces drain correctly? If so, we should have an example of that.

@bcomnes
Copy link
Contributor Author

bcomnes commented Jun 23, 2025

This is what the node-sdk recommends:

// You can also use the shutdown method to gracefully shut down the SDK before process shutdown
// or on some operating system signal.
const process = require("process");
process.on("SIGTERM", () => {
  sdk
    .shutdown()
    .then(
      () => console.log("SDK shut down successfully"),
      (err) => console.log("Error shutting down SDK", err)
    )
    .finally(() => process.exit(0));
});

@bcomnes
Copy link
Contributor Author

bcomnes commented Jun 23, 2025

Reading through the code, this plugin does NOT automatically close down the SDK on server stop. We should add the best way to do this. I'm guessing in the otel.js file as the example shows.

@metcoder95
Copy link
Member

metcoder95 commented Jun 24, 2025

Reading through the code, this plugin does NOT automatically close down the SDK on server stop. We should add the best way to do this. I'm guessing in the otel.js file as the example shows.

Noup it currently does not; you'd like to open a PR for that? We can extend the documentation there as well.

The best is to hop into the onPreClose hook to stop the instrumentation and let telemetry get flushed.

README.md Outdated

const app = fastify();
If `registerOnInitialization=true`, use a loader to load your otel.js before everything else.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd set this up as an alternative; often instrumentation is just imported manually first thing when booting up a service

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why the the loader flags are appropriate as the default suggestion is because fastify-cli. Rather than directly launching some entry point with node where you control load order, many fastify apps will just have an app.js that fastify-cli loads. In order to guarantee the otel instruments are the first thing that loads, you must load them in with the loader/reqiuire flag otherwise the fastify-cli might load in a module before the otel instruments can intercept it.

Additionally the otel tutorials and docs recommend the loader flag approach. I found that working through those docs, and then hopping over to this module and seeing two different approaches super confusing.

https://opentelemetry.io/docs/languages/js/getting-started/nodejs/
https://github.com/open-telemetry/opentelemetry-js/blob/main/doc/esm-support.md

(Speaking of loader flags, I need to add the experimental loader note for esm)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough; I'd just add a note that import is also an alternative


Example:
## Example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all this code could be moved to an example.js file instead of the readme?

it seems too much here to me (and we can't test it too)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Will add in a runnable example in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reduced the scope of the example in the README. I will still set up an external example folder.

@bcomnes
Copy link
Contributor Author

bcomnes commented Jun 24, 2025

A few more updates:

  • Reduce the overall scope of the example (remove host metrics and runtime metrics)
  • Move FastifyOtelInstrumentation into the instrumentations array. It's my understanding this is the preferred way to set up instruments in otel, and it handles things like setting the setTracerProvider automatically.
  • Add a graceful shutdown to the example
  • Add more information about loading otel in ESM and add fastify examples
  • Remove any use of the serverName setting, because this is probably a mistake to have that setting Don't set ATTR_SERVICE_NAME in fastify instrumentation  #70

Next steps I will set up a runnable example folder or two, and narrow down the README example more.

@bcomnes bcomnes marked this pull request as draft June 25, 2025 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants