ApplicationInsights-Java icon indicating copy to clipboard operation
ApplicationInsights-Java copied to clipboard

Custom properties aren't present in Request telemetry.

Open xsmrcek opened this issue 1 year ago • 2 comments

Expected behavior

Custom properties are present in Request telemetry. (it works with applicationinsights-core 2.6.4).

Actual behavior

Custom properties aren't present in Request telemetry. It's kinda follow up for https://github.com/microsoft/ApplicationInsights-Java/issues/2327, as it worked just fine with applicationinsights-core 2.6.4. SO I closed issue. I found that it's not working again, after migrating to new agent.

To Reproduce

Please use sample application

Sample Application

Please use RabbitTest (it needs docker to run) from this example together with agent from resources (just adjust connection string in applicationinsights.json) https://github.com/xsmrcek/minimal-example

System information

  • SDK Version:
    • applicationinsights-agent-3.4.0
    • opentelemetry-api 1.16
    • applicationinsights-web 3.4.0
  • OS type and version: it behaves the same on windows/linux under Java 15 (compiled by 15 or 17)
  • Using spring-boot
    • spring-boot-starter-parent 2.7.0
    • spring cloud 2021.0.3
  • Kotlin
    • kotlin 1.6.21
    • kotlin-coroutines 1.6.2
  • Additional relevant libraries :
    • io.projectreactor.rabbitmq reactor-rabbitmq 1.5.4

xsmrcek avatar Sep 22 '22 13:09 xsmrcek

hi @xsmrcek! since you are using kotlin, please note the javadoc on the OpenTelemetry makeCurrent() method:

Kotlin coroutine users SHOULD NOT use this method as the ThreadLocal will not be properly synced across coroutine suspension and resumption. Instead, use withContext(value.asContextElement()) provided by the opentelemetry-extension-kotlin library.

can you update that and see if you still have issues?

trask avatar Oct 13 '22 05:10 trask

HI @trask , thank you for pointing it ou! I fixed it in my code (at least I hope I did it right). But my problem here is with RequestTelemetry not spans (but this fixed something else :D ). I have problem with this:

telemetryRequest.properties.put("CustomKey", "my attribute")

Custom properties aren;t present in application insights on request. image It works on Spans but not this custom request.

xsmrcek avatar Oct 13 '22 14:10 xsmrcek

Hi @trask , do we have any update here? It's kinda critical bug for me

xsmrcek avatar Oct 21 '22 09:10 xsmrcek

hey @xsmrcek can you update your repro with the kotlin changes, and I will re-test?

trask avatar Oct 21 '22 17:10 trask

Hi @trask , I updated it. Thank you!

xsmrcek avatar Oct 24 '22 06:10 xsmrcek

hey @xsmrcek, sorry for the slow feedback, I think I see the issue now.

here you are starting a span but not ending it

here you get the current span that you started above, but as an Application Insights Classic SDK RequestTelemetry instance

here you call trackRequest on that RequestTelemetry instance.

For starters, I would remove the trackRequest(), and add span.end().

I also had to change the TraceFlags here, because the test was setting TraceFlags.getDefault() which caused the span not to be recorded/exported.

Also, if you setSpanKind(SpanKind.SERVER) on the span you create, it will show up in the Application Insights requests table.

In general, I'd recommend using the OpenTelemetry API where possible and try not to mix in TelemetryClient unless necessary. It's not clear to me that you need TelemetryClient or associated ThreadContext in your example.

trask avatar Nov 05 '22 22:11 trask