tompea
tompea

Reputation: 135

Renaming Span with Open Telemetry HTTP Instrumentation

I am using the HTTP Instrumentation from @opentelemetry/instrumentation-http. In this your spans are automatically named something generic like HTTP GET OR HTTP POST and natively span.updateName doesn't actually rename the span when it is sent off. I have found a solution here (not exactly applicable to my issue but best I could find): https://github.com/open-telemetry/opentelemetry-dotnet/issues/1277 and reworked it for typescript:

class ActivityNameProcessor implements SpanProcessor {
  onStart(span: Span): void {
    // can't update name here as I don't know if its the span I want to rename
  }
  onEnd(span: Span): void {
    // can't update span here because I get an error
  }
  shutdown(): Promise<void> {
    return Promise.resolve();
  }

  forceFlush(): Promise<void> {
    return Promise.resolve();
  }
}

I can then add this to my provider like such:

const provider = new NodeTracerProvider({
  resource,
});

provider.addSpanProcessor(new ActivityNameProcessor());

// I am also using a batch processor to send the traces off
provider.addSpanProcessor(
  new BatchSpanProcessor(traceExporter, {
    maxQueueSize: 1000,
    scheduledDelayMillis: 5000,
  })
);

The issue is when I attempt to change the name in onEnd() I get an error as the span is already in a complete state and the attribute cannot be changed. I can update the name in onStart() and it works as expected when the traces are view in New Relic, however, in that instance I don't have any context on the span and I do not want to rename all spans, only select ones with certain attributes. Any help of sultions would be appreciated, Thanks :)

Upvotes: 1

Views: 1835

Answers (2)

E.Eldridge
E.Eldridge

Reputation: 143

You can do this pretty simply with some Collector processing rules

span/rename:
include:
  match_type: regexp
  span_names:
  - ((GET)|(POST)|(PUT)|(DELETE)|(PATCH)|(UPDATE)|(OPTIONS)|(HEAD)|(TRACE)*?)$
name:
  separator: ' '
  from_attributes:
  - http.method
  - server.address

Upvotes: 3

SuryaVal
SuryaVal

Reputation: 158

I tried the same but it looks like auto instrumented spans cannot be renamed. Did you try starting and ending the span using middleware?

app.use(function (request: Request, response: Response, next) { 
// your code to start and end your span
});

Refer to this blog: https://ipirozhenko.com/blog/measuring-requests-duration-nodejs-express/

you can apply the same to your situation.

Upvotes: 1

Related Questions