open-telemetry / opentelemetry-android

OpenTelemetry Tooling for Android
Apache License 2.0
156 stars 41 forks source link

Not getting trace id and span id when application crash #684

Open tech-ind-02 opened 5 days ago

tech-ind-02 commented 5 days ago

Hello,

I am new to otel and android both. After running demo app i have followed steps to make application crash.

here i am getting logs in otel collector but not getting trace id and span id for same.

Pls help !!!

image

LikeTheSalad commented 4 days ago

Hi!

OpenTelemetry has 3 types of data, called signals, which are Traces, Metrics and Logs.

Trace id and Span id attrs are only present when there's a Span involved, and application crashes are sent using Logs instead.

However, Logs can get the span and trace id attrs if they are created inside a Span, for example:

// This function only sends a log and there are no spans involved, so the log won't get any span id nor trace id info:
fun sendLogOnly() {
  val openTelemetry: OpenTelemetry
  openTelemetry.logsBridge.get("myLogger").logRecordBuilder().setBody("Log body").emit()
}

// This other function creates a log with trace and span id:
fun sendLogWithTraceInfo() {
  val openTelemetry: OpenTelemetry
  val span = openTelemetry.getTracer("myTracer").spanBuilder("My Span").startSpan()
  span.makeCurrent().use {
        // This log is created inside a span, so it will get the trace attrs.
        openTelemetry.logsBridge.get("myLogger").logRecordBuilder().setBody("Log body").emit()
  }
  span.end()
}

For more info on how to create spans and logs, I'd suggest taking a look at the Java docs: https://opentelemetry.io/docs/languages/java/

OTel Android sends crashes as Log signals, which means that there has to be an active span in the same thread where the crash occurred for it to contain trace attrs. So you will probably get some crashes with this info and some others without it. In theory, the more spans you have around your app, the more chances there will be of crashes containing this info.

tech-ind-02 commented 23 hours ago

@LikeTheSalad Is it possible you to share new version of android agent library which will have span_id and trace_id. because seems changes required in instrumentation module.

LikeTheSalad commented 19 hours ago

I'm not sure I'm following.

If you'd like to see trace_id and span_id in logs you need to make sure to have a Span marked as "current", by calling span.makeCurrent() as I showed in my previous example. Logs on their own can't have a trace_id and span_id, that's something outside the scope of this project.

because seems changes required in instrumentation module

Can you please elaborate a bit more on what changes and where are you referring to?

tech-ind-02 commented 17 hours ago

@LikeTheSalad https://github.com/open-telemetry/opentelemetry-android/blob/main/instrumentation/crash/src/main/java/io/opentelemetry/android/instrumentation/crash/CrashReporter.java image

what change i need to do in above line.