Skip to content
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

Image Render: Support Tracing #586

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

lucychen-grafana
Copy link

@lucychen-grafana lucychen-grafana commented Dec 10, 2024

Testing Instructons:

Local Setup:

  1. Install tempo https://grafana.com/docs/tempo/latest/getting-started/docker-example/
  1. Checkout this branch of grafana and run grafana-enterprise. Set otlp address in defaults.ini
[tracing.opentelemetry.otlp]
address = localhost:4317
  1. Checkout this pr grafana-image-renderer branch. Run the server: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://localhost:4318/v1/traces" node build/app.js server --port=8081 --config=dev.json to set env var
  2. Open Localhost:3000; run a report or PDF creation, or any call to the image renderer service
  3. Check traces: Explore -> Select ‘gdev-tempo’ datasource -> run query resource.service.name="image-renderer-service"
  4. Check logs in the console to confirm trace_id and span_id is included in the logs

Deployment_tools pr for adding env vars

@CLAassistant
Copy link

CLAassistant commented Dec 10, 2024

CLA assistant check
All committers have signed the CLA.

@lucychen-grafana lucychen-grafana linked an issue Dec 10, 2024 that may be closed by this pull request
@@ -45,6 +46,8 @@ export class ConsoleLogger implements Logger {
transports.push(new winston.transports.Console(options));
}

//@opentelemetry/instrumentation-winston auto inject trace-context into Winston log records

Copy link
Author

Choose a reason for hiding this comment

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

Confirm this is the case in the log output in terminal. You can check that trace_id, span_id, and trace_flags is included

src/tracing.ts Outdated
@@ -0,0 +1,41 @@
import {NodeSDK} from '@opentelemetry/sdk-node';
Copy link
Author

Choose a reason for hiding this comment

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

I followed OpenTelemetry Doc for Node.js to implement this

src/tracing.ts Outdated
instrumentations: [
getNodeAutoInstrumentations({
// only instrument fs if it is part of another trace
'@opentelemetry/instrumentation-fs': {
Copy link
Author

@lucychen-grafana lucychen-grafana Jan 30, 2025

Choose a reason for hiding this comment

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

the fs module is very noisy. It create 10+ traces with singular span so it did not make much sense to have it unless it's part of another trace.

There is a list of supported autoinstrumentation. We can opt out of any in the future we do not care to trace those libraries

src/tracing.ts Outdated
Comment on lines 31 to 40
export function startTracing(log: Logger) {
sdk.start();
log.info('Starting tracing');

process.on('SIGTERM', () => {
sdk.shutdown()
.then(() => log.debug('Tracing terminated'))
.catch((error) => log.error('Error terminating tracing', error))
.finally(() => process.exit(0));
});
Copy link
Author

Choose a reason for hiding this comment

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

Create startTracing separately from the rest of initialization steps b/c we want to only capture traces if it's enabled

@@ -1,3 +1,4 @@
import { startTracing } from './tracing';
Copy link
Author

Choose a reason for hiding this comment

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

OpenTelemetry needs to be initialized before the other modules (express, http, winston, etc) in order for it to auto capture telemetry trace data from those libraries so it must come first

@lucychen-grafana lucychen-grafana requested review from a team, juanicabanas and AgnesToulet and removed request for a team January 30, 2025 16:55
@lucychen-grafana lucychen-grafana marked this pull request as ready for review January 30, 2025 16:56
Copy link
Contributor

@AgnesToulet AgnesToulet left a comment

Choose a reason for hiding this comment

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

This looks good! I tested it and this worked. However, I don't find the current traces very useful as there is a big blank gap in the middle.

An easy place to add some manual spans would be in the withTimingMetrics function. Something like that but adding some config checks:

  async withTimingMetrics<T>(step: string, callback: () => Promise<T>): Promise<T> {
    if (this.config.timingMetrics) {
      const endTimer = this.metrics.durationHistogram.startTimer({ step });
      const res = this.tracer.startActiveSpan(step, async (span) => {
        const cbRes = await callback();
        span.end();
        return cbRes;
      });
      endTimer();

      return res;
    } else {
      return callback();
    }
  }

The Browser class could initialize its tracer in its constructor (this should work for all browser implementations, e.g. with mode: "clustered"):

export class Browser {
  tracer: Tracer;

  constructor(protected config: RenderingConfig, protected log: Logger, protected metrics: Metrics) {
    this.log.debug('Browser initialized', 'config', this.config);
    this.tracer = trace.getTracer('browser');
  }
  ...

Also, I looked into what is missing to have a link between Grafana traces and image renderer traces and this simple change is working (to enable traces in Grafana, you need to set:

[tracing.opentelemetry.otlp]
# otlp destination (ex localhost:4317)
address = localhost:4317

). I will test it out a bit more and check if this need to be initialized only when tracing is enabled or if this is a no-op when tracing is disabled.

package.json Outdated Show resolved Hide resolved
src/tracing.ts Outdated
@@ -0,0 +1,41 @@
import {NodeSDK} from '@opentelemetry/sdk-node';
Copy link
Contributor

Choose a reason for hiding this comment

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

This whole file should be formatted using Prettier linter (we don't have a check for that in CI but we should do our best).

src/tracing.ts Outdated Show resolved Hide resolved
@AgnesToulet AgnesToulet mentioned this pull request Feb 10, 2025
@lucychen-grafana
Copy link
Author

[tracing.opentelemetry.otlp]

otlp destination (ex localhost:4317)

address = localhost:4317

@AgnesToulet setting this address is enable traces in Grafana in dev env right? Traces should already be enabled in Cloud via Faro

@AgnesToulet
Copy link
Contributor

Yes it's for your local environment. It's enabled by default on Cloud (Faro is for frontend monitoring, its configuration is in a different section of the ini file, I don't think both are related).

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.

Render: Support tracing
4 participants