Skip to content

Commit

Permalink
Fix OpenTelemetry Metrics and Logs endpoint resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Feb 19, 2025
1 parent 540fe14 commit 19f2ec6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public MetricExporter apply(SyntheticCreationalContext<MetricExporter> context)

public Function<SyntheticCreationalContext<LogRecordExporter>, LogRecordExporter> createLogRecordExporter(
OTelRuntimeConfig otelRuntimeConfig, OtlpExporterRuntimeConfig exporterRuntimeConfig, Supplier<Vertx> vertx) {
final URI baseUri = getMetricsUri(exporterRuntimeConfig);
final URI baseUri = getLogsUri(exporterRuntimeConfig);

return new Function<>() {
@Override
Expand Down Expand Up @@ -398,7 +398,15 @@ private URI getTracesUri(OtlpExporterRuntimeConfig exporterRuntimeConfig) {
}

private URI getMetricsUri(OtlpExporterRuntimeConfig exporterRuntimeConfig) {
String endpoint = resolveTraceEndpoint(exporterRuntimeConfig);
String endpoint = resolveMetricEndpoint(exporterRuntimeConfig);
if (endpoint.isEmpty()) {
return null;
}
return ExporterBuilderUtil.validateEndpoint(endpoint);
}

private URI getLogsUri(OtlpExporterRuntimeConfig exporterRuntimeConfig) {
String endpoint = resolveLogsEndpoint(exporterRuntimeConfig);
if (endpoint.isEmpty()) {
return null;
}
Expand All @@ -423,6 +431,15 @@ static String resolveMetricEndpoint(final OtlpExporterRuntimeConfig runtimeConfi
return endpoint.trim();
}

static String resolveLogsEndpoint(final OtlpExporterRuntimeConfig runtimeConfig) {
String endpoint = runtimeConfig.logs().endpoint()
.filter(OTelExporterRecorder::excludeDefaultEndpoint)
.orElse(runtimeConfig.endpoint()
.filter(OTelExporterRecorder::excludeDefaultEndpoint)
.orElse(DEFAULT_GRPC_BASE_URI));
return endpoint.trim();
}

private static boolean excludeDefaultEndpoint(String endpoint) {
return !DEFAULT_GRPC_BASE_URI.equals(endpoint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@ public void resolveMetricEndpoint_testIsSet() {
null)));
}

@Test
public void resolveLogsEndpoint_newWins() {
assertEquals("http://localhost:2222/",
OTelExporterRecorder.resolveLogsEndpoint(createOtlpExporterRuntimeConfig(
"http://localhost:1111/",
"http://localhost:2222/")));
}

@Test
public void resolveLogsEndpoint_globalWins() {
assertEquals("http://localhost:1111/",
OTelExporterRecorder.resolveLogsEndpoint(createOtlpExporterRuntimeConfig(
"http://localhost:1111/",
DEFAULT_GRPC_BASE_URI)));
}

@Test
public void resolveLogsEndpoint_legacyTraceWins() {
assertEquals("http://localhost:2222/",
OTelExporterRecorder.resolveLogsEndpoint(createOtlpExporterRuntimeConfig(
DEFAULT_GRPC_BASE_URI,
"http://localhost:2222/")));
}

@Test
public void resolveLogsEndpoint_legacyGlobalWins() {
assertEquals(DEFAULT_GRPC_BASE_URI,
OTelExporterRecorder.resolveLogsEndpoint(createOtlpExporterRuntimeConfig(
DEFAULT_GRPC_BASE_URI,
null)));
}

@Test
public void resolveLogsEndpoint_testIsSet() {
assertEquals(DEFAULT_GRPC_BASE_URI,
OTelExporterRecorder.resolveLogsEndpoint(createOtlpExporterRuntimeConfig(
null,
null)));
}

private OtlpExporterRuntimeConfig createOtlpExporterRuntimeConfig(String exporterGlobal, String newTrace) {
return new OtlpExporterRuntimeConfig() {
@Override
Expand Down

0 comments on commit 19f2ec6

Please sign in to comment.