Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
asafm committed Feb 19, 2024
1 parent d6f16f7 commit f45dc37
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ void setUp() {
@Test
void build_addMetricReaderCustomizerPrometheus() {
AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder();

builder.addPropertiesSupplier(() -> singletonMap("otel.metrics.exporter", "prometheus"));

int port = FreePortFinder.getFreePort();
builder.addMetricReaderCustomizer(
(reader, config) -> {
assertThat(reader).isInstanceOf(PrometheusHttpServer.class);
return PrometheusHttpServer.builder().setPort(1234).build();
return PrometheusHttpServer.builder().setPort(port).build();
});
builder.build();

WebClient client = WebClient.builder("http://localhost:1234").build();
assertThatCode(() -> client.get("/metrics")).doesNotThrowAnyException();
try (OpenTelemetrySdk ignored = builder.build().getOpenTelemetrySdk()) {
WebClient client = WebClient.builder("http://localhost:" + port).build();
assertThatCode(() -> client.get("/metrics")).doesNotThrowAnyException();
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.autoconfigure;

import java.io.IOException;
import java.net.ServerSocket;

final class FreePortFinder {

static int getFreePort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private FreePortFinder() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ void configureReader_PrometheusOnClasspath() {
}

@Test
void configureReader_customizeReader() {
void configureReader_customizeReader_prometheus() {
List<Closeable> closeables = new ArrayList<>();

int port = FreePortFinder.getFreePort();
BiFunction<MetricReader, ConfigProperties, MetricReader> readerCustomizer =
(existingReader, config) -> PrometheusHttpServer.builder().setPort(7137).build();
(existingReader, config) -> PrometheusHttpServer.builder().setPort(port).build();
MetricReader reader =
MetricExporterConfiguration.configureReader(
"prometheus", CONFIG_PROPERTIES, spiHelper, readerCustomizer, (a, b) -> a, closeables);
Expand All @@ -78,23 +79,25 @@ void configureReader_customizeReader() {
PrometheusHttpServer prometheusHttpServer = (PrometheusHttpServer) reader;
assertThat(prometheusHttpServer)
.extracting("httpServer", as(InstanceOfAssertFactories.type(HTTPServer.class)))
.satisfies(httpServer -> assertThat(httpServer.getPort()).isEqualTo(7137));
.satisfies(httpServer -> assertThat(httpServer.getPort()).isEqualTo(port));
}

List<Closeable> closeablesPart2 = new ArrayList<>();
@Test
void configureReader_customizeReader_otlp() {
List<Closeable> closeables = new ArrayList<>();

readerCustomizer =
BiFunction<MetricReader, ConfigProperties, MetricReader> readerCustomizer =
(existingReader, config) ->
PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build())
.setInterval(Duration.ofSeconds(123))
.build();

reader =
MetricReader reader =
MetricExporterConfiguration.configureReader(
"otlp", CONFIG_PROPERTIES, spiHelper, readerCustomizer, (a, b) -> a, closeablesPart2);
cleanup.addCloseables(closeablesPart2);
"otlp", CONFIG_PROPERTIES, spiHelper, readerCustomizer, (a, b) -> a, closeables);
cleanup.addCloseables(closeables);

assertThat(reader).isInstanceOf(PeriodicMetricReader.class);
assertThat(closeablesPart2).hasSize(3);
assertThat(closeables).hasSize(3);
PeriodicMetricReader periodicMetricReader = (PeriodicMetricReader) reader;
assertThat(periodicMetricReader)
.extracting("intervalNanos")
Expand Down

0 comments on commit f45dc37

Please sign in to comment.