forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement prometheus exporter provider (open-telemetry#5053)
* Implement prometheus exporter provider * Remove stray comment * Adjust method visibility * Delete ClasspathUtil
- Loading branch information
Showing
12 changed files
with
287 additions
and
151 deletions.
There are no files selected for viewing
This file contains 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
49 changes: 49 additions & 0 deletions
49
...main/java/io/opentelemetry/exporter/prometheus/internal/PrometheusCustomizerProvider.java
This file contains 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,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.prometheus.internal; | ||
|
||
import io.opentelemetry.exporter.prometheus.PrometheusHttpServer; | ||
import io.opentelemetry.exporter.prometheus.PrometheusHttpServerBuilder; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
|
||
/** | ||
* SPI implementation for {@link PrometheusHttpServer}. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public class PrometheusCustomizerProvider implements AutoConfigurationCustomizerProvider { | ||
|
||
@Override | ||
public void customize(AutoConfigurationCustomizer autoConfiguration) { | ||
autoConfiguration.addMeterProviderCustomizer( | ||
(builder, config) -> { | ||
boolean prometheusEnabled = | ||
config.getList("otel.metrics.exporter").contains("prometheus"); | ||
if (prometheusEnabled) { | ||
builder.registerMetricReader(configurePrometheusHttpServer(config)); | ||
} | ||
return builder; | ||
}); | ||
} | ||
|
||
// Visible for test | ||
static PrometheusHttpServer configurePrometheusHttpServer(ConfigProperties config) { | ||
PrometheusHttpServerBuilder prometheusBuilder = PrometheusHttpServer.builder(); | ||
|
||
Integer port = config.getInt("otel.exporter.prometheus.port"); | ||
if (port != null) { | ||
prometheusBuilder.setPort(port); | ||
} | ||
String host = config.getString("otel.exporter.prometheus.host"); | ||
if (host != null) { | ||
prometheusBuilder.setHost(host); | ||
} | ||
return prometheusBuilder.build(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...A-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider
This file contains 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 @@ | ||
io.opentelemetry.exporter.prometheus.internal.PrometheusCustomizerProvider |
134 changes: 134 additions & 0 deletions
134
.../java/io/opentelemetry/exporter/prometheus/internal/PrometheusCustomizerProviderTest.java
This file contains 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,134 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.prometheus.internal; | ||
|
||
import static org.assertj.core.api.Assertions.as; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
import io.opentelemetry.exporter.prometheus.PrometheusHttpServer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
class PrometheusCustomizerProviderTest { | ||
|
||
private static final PrometheusCustomizerProvider provider = new PrometheusCustomizerProvider(); | ||
|
||
private SdkMeterProviderBuilder meterProviderBuilder; | ||
|
||
@Mock private ConfigProperties configProperties; | ||
|
||
@Mock private AutoConfigurationCustomizer customizer; | ||
|
||
@BeforeEach | ||
void setup() { | ||
meterProviderBuilder = SdkMeterProvider.builder(); | ||
doAnswer( | ||
invocation -> { | ||
BiFunction<SdkMeterProviderBuilder, ConfigProperties, SdkMeterProviderBuilder> | ||
meterProviderCustomizer = invocation.getArgument(0); | ||
meterProviderBuilder = | ||
meterProviderCustomizer.apply(meterProviderBuilder, configProperties); | ||
return null; | ||
}) | ||
.when(customizer) | ||
.addMeterProviderCustomizer(any()); | ||
} | ||
|
||
@Test | ||
void customize_PrometheusEnabled() { | ||
when(configProperties.getList("otel.metrics.exporter")) | ||
.thenReturn(Collections.singletonList("prometheus")); | ||
provider.customize(customizer); | ||
|
||
try (SdkMeterProvider meterProvider = meterProviderBuilder.build()) { | ||
assertThat(meterProvider) | ||
.extracting("registeredReaders", as(InstanceOfAssertFactories.list(Object.class))) | ||
.satisfiesExactly( | ||
registeredReader -> | ||
assertThat(registeredReader) | ||
.extracting("metricReader") | ||
.isInstanceOf(PrometheusHttpServer.class)); | ||
} | ||
} | ||
|
||
@Test | ||
void customize_PrometheusDisabled() { | ||
when(configProperties.getList("otel.metrics.exporter")) | ||
.thenReturn(Collections.singletonList("foo")); | ||
provider.customize(customizer); | ||
|
||
try (SdkMeterProvider meterProvider = meterProviderBuilder.build()) { | ||
assertThat(meterProvider) | ||
.extracting("registeredReaders", as(InstanceOfAssertFactories.list(Object.class))) | ||
.isEmpty(); | ||
} | ||
} | ||
|
||
@Test | ||
void configurePrometheusHttpServer_Default() { | ||
try (PrometheusHttpServer prometheusHttpServer = | ||
PrometheusCustomizerProvider.configurePrometheusHttpServer( | ||
DefaultConfigProperties.createForTest(Collections.emptyMap()))) { | ||
assertThat(prometheusHttpServer) | ||
.extracting("server", as(InstanceOfAssertFactories.type(HttpServer.class))) | ||
.satisfies( | ||
server -> { | ||
assertThat(server.getAddress().getHostName()).isEqualTo("0:0:0:0:0:0:0:0"); | ||
assertThat(server.getAddress().getPort()).isEqualTo(9464); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
void configurePrometheusHttpServer_WithConfiguration() throws IOException { | ||
// Find a random unused port. There's a small race if another process takes it before we | ||
// initialize. Consider adding retries to this test if it flakes, presumably it never will on | ||
// CI since there's no prometheus there blocking the well-known port. | ||
int port; | ||
try (ServerSocket socket2 = new ServerSocket(0)) { | ||
port = socket2.getLocalPort(); | ||
} | ||
|
||
Map<String, String> config = new HashMap<>(); | ||
config.put("otel.exporter.prometheus.host", "localhost"); | ||
config.put("otel.exporter.prometheus.port", String.valueOf(port)); | ||
|
||
try (PrometheusHttpServer prometheusHttpServer = | ||
PrometheusCustomizerProvider.configurePrometheusHttpServer( | ||
DefaultConfigProperties.createForTest(config))) { | ||
assertThat(prometheusHttpServer) | ||
.extracting("server", as(InstanceOfAssertFactories.type(HttpServer.class))) | ||
.satisfies( | ||
server -> { | ||
assertThat(server.getAddress().getHostName()).isEqualTo("localhost"); | ||
assertThat(server.getAddress().getPort()).isEqualTo(port); | ||
}); | ||
} | ||
} | ||
} |
This file contains 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
27 changes: 0 additions & 27 deletions
27
...ensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ClasspathUtil.java
This file was deleted.
Oops, something went wrong.
This file contains 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 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
Oops, something went wrong.