open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.96k stars 860 forks source link

Support Cutsom OperationMetrics、AttributesExtractor、ContextCustomizer in Instrumenter #12250

Open pepeshore opened 2 months ago

pepeshore commented 2 months ago

Is your feature request related to a problem? Please describe.

We forked this code repository and made several significant modifications to support our clients. These changes mainly involved adding some attributes to certain spans and modifying the names and dimensions of metrics. Consequently, merging the latest open-source code will result in many conflicts. We hope to receive support in providing some extension mechanisms that allow us to expand functionality without modifying this code repository.

Describe the solution you'd like

We expecte the following fields in io.opentelemetry.instrumentation.api.instrumenter.Instrumenter can be customized

pseudo code

public final class TomcatInstrumenterFactory {

  private TomcatInstrumenterFactory() {}

  public static <REQUEST, RESPONSE> Instrumenter<Request, Response> create(
      String instrumentationName, ServletAccessor<REQUEST, RESPONSE> accessor,
      HttpServerAttributesGetter<Request, Response> httpAttributesGetter) {
    TomcatNetAttributesGetter netAttributesGetter = new TomcatNetAttributesGetter()

    return Instrumenter.<Request, Response>builder(
            GlobalOpenTelemetry.get(),
            instrumentationName,
            HttpSpanNameExtractor.create(httpAttributesGetter))
        .addAttributesExtractor(
            HttpServerAttributesExtractor.builder(httpAttributesGetter, netAttributesGetter)
                .setCapturedRequestHeaders(CommonConfig.get().getServerRequestHeaders())
                .setCapturedResponseHeaders(CommonConfig.get().getServerResponseHeaders())
                .build())
        .addAttributesExtractor(
          loadCustomAttributesExtractor(instrumentationName)
         )
        .addContextCustomizer(
          loadContextCustomizer(instrumentationName)
         )
        .addOperationMetrics(HttpServerMetrics.get())
        .addOperationMetrics(loadCustomOperationMetrics())
        .buildServerInstrumenter(TomcatRequestGetter.INSTANCE)
  }

  public AttributesExtractor loadCustomAttributesExtractor(String instrumentationName) {
    throw new UnsupportedException();
  }

  public ContextCustomizer loadContextCustomizer(String instrumentationName) {
    throw new UnsupportedException();
  }

  public OperationMetrics loadCustomOperationMetrics(String instrumentationName)) {
    throw new UnsupportedException();  
  }

}

Describe alternatives you've considered

No response

Additional context

No response

trask commented 2 months ago

I would support this, some kind of SPI that a Java agent extension could use to add these things to the existing instrumentations

cc @steverao @ralf0131 who I believe have similar need

steverao commented 1 month ago

I would support this, some kind of SPI that a Java agent extension could use to add these things to the existing instrumentations

cc @steverao @ralf0131 who I believe have similar need

In fact, @pepeshore is also our colleague.😂 He is focusing on relevant field.