open-telemetry / opentelemetry-java-instrumentation

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

Could we avoid attributes merge in HttpServerMetrics.onEnd? #11887

Open wgy035 opened 4 months ago

wgy035 commented 4 months ago

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

While conducting a performance test, I discovered through flame graph analysis that the merge operation in the HttpServerMetrics.onEnd() causes performance overhead. image

The startAttributes is generated by HttpCommonAttributesExtractor.onStart() and all attributes are extracted from request, could we move the attributes extract into HttpCommonAttributesExtractor.onEnd() to avoid merge in the HttpServerMetrics.onEnd()? I have reviewed the tracking code, the request will not be changed.

Describe the solution you'd like

I would like HttpCommonAttributesExtractor to extract attributes onEnd, so that it can be used in HttpServerMetrics directly. And startAttributes().toBuilder().putAll(endAttributes).build() is no longer needed.

Describe alternatives you've considered

No response

Additional context

No response

knarfli commented 3 months ago

looked interesting, I want to take a look into it.

wgy035 commented 3 months ago

I audited and tested most of AttributeExtractor.onEnd implementations, in each scenario, whether the attribute extract is executed in onStart or onEnd, the test results are the same.