Update the examples for the 1.7.0 release (#3719)
* WIP on example updates * update prometheus version and add more cycles to the otlp example * no spots here
This commit is contained in:
parent
a8a27f4d01
commit
efbdb55601
|
|
@ -4,8 +4,8 @@ plugins {
|
|||
}
|
||||
|
||||
ext {
|
||||
openTelemetryVersion = "1.6.0"
|
||||
openTelemetryAlphaVersion = "1.6.0-alpha"
|
||||
openTelemetryVersion = "1.7.0"
|
||||
openTelemetryAlphaVersion = "1.7.0-alpha"
|
||||
|
||||
grpcVersion = '1.34.1'
|
||||
protobufVersion = '3.11.4'
|
||||
|
|
|
|||
|
|
@ -5,10 +5,11 @@ import io.opentelemetry.exporter.logging.LoggingMetricExporter;
|
|||
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
|
||||
import io.opentelemetry.sdk.OpenTelemetrySdk;
|
||||
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
||||
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
|
||||
import io.opentelemetry.sdk.metrics.export.MetricReaderFactory;
|
||||
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
|
||||
import io.opentelemetry.sdk.trace.SdkTracerProvider;
|
||||
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
|
||||
import java.util.Collections;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* All SDK management takes place here, away from the instrumentation code, which should only access
|
||||
|
|
@ -25,16 +26,17 @@ public final class ExampleConfiguration {
|
|||
* @return A ready-to-use {@link OpenTelemetry} instance.
|
||||
*/
|
||||
public static OpenTelemetry initOpenTelemetry() {
|
||||
// This will be used to create instruments
|
||||
SdkMeterProvider meterProvider = SdkMeterProvider.builder().buildAndRegisterGlobal();
|
||||
// Create an instance of PeriodicMetricReaderFactory and configure it
|
||||
// to export via the logging exporter
|
||||
MetricReaderFactory periodicReaderFactory =
|
||||
PeriodicMetricReader.create(
|
||||
new LoggingMetricExporter(), Duration.ofMillis(METRIC_EXPORT_INTERVAL_MS));
|
||||
|
||||
// Create an instance of IntervalMetricReader and configure it
|
||||
// to read metrics from the meterProvider and export them to the logging exporter
|
||||
IntervalMetricReader.builder()
|
||||
.setMetricExporter(new LoggingMetricExporter())
|
||||
.setMetricProducers(Collections.singleton(meterProvider))
|
||||
.setExportIntervalMillis(METRIC_EXPORT_INTERVAL_MS)
|
||||
.build();
|
||||
// This will be used to create instruments
|
||||
SdkMeterProvider meterProvider =
|
||||
SdkMeterProvider.builder()
|
||||
.registerMetricReader(periodicReaderFactory)
|
||||
.buildAndRegisterGlobal();
|
||||
|
||||
// Tracer provider configured to export spans with SimpleSpanProcessor using
|
||||
// the logging exporter.
|
||||
|
|
|
|||
|
|
@ -12,10 +12,11 @@ import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
|
|||
import io.opentelemetry.sdk.OpenTelemetrySdk;
|
||||
import io.opentelemetry.sdk.autoconfigure.OpenTelemetryResourceAutoConfiguration;
|
||||
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
||||
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
|
||||
import io.opentelemetry.sdk.metrics.export.MetricReaderFactory;
|
||||
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
|
||||
import io.opentelemetry.sdk.trace.SdkTracerProvider;
|
||||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
|
||||
import java.util.Collections;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +46,7 @@ public final class ExampleConfiguration {
|
|||
OpenTelemetrySdk openTelemetrySdk =
|
||||
OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).buildAndRegisterGlobal();
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(tracerProvider::close));
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(tracerProvider::shutdown));
|
||||
|
||||
return openTelemetrySdk;
|
||||
}
|
||||
|
|
@ -59,16 +60,15 @@ public final class ExampleConfiguration {
|
|||
// set up the metric exporter and wire it into the SDK and a timed reader.
|
||||
OtlpGrpcMetricExporter metricExporter = OtlpGrpcMetricExporter.getDefault();
|
||||
|
||||
SdkMeterProvider meterProvider = SdkMeterProvider.builder().buildAndRegisterGlobal();
|
||||
IntervalMetricReader intervalMetricReader =
|
||||
IntervalMetricReader.builder()
|
||||
.setMetricExporter(metricExporter)
|
||||
.setMetricProducers(Collections.singleton(meterProvider))
|
||||
.setExportIntervalMillis(1000)
|
||||
.buildAndStart();
|
||||
MetricReaderFactory periodicReaderFactory =
|
||||
PeriodicMetricReader.create(metricExporter, Duration.ofMillis(1000));
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(intervalMetricReader::shutdown));
|
||||
SdkMeterProvider sdkMeterProvider =
|
||||
SdkMeterProvider.builder()
|
||||
.registerMetricReader(periodicReaderFactory)
|
||||
.buildAndRegisterGlobal();
|
||||
|
||||
return meterProvider;
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::shutdown));
|
||||
return sdkMeterProvider;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ public final class OtlpExporterExample {
|
|||
LongCounter counter = meter.counterBuilder("example_counter").build();
|
||||
LongHistogram recorder = meter.histogramBuilder("super_timer").ofLongs().setUnit("ms").build();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Span exampleSpan = tracer.spanBuilder("exampleSpan").startSpan();
|
||||
try (Scope scope = exampleSpan.makeCurrent()) {
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ ext.moduleName = "io.opentelemetry.examples.prometheus"
|
|||
dependencies {
|
||||
implementation("io.opentelemetry:opentelemetry-api")
|
||||
implementation("io.opentelemetry:opentelemetry-sdk")
|
||||
implementation("io.prometheus:simpleclient:0.8.1")
|
||||
implementation("io.prometheus:simpleclient_httpserver:0.8.1")
|
||||
implementation("io.prometheus:simpleclient:0.12.0")
|
||||
implementation("io.prometheus:simpleclient_httpserver:0.12.0")
|
||||
|
||||
//alpha modules
|
||||
implementation("io.opentelemetry:opentelemetry-exporter-prometheus")
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ package io.opentelemetry.example.prometheus;
|
|||
import io.opentelemetry.api.metrics.MeterProvider;
|
||||
import io.opentelemetry.exporter.prometheus.PrometheusCollector;
|
||||
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
||||
import io.opentelemetry.sdk.metrics.export.MetricReaderFactory;
|
||||
import io.prometheus.client.exporter.HTTPServer;
|
||||
import java.io.IOException;
|
||||
|
||||
|
|
@ -21,12 +22,14 @@ public final class ExampleConfiguration {
|
|||
* @return A MeterProvider for use in instrumentation.
|
||||
*/
|
||||
static MeterProvider initializeOpenTelemetry(int prometheusPort) throws IOException {
|
||||
SdkMeterProvider meterProvider = SdkMeterProvider.builder().buildAndRegisterGlobal();
|
||||
MetricReaderFactory prometheusReaderFactory = PrometheusCollector.create();
|
||||
|
||||
PrometheusCollector.builder().setMetricProducer(meterProvider).buildAndRegister();
|
||||
SdkMeterProvider meterProvider =
|
||||
SdkMeterProvider.builder()
|
||||
.registerMetricReader(prometheusReaderFactory)
|
||||
.buildAndRegisterGlobal();
|
||||
|
||||
server = new HTTPServer(prometheusPort);
|
||||
|
||||
return meterProvider;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue