Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,57 @@ public OtlpExporterConfig(
OtlpEndpointConfig endpoint = options.endpoint;
URI location = endpoint.location;
OtlpOverridesConfig overrides = endpoint.overrides;
this.metrics = location.resolve(overrides.metrics);
this.logs = location.resolve(overrides.logs);
this.metrics = resolveOverride(location, overrides.metrics);
this.logs = resolveOverride(location, overrides.logs);
}

private URI resolveOverride(
URI location,
String override)
{
URI overrideURI = URI.create(override);

if (overrideURI.isAbsolute() || overrideURI.getAuthority() != null)
{
return overrideURI;
}

String basePath = location.getPath();
if (basePath == null || basePath.isEmpty())
{
basePath = "/";
}

if (!basePath.endsWith("/"))
{
basePath += "/";
}

String overridePath = override;
if (overridePath.startsWith("/"))
{
overridePath = overridePath.substring(1);
}

try
{
URI normalizedBase = new URI(
location.getScheme(),
location.getUserInfo(),
location.getHost(),
location.getPort(),
basePath,
location.getQuery(),
location.getFragment()
);

return normalizedBase.resolve(overridePath);
}
catch (Exception ex)
{
LangUtil.rethrowUnchecked(ex);
return location;
}
}

public HttpClient supplyHttpClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package io.aklivity.zilla.runtime.exporter.otlp.internal.config;

import static io.aklivity.zilla.runtime.exporter.otlp.config.OtlpOptionsConfig.OtlpSignalsConfig.METRICS;
import static io.aklivity.zilla.runtime.exporter.otlp.config.OtlpOptionsConfig.OtlpSignalsConfig.LOGS;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -84,8 +85,8 @@ public void shouldOverrideAbsoluteMetricsUrl()
.protocol("http")
.location(URI.create("http://example.com"))
.overrides()
.metrics(URI.create("http://overridden.com/metrics"))
.logs(URI.create("http://overridden.com/logs"))
.metrics("http://overridden.com/metrics")
.logs("http://overridden.com/logs")
.build()
.build()
.build();
Expand Down Expand Up @@ -117,8 +118,8 @@ public void shouldOverrideRelativeMetricsUrl()
.protocol("http")
.location(URI.create("http://example.com"))
.overrides()
.metrics(URI.create("/v42/metrix"))
.logs(URI.create("/v42/logz"))
.metrics("/v42/metrix")
.logs("/v42/logz")
.build()
.build()
.build();
Expand All @@ -138,4 +139,103 @@ public void shouldOverrideRelativeMetricsUrl()
assertThat(metrics, equalTo(URI.create("http://example.com/v42/metrix")));
assertThat(logs, equalTo(URI.create("http://example.com/v42/logz")));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for full URI override, including scheme host and port that differ from base location.

@Test
public void shouldAppendPathsWithNonRootBase()
{
// GIVEN
OtlpOptionsConfig options = OtlpOptionsConfig.builder()
.interval(Duration.ofSeconds(30L))
.signals(Set.of(METRICS))
.endpoint()
.protocol("http")
.location(URI.create("http://localhost:8080/telemetry"))
.overrides()
.metrics("/v1/metrics")
.logs("/v1/logs")
.build()
.build()
.build();
ExporterConfig exporter = ExporterConfig.builder()
.namespace("test")
.name("oltp0")
.type("oltp")
.options(options)
.build();
OtlpExporterConfig oltpExporter = new OtlpExporterConfig(config, context, exporter);

// WHEN
URI metrics = oltpExporter.metrics;
URI logs = oltpExporter.logs;

// THEN
assertThat(metrics, equalTo(URI.create("http://localhost:8080/telemetry/v1/metrics")));
assertThat(logs, equalTo(URI.create("http://localhost:8080/telemetry/v1/logs")));
}

@Test
public void shouldHandlePathsWithoutLeadingSlash()
{
// GIVEN
OtlpOptionsConfig options = OtlpOptionsConfig.builder()
.interval(Duration.ofSeconds(30L))
.signals(Set.of(METRICS))
.endpoint()
.protocol("http")
.location(URI.create("http://localhost:8080/telemetry"))
.overrides()
.metrics("v1/metrics")
.logs("v1/logs")
.build()
.build()
.build();
ExporterConfig exporter = ExporterConfig.builder()
.namespace("test")
.name("oltp0")
.type("oltp")
.options(options)
.build();
OtlpExporterConfig oltpExporter = new OtlpExporterConfig(config, context, exporter);

// WHEN
URI metrics = oltpExporter.metrics;
URI logs = oltpExporter.logs;

// THEN
assertThat(metrics, equalTo(URI.create("http://localhost:8080/telemetry/v1/metrics")));
assertThat(logs, equalTo(URI.create("http://localhost:8080/telemetry/v1/logs")));
}

@Test
public void shouldAllowFullUriOverrideWithNonRootBase()
{
// GIVEN
OtlpOptionsConfig options = OtlpOptionsConfig.builder()
.interval(Duration.ofSeconds(30L))
.signals(Set.of(METRICS, LOGS))
.endpoint()
.protocol("http")
.location(URI.create("http://localhost:8080/telemetry"))
.overrides()
.metrics("https://metrics-endpoint.com:9090/api/metrics")
.logs("http://logs.other.com/v1/post")
.build()
.build()
.build();
ExporterConfig exporter = ExporterConfig.builder()
.namespace("test")
.name("oltp0")
.type("oltp")
.options(options)
.build();
OtlpExporterConfig oltpExporter = new OtlpExporterConfig(config, context, exporter);

// WHEN
URI metrics = oltpExporter.metrics;
URI logs = oltpExporter.logs;

// THEN
assertThat(metrics, equalTo(URI.create("https://metrics-endpoint.com:9090/api/metrics")));
assertThat(logs, equalTo(URI.create("http://logs.other.com/v1/post")));
}
}
Loading