Skip to content
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

Adding http exporting path #319

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ ${next_release_notes}
==== ${version} - ${release_date}

[float]
===== Features
===== Bug fixes

* New feature: {pull}000[#000]
* HTTP exporting fix: {pull}319[#319]
////

[[release-notes-0.17.0]]
Expand Down
2 changes: 2 additions & 0 deletions android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ dependencies {
compileOnly 'co.elastic.apm.compile:processor'
testImplementation libs.bundles.mocking
testImplementation libs.junit
testImplementation libs.mockwebserver
testImplementation libs.opentelemetry.testing
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public final class DefaultSignalConfiguration extends DefaultSignalProcessorConf
private final Provider<ConnectivityConfiguration> connectivity;

public DefaultSignalConfiguration() {
connectivity = LazyProvider.of(() -> Configurations.get(ConnectivityConfiguration.class));
this(LazyProvider.of(() -> Configurations.get(ConnectivityConfiguration.class)));
}

DefaultSignalConfiguration(Provider<ConnectivityConfiguration> connectivity) {
this.connectivity = connectivity;
}

@Override
Expand Down Expand Up @@ -117,7 +121,7 @@ private OtlpGrpcMetricExporter getOtlpGrpcMetricExporter() {

@NonNull
private OtlpHttpSpanExporter getOtlpHttpSpanExporter() {
OtlpHttpSpanExporterBuilder exporterBuilder = OtlpHttpSpanExporter.builder().setEndpoint(getConnectivity().getEndpoint());
OtlpHttpSpanExporterBuilder exporterBuilder = OtlpHttpSpanExporter.builder().setEndpoint(getHttpEndpoint("traces"));
if (getConnectivity().getAuthConfiguration() != null) {
exporterBuilder.addHeader(AUTHORIZATION_HEADER_NAME, getAuthorizationHeaderValue());
}
Expand All @@ -126,7 +130,7 @@ private OtlpHttpSpanExporter getOtlpHttpSpanExporter() {

@NonNull
private OtlpHttpLogRecordExporter getOtlpHttpLogRecordExporter() {
OtlpHttpLogRecordExporterBuilder exporterBuilder = OtlpHttpLogRecordExporter.builder().setEndpoint(getConnectivity().getEndpoint());
OtlpHttpLogRecordExporterBuilder exporterBuilder = OtlpHttpLogRecordExporter.builder().setEndpoint(getHttpEndpoint("logs"));
if (getConnectivity().getAuthConfiguration() != null) {
exporterBuilder.addHeader(AUTHORIZATION_HEADER_NAME, getAuthorizationHeaderValue());
}
Expand All @@ -137,7 +141,7 @@ private OtlpHttpLogRecordExporter getOtlpHttpLogRecordExporter() {
private OtlpHttpMetricExporter getOtlpHttpMetricExporter() {
OtlpHttpMetricExporterBuilder exporterBuilder = OtlpHttpMetricExporter.builder()
.setAggregationTemporalitySelector(AggregationTemporalitySelector.deltaPreferred())
.setEndpoint(getConnectivity().getEndpoint());
.setEndpoint(getHttpEndpoint("metrics"));
if (getConnectivity().getAuthConfiguration() != null) {
exporterBuilder.addHeader(AUTHORIZATION_HEADER_NAME, getAuthorizationHeaderValue());
}
Expand All @@ -152,4 +156,8 @@ private ConnectivityConfiguration getConnectivity() {
private String getAuthorizationHeaderValue() {
return getConnectivity().getAuthConfiguration().asAuthorizationHeaderValue();
}

private String getHttpEndpoint(String signalId) {
return String.format("%s/v1/%s", getConnectivity().getEndpoint(), signalId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.android.sdk.connectivity.opentelemetry;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import co.elastic.apm.android.sdk.connectivity.ExportProtocol;
import co.elastic.apm.android.sdk.internal.configuration.impl.ConnectivityConfiguration;
import co.elastic.apm.android.sdk.testutils.providers.SimpleProvider;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.logs.TestLogRecordData;
import io.opentelemetry.sdk.testing.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

public class DefaultSignalConfigurationTest {

private MockWebServer server;
private ConnectivityConfiguration connectivityConfiguration;
private DefaultSignalConfiguration signalConfiguration;

@Before
public void setUp() throws IOException {
server = new MockWebServer();
server.start();
server.enqueue(new MockResponse());

connectivityConfiguration = mock();
doReturn("http://" + server.getHostName() + ":" + server.getPort()).when(connectivityConfiguration).getEndpoint();
signalConfiguration = new DefaultSignalConfiguration(SimpleProvider.create(connectivityConfiguration));
}

@Test
public void testSpansHttpEndpoint() throws InterruptedException {
doReturn(ExportProtocol.HTTP).when(connectivityConfiguration).getExportProtocol();
TestSpanData spanData = getTestSpanData();

signalConfiguration.provideSpanExporter().export(Collections.singleton(spanData)).join(1, TimeUnit.SECONDS);

RecordedRequest recordedRequest = server.takeRequest();
assertEquals("/v1/traces", recordedRequest.getPath());
}

@Test
public void testMetricsHttpEndpoint() throws InterruptedException {
doReturn(ExportProtocol.HTTP).when(connectivityConfiguration).getExportProtocol();
MetricData metricData = getTestMetricData();

signalConfiguration.provideMetricExporter().export(Collections.singleton(metricData)).join(1, TimeUnit.SECONDS);

RecordedRequest recordedRequest = server.takeRequest();
assertEquals("/v1/metrics", recordedRequest.getPath());
}

@Test
public void testLogRecordsHttpEndpoint() throws InterruptedException {
doReturn(ExportProtocol.HTTP).when(connectivityConfiguration).getExportProtocol();
TestLogRecordData logRecordData = TestLogRecordData.builder().setBody("Log body").build();

signalConfiguration.provideLogExporter().export(Collections.singleton(logRecordData)).join(1, TimeUnit.SECONDS);

RecordedRequest recordedRequest = server.takeRequest();
assertEquals("/v1/logs", recordedRequest.getPath());
}

private static TestSpanData getTestSpanData() {
return TestSpanData.builder().setName("Some name")
.setStartEpochNanos(1000)
.setEndEpochNanos(1100)
.setHasEnded(true)
.setStatus(StatusData.ok())
.setKind(SpanKind.CLIENT)
.build();
}

private static MetricData getTestMetricData() {
return ImmutableMetricData.createLongSum(Resource.empty(),
InstrumentationScopeInfo.empty(),
"A Metric",
"A description",
"m",
ImmutableSumData.empty());
}

@After
public void tearDown() throws IOException {
server.shutdown();
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ kotlin-coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }
mockito-inline = { module = "org.mockito:mockito-inline", version.ref = "mockito" }
junit = "junit:junit:4.13.2"
mockwebserver = "com.squareup.okhttp3:mockwebserver:4.12.0"
opentelemetry-testing = { module = "io.opentelemetry:opentelemetry-sdk-testing", version.ref = "opentelemetry" }

#Compilation tools
apache-commons-text = "org.apache.commons:commons-text:1.10.0"
Expand Down
Loading