-
Notifications
You must be signed in to change notification settings - Fork 105
Agent initialization api #945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LikeTheSalad
merged 8 commits into
open-telemetry:main
from
LikeTheSalad:agent-initialization-api
May 5, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
222de61
Adding http endpoint connectivity
LikeTheSalad 1e96878
Creating OpenTelemetryRumInitializer
LikeTheSalad 15ca08f
Initializing OpenTelemetryRum
LikeTheSalad 9cb83aa
Clean up
LikeTheSalad 18a7f74
Using initializer in demo app
LikeTheSalad 2ae6bac
Adding docs
LikeTheSalad b9bcf82
Renaming test
LikeTheSalad c18acfe
Revert removing extensions
LikeTheSalad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
android-agent/src/main/kotlin/io/opentelemetry/android/agent/OpenTelemetryRumInitializer.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.agent | ||
|
|
||
| import android.app.Application | ||
| import io.opentelemetry.android.OpenTelemetryRum | ||
| import io.opentelemetry.android.OpenTelemetryRumBuilder | ||
| import io.opentelemetry.android.agent.connectivity.EndpointConnectivity | ||
| import io.opentelemetry.android.agent.connectivity.HttpEndpointConnectivity | ||
| import io.opentelemetry.android.config.OtelRumConfig | ||
| import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter | ||
| import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter | ||
| import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter | ||
|
|
||
| object OpenTelemetryRumInitializer { | ||
| /** | ||
| * Opinionated [OpenTelemetryRum] initialization. | ||
| * | ||
| * @param application Your android app's application object. | ||
| * @param endpointBaseUrl The base endpoint for exporting all your signals. | ||
| * @param endpointHeaders These will be added to each signal export request. | ||
| * @param spanEndpointConnectivity Span-specific endpoint configuration. | ||
| * @param logEndpointConnectivity Log-specific endpoint configuration. | ||
| * @param metricEndpointConnectivity Metric-specific endpoint configuration. | ||
| * @param rumConfig Configuration used by [OpenTelemetryRumBuilder]. | ||
| */ | ||
| @JvmStatic | ||
| fun initialize( | ||
| application: Application, | ||
| endpointBaseUrl: String, | ||
| endpointHeaders: Map<String, String> = emptyMap(), | ||
| spanEndpointConnectivity: EndpointConnectivity = | ||
| HttpEndpointConnectivity.forTraces( | ||
| endpointBaseUrl, | ||
| endpointHeaders, | ||
| ), | ||
| logEndpointConnectivity: EndpointConnectivity = | ||
| HttpEndpointConnectivity.forLogs( | ||
| endpointBaseUrl, | ||
| endpointHeaders, | ||
| ), | ||
| metricEndpointConnectivity: EndpointConnectivity = | ||
| HttpEndpointConnectivity.forMetrics( | ||
| endpointBaseUrl, | ||
| endpointHeaders, | ||
| ), | ||
| rumConfig: OtelRumConfig = OtelRumConfig(), | ||
| ): OpenTelemetryRum = | ||
| OpenTelemetryRum | ||
| .builder(application, rumConfig) | ||
| .addSpanExporterCustomizer { | ||
| OtlpHttpSpanExporter | ||
| .builder() | ||
| .setEndpoint(spanEndpointConnectivity.getUrl()) | ||
| .setHeaders(spanEndpointConnectivity::getHeaders) | ||
| .build() | ||
| }.addLogRecordExporterCustomizer { | ||
| OtlpHttpLogRecordExporter | ||
| .builder() | ||
| .setEndpoint(logEndpointConnectivity.getUrl()) | ||
| .setHeaders(logEndpointConnectivity::getHeaders) | ||
| .build() | ||
| }.addMetricExporterCustomizer { | ||
| OtlpHttpMetricExporter | ||
| .builder() | ||
| .setEndpoint(metricEndpointConnectivity.getUrl()) | ||
| .setHeaders(metricEndpointConnectivity::getHeaders) | ||
| .build() | ||
| }.build() | ||
| } |
12 changes: 12 additions & 0 deletions
12
...agent/src/main/kotlin/io/opentelemetry/android/agent/connectivity/EndpointConnectivity.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.agent.connectivity | ||
|
|
||
| interface EndpointConnectivity { | ||
| fun getUrl(): String | ||
|
|
||
| fun getHeaders(): Map<String, String> | ||
| } |
32 changes: 32 additions & 0 deletions
32
...t/src/main/kotlin/io/opentelemetry/android/agent/connectivity/HttpEndpointConnectivity.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.agent.connectivity | ||
|
|
||
| class HttpEndpointConnectivity private constructor( | ||
| private val url: String, | ||
| private val headers: Map<String, String>, | ||
| ) : EndpointConnectivity { | ||
| companion object { | ||
| fun forTraces( | ||
| baseUrl: String, | ||
| headers: Map<String, String> = emptyMap(), | ||
| ): HttpEndpointConnectivity = HttpEndpointConnectivity(baseUrl.trimEnd('/') + "/v1/traces", headers) | ||
|
|
||
| fun forLogs( | ||
| baseUrl: String, | ||
| headers: Map<String, String> = emptyMap(), | ||
| ): HttpEndpointConnectivity = HttpEndpointConnectivity(baseUrl.trimEnd('/') + "/v1/logs", headers) | ||
|
|
||
| fun forMetrics( | ||
| baseUrl: String, | ||
| headers: Map<String, String> = emptyMap(), | ||
| ): HttpEndpointConnectivity = HttpEndpointConnectivity(baseUrl.trimEnd('/') + "/v1/metrics", headers) | ||
| } | ||
|
|
||
| override fun getUrl(): String = url | ||
|
|
||
| override fun getHeaders(): Map<String, String> = headers | ||
| } |
27 changes: 27 additions & 0 deletions
27
...c/test/kotlin/io/opentelemetry/android/agent/connectivity/HttpEndpointConnectivityTest.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.agent.connectivity | ||
|
|
||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.Test | ||
|
|
||
| class HttpEndpointConnectivityTest { | ||
| @Test | ||
| fun `Validate exporter endpoint configuration`() { | ||
| val headers = mapOf("Authorization" to "Basic something") | ||
| val tracesConnectivity = HttpEndpointConnectivity.forTraces("http://some.endpoint", headers) | ||
| val logsConnectivity = HttpEndpointConnectivity.forLogs("http://some.endpoint/", headers) | ||
| val metricsConnectivity = | ||
| HttpEndpointConnectivity.forMetrics("http://some.endpoint", headers) | ||
|
|
||
| assertThat(tracesConnectivity.getUrl()).isEqualTo("http://some.endpoint/v1/traces") | ||
| assertThat(tracesConnectivity.getHeaders()).isEqualTo(headers) | ||
| assertThat(logsConnectivity.getUrl()).isEqualTo("http://some.endpoint/v1/logs") | ||
| assertThat(logsConnectivity.getHeaders()).isEqualTo(headers) | ||
| assertThat(metricsConnectivity.getUrl()).isEqualTo("http://some.endpoint/v1/metrics") | ||
| assertThat(metricsConnectivity.getHeaders()).isEqualTo(headers) | ||
| } | ||
| } |
This file contains hidden or 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 file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenTelemetryRumInitializeris an object (singleton) but looks like it should be just a function or extension?the single class holds no state or anything.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. The reason I went with an object is to try and provide a consistent experience for users calling it either from Kotlin or Java code. Though I'm open to other approaches. If we turned it into a standalone function, for example, would it be a way for people from Java to still call it this way:
OpenTelemetryRumInitializer.initialize(...)?We could also decide not to think/worry about Java usage apis, though I'd like to get some feedback on whether that could cause issues or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as a kotlin object, i think Java users have to do eg
OpenTelemetryRumInitializer.INSTANCE.initializeI think for
OpenTelemetryRumInitializer.initializeyou'd need a@JvmStaticannotation within a companion objectThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, though it seems to work with objects with
@JvmStaticannotations too, without having to be inside a companion object (at least that was the case when I tried it locally).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i was not sure about being within the companion object anyway, if it works, all good