Created
February 15, 2024 10:45
-
-
Save LikeTheSalad/162278712aa04add0f19f4c2c008e7b6 to your computer and use it in GitHub Desktop.
Configuring the OpenTelemetry Android lib (for its version `0.3.0`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is to enable the exporting of spans using OTLP over GRPC. | |
// The dependency "io.opentelemetry:opentelemetry-exporter-otlp" needs to be present in the | |
// host project in order to make this work, more info on this dependency in the OTel Java repo: https://github.com/open-telemetry/opentelemetry-java | |
Function<SpanExporter, SpanExporter> spanExporterCustomizer = new Function<>() { | |
@Override | |
public SpanExporter apply(SpanExporter spanExporter) { | |
return OtlpGrpcSpanExporter.builder() | |
.setEndpoint("https://my.server.endpoint") | |
.build(); | |
} | |
}; | |
// This is to make the metrics work (to get exported to your backend). | |
BiFunction<SdkMeterProviderBuilder, Application, SdkMeterProviderBuilder> metricsCustomizer = new BiFunction<>() { | |
@Override | |
public SdkMeterProviderBuilder apply(SdkMeterProviderBuilder builder, Application application) { | |
return builder.registerMetricReader(PeriodicMetricReader.create(OtlpGrpcMetricExporter.builder() | |
.setEndpoint("https://my.server.endpoint") | |
.build())); | |
} | |
}; | |
// This is to make logs work. | |
BiFunction<SdkLoggerProviderBuilder, Application, SdkLoggerProviderBuilder> logsCustomizer = new BiFunction<>() { | |
@Override | |
public SdkLoggerProviderBuilder apply(SdkLoggerProviderBuilder sdkLoggerProviderBuilder, Application application) { | |
return sdkLoggerProviderBuilder | |
.addLogRecordProcessor(BatchLogRecordProcessor.builder( | |
OtlpGrpcLogRecordExporter.builder() | |
.setEndpoint("https://my.server.endpoint") | |
.build() | |
).build()); | |
} | |
}; | |
// Initializing OpenTelemetry Android RUM: | |
OpenTelemetryRum openTelemetryRum = OpenTelemetryRum.builder(this /* Your Android application */) | |
.addSpanExporterCustomizer(spanExporterCustomizer) | |
.addMeterProviderCustomizer(metricsCustomizer) | |
.addLoggerProviderCustomizer(logsCustomizer) | |
.addInstrumentation(instrumentedApplication -> { | |
// This is to automatically send activity and fragment lifecycle spans. | |
new AndroidLifecycleInstrumentationBuilder() | |
.setVisibleScreenTracker(new VisibleScreenTracker()) | |
.setStartupTimer(new AppStartupTimer()) | |
.build().installOn(instrumentedApplication); | |
}) | |
.build(); | |
// Get the OpenTelemetry instance to start sending manual telemetry (more info on https://opentelemetry.io/docs/languages/java/instrumentation/) | |
OpenTelemetry openTelemetry = openTelemetryRum.getOpenTelemetry(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment