-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration snippets for opentelemetry.io (#455)
- Loading branch information
Showing
35 changed files
with
942 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
id("java") | ||
} | ||
|
||
val moduleName by extra { "io.opentelemetry.examples.docs.configuration" } | ||
|
||
dependencies { | ||
implementation("io.opentelemetry:opentelemetry-sdk") | ||
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
|
||
implementation("io.opentelemetry:opentelemetry-exporter-otlp") | ||
implementation("io.opentelemetry:opentelemetry-exporter-logging") | ||
implementation("io.opentelemetry:opentelemetry-exporter-logging-otlp") | ||
implementation("io.opentelemetry:opentelemetry-exporter-zipkin") | ||
implementation("io.opentelemetry:opentelemetry-exporter-prometheus") | ||
implementation("io.opentelemetry:opentelemetry-sdk-extension-jaeger-remote-sampler") | ||
|
||
implementation("io.opentelemetry.semconv:opentelemetry-semconv") | ||
implementation("io.opentelemetry.semconv:opentelemetry-semconv-incubating:1.26.0-alpha") | ||
} |
10 changes: 10 additions & 0 deletions
10
doc-snippets/configuration/src/main/java/otel/AutoConfiguredSdk.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,10 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; | ||
|
||
public class AutoConfiguredSdk { | ||
public static OpenTelemetrySdk autoconfiguredSdk() { | ||
return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
doc-snippets/configuration/src/main/java/otel/ContextPropagatorsConfig.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,14 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.context.propagation.TextMapPropagator; | ||
|
||
public class ContextPropagatorsConfig { | ||
public static ContextPropagators create() { | ||
return ContextPropagators.create( | ||
TextMapPropagator.composite( | ||
W3CTraceContextPropagator.getInstance(), W3CBaggagePropagator.getInstance())); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
doc-snippets/configuration/src/main/java/otel/CustomLogRecordExporter.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,36 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.logs.data.LogRecordData; | ||
import io.opentelemetry.sdk.logs.export.LogRecordExporter; | ||
import java.util.Collection; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class CustomLogRecordExporter implements LogRecordExporter { | ||
|
||
private static final Logger logger = Logger.getLogger(CustomLogRecordExporter.class.getName()); | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<LogRecordData> logs) { | ||
// Export the records. Typically, records are sent out of process via some network protocol, but | ||
// we simply log for illustrative purposes. | ||
System.out.println("Exporting logs"); | ||
logs.forEach(log -> System.out.println("log record: " + log)); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
// Export any records which have been queued up but not yet exported. | ||
logger.log(Level.INFO, "flushing"); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
// Shutdown the exporter and cleanup any resources. | ||
logger.log(Level.INFO, "shutting down"); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
doc-snippets/configuration/src/main/java/otel/CustomLogRecordExporterProvider.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,19 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider; | ||
import io.opentelemetry.sdk.logs.export.LogRecordExporter; | ||
|
||
public class CustomLogRecordExporterProvider implements ConfigurableLogRecordExporterProvider { | ||
|
||
@Override | ||
public LogRecordExporter createExporter(ConfigProperties config) { | ||
// Callback invoked when OTEL_LOGS_EXPORTER includes the value from getName(). | ||
return new CustomLogRecordExporter(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "custom-exporter"; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
doc-snippets/configuration/src/main/java/otel/CustomLogRecordProcessor.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,29 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.logs.LogRecordProcessor; | ||
import io.opentelemetry.sdk.logs.ReadWriteLogRecord; | ||
|
||
public class CustomLogRecordProcessor implements LogRecordProcessor { | ||
|
||
@Override | ||
public void onEmit(Context context, ReadWriteLogRecord logRecord) { | ||
// Callback invoked when log record is emitted. | ||
// Enrich the record with a custom attribute. | ||
logRecord.setAttribute(AttributeKey.stringKey("my.custom.attribute"), "hello world"); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
// Optionally shutdown the processor and cleanup any resources. | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode forceFlush() { | ||
// Optionally process any records which have been queued up but not yet processed. | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
doc-snippets/configuration/src/main/java/otel/CustomMetricExporter.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,61 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.common.export.MemoryMode; | ||
import io.opentelemetry.sdk.metrics.Aggregation; | ||
import io.opentelemetry.sdk.metrics.InstrumentType; | ||
import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
import io.opentelemetry.sdk.metrics.data.MetricData; | ||
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector; | ||
import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
import java.util.Collection; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class CustomMetricExporter implements MetricExporter { | ||
|
||
private static final Logger logger = Logger.getLogger(CustomMetricExporter.class.getName()); | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<MetricData> metrics) { | ||
// Export the records. Typically, records are sent out of process via some network protocol, but | ||
// we simply log for illustrative purposes. | ||
logger.log(Level.INFO, "Exporting metrics"); | ||
metrics.forEach(metric -> logger.log(Level.INFO, "Metric: " + metric)); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
// Export any records which have been queued up but not yet exported. | ||
logger.log(Level.INFO, "flushing"); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
// Shutdown the exporter and cleanup any resources. | ||
logger.log(Level.INFO, "shutting down"); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) { | ||
// Specify the required aggregation temporality as a function of instrument type | ||
return AggregationTemporalitySelector.deltaPreferred() | ||
.getAggregationTemporality(instrumentType); | ||
} | ||
|
||
@Override | ||
public MemoryMode getMemoryMode() { | ||
// Optionally specify the memory mode, indicating whether metric records can be reused or must | ||
// be immutable | ||
return MemoryMode.REUSABLE_DATA; | ||
} | ||
|
||
@Override | ||
public Aggregation getDefaultAggregation(InstrumentType instrumentType) { | ||
// Optionally specify the default aggregation as a function of instrument kind | ||
return Aggregation.defaultAggregation(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
doc-snippets/configuration/src/main/java/otel/CustomMetricExporterProvider.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,19 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider; | ||
import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
|
||
public class CustomMetricExporterProvider implements ConfigurableMetricExporterProvider { | ||
|
||
@Override | ||
public MetricExporter createExporter(ConfigProperties config) { | ||
// Callback invoked when OTEL_METRICS_EXPORTER includes the value from getName(). | ||
return new CustomMetricExporter(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "custom-exporter"; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
doc-snippets/configuration/src/main/java/otel/CustomMetricReader.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,76 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.common.export.MemoryMode; | ||
import io.opentelemetry.sdk.metrics.Aggregation; | ||
import io.opentelemetry.sdk.metrics.InstrumentType; | ||
import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector; | ||
import io.opentelemetry.sdk.metrics.export.CollectionRegistration; | ||
import io.opentelemetry.sdk.metrics.export.MetricReader; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class CustomMetricReader implements MetricReader { | ||
|
||
private static final Logger logger = Logger.getLogger(CustomMetricExporter.class.getName()); | ||
|
||
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); | ||
private final AtomicReference<CollectionRegistration> collectionRef = | ||
new AtomicReference<>(CollectionRegistration.noop()); | ||
|
||
@Override | ||
public void register(CollectionRegistration collectionRegistration) { | ||
// Callback invoked when SdkMeterProvider is initialized, providing a handle to collect metrics. | ||
collectionRef.set(collectionRegistration); | ||
executorService.scheduleWithFixedDelay(this::collectMetrics, 0, 60, TimeUnit.SECONDS); | ||
} | ||
|
||
private void collectMetrics() { | ||
// Collect metrics. Typically, records are sent out of process via some network protocol, but we | ||
// simply log for illustrative purposes. | ||
logger.log(Level.INFO, "Collecting metrics"); | ||
collectionRef | ||
.get() | ||
.collectAllMetrics() | ||
.forEach(metric -> logger.log(Level.INFO, "Metric: " + metric)); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode forceFlush() { | ||
// Export any records which have been queued up but not yet exported. | ||
logger.log(Level.INFO, "flushing"); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
// Shutdown the exporter and cleanup any resources. | ||
logger.log(Level.INFO, "shutting down"); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) { | ||
// Specify the required aggregation temporality as a function of instrument type | ||
return AggregationTemporalitySelector.deltaPreferred() | ||
.getAggregationTemporality(instrumentType); | ||
} | ||
|
||
@Override | ||
public MemoryMode getMemoryMode() { | ||
// Optionally specify the memory mode, indicating whether metric records can be reused or must | ||
// be immutable | ||
return MemoryMode.REUSABLE_DATA; | ||
} | ||
|
||
@Override | ||
public Aggregation getDefaultAggregation(InstrumentType instrumentType) { | ||
// Optionally specify the default aggregation as a function of instrument kind | ||
return Aggregation.defaultAggregation(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
doc-snippets/configuration/src/main/java/otel/CustomResourceProvider.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,20 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
|
||
public class CustomResourceProvider implements ResourceProvider { | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties config) { | ||
// Callback invoked to contribute to the resource. | ||
return Resource.builder().put("my.custom.resource.attribute", "abc123").build(); | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// Optionally influence the order of invocation. | ||
return 0; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
doc-snippets/configuration/src/main/java/otel/CustomSampler.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,38 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.data.LinkData; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
import java.util.List; | ||
|
||
public class CustomSampler implements Sampler { | ||
@Override | ||
public SamplingResult shouldSample( | ||
Context parentContext, | ||
String traceId, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks) { | ||
// Callback invoked when span is started, before any SpanProcessor is called. | ||
// If the SamplingDecision is: | ||
// - DROP: the span is dropped. A valid span context is created and SpanProcessor#onStart is | ||
// still called, but no data is recorded and SpanProcessor#onEnd is not called. | ||
// - RECORD_ONLY: the span is recorded but not sampled. Data is recorded to the span, | ||
// SpanProcessor#onStart and SpanProcessor#onEnd are called, but the span's sampled status | ||
// indicates it should not be exported out of process. | ||
// - RECORD_AND_SAMPLE: the span is recorded and sampled. Data is recorded to the span, | ||
// SpanProcessor#onStart and SpanProcessor#onEnd are called, and the span's sampled status | ||
// indicates it should be exported out of process. | ||
return SpanKind.SERVER == spanKind ? SamplingResult.recordAndSample() : SamplingResult.drop(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
// Return a description of the sampler. | ||
return this.getClass().getSimpleName(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
doc-snippets/configuration/src/main/java/otel/CustomSamplerProvider.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,19 @@ | ||
package otel; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
|
||
public class CustomSamplerProvider implements ConfigurableSamplerProvider { | ||
|
||
@Override | ||
public Sampler createSampler(ConfigProperties config) { | ||
// Callback invoked when OTEL_TRACES_SAMPLER is set to the value from getName(). | ||
return new CustomSampler(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "custom-sampler"; | ||
} | ||
} |
Oops, something went wrong.