-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AppConfig and Event Hubs samples for using Monitor exporter (#17565)
* Add AppConfig and Event Hubs samples for using exporters * Fix compiler warnings * Update sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/pom.xml * Update method names
- Loading branch information
Showing
4 changed files
with
226 additions
and
28 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
65 changes: 65 additions & 0 deletions
65
...osoft/opentelemetry/exporter/azuremonitor/AppConfigurationAzureMonitorExporterSample.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,65 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.opentelemetry.exporter.azuremonitor; | ||
|
||
import com.azure.data.appconfiguration.ConfigurationClient; | ||
import com.azure.data.appconfiguration.ConfigurationClientBuilder; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.trace.TracerSdkProvider; | ||
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
import io.opentelemetry.trace.Span; | ||
import io.opentelemetry.trace.Tracer; | ||
|
||
/** | ||
* Sample to demonstrate using {@link AzureMonitorExporter} to export telemetry events when setting a configuration | ||
* in App Configuration through the {@link ConfigurationClient}. | ||
*/ | ||
public class AppConfigurationAzureMonitorExporterSample { | ||
|
||
private static final Tracer TRACER = configureAzureMonitorExporter(); | ||
private static final String CONNECTION_STRING = "<YOUR_CONNECTION_STRING>"; | ||
|
||
/** | ||
* The main method to run the application. | ||
* @param args Ignored args. | ||
*/ | ||
public static void main(String[] args) { | ||
doClientWork(); | ||
} | ||
|
||
/** | ||
* Configure the OpenTelemetry {@link AzureMonitorExporter} to enable tracing. | ||
* @return The OpenTelemetry {@link Tracer} instance. | ||
*/ | ||
private static Tracer configureAzureMonitorExporter() { | ||
AzureMonitorExporter exporter = new AzureMonitorExporterBuilder() | ||
.connectionString("{connection-string}") | ||
.buildExporter(); | ||
|
||
TracerSdkProvider tracerSdkProvider = OpenTelemetrySdk.getTracerProvider(); | ||
tracerSdkProvider.addSpanProcessor(SimpleSpanProcessor.newBuilder(exporter).build()); | ||
return tracerSdkProvider.get("Sample"); | ||
} | ||
|
||
/** | ||
* Creates the {@link ConfigurationClient} and sets a configuration in Azure App Configuration with distributed | ||
* tracing enabled and using the Azure Monitor exporter to export telemetry events to Azure Monitor. | ||
*/ | ||
private static void doClientWork() { | ||
ConfigurationClient client = new ConfigurationClientBuilder() | ||
.connectionString(CONNECTION_STRING) | ||
.buildClient(); | ||
|
||
Span span = TRACER.spanBuilder("user-parent-span").startSpan(); | ||
final Scope scope = TRACER.withSpan(span); | ||
try { | ||
// Thread bound (sync) calls will automatically pick up the parent span and you don't need to pass it explicitly. | ||
client.setConfigurationSetting("hello", "text", "World"); | ||
} finally { | ||
span.end(); | ||
scope.close(); | ||
} | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
...es/java/com/microsoft/opentelemetry/exporter/azuremonitor/AzureMonitorExporterSample.java
This file was deleted.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
...om/microsoft/opentelemetry/exporter/azuremonitor/EventHubsAzureMonitorExporterSample.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,140 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.opentelemetry.exporter.azuremonitor; | ||
|
||
import com.azure.messaging.eventhubs.EventData; | ||
import com.azure.messaging.eventhubs.EventDataBatch; | ||
import com.azure.messaging.eventhubs.EventHubClientBuilder; | ||
import com.azure.messaging.eventhubs.EventHubProducerAsyncClient; | ||
import com.azure.messaging.eventhubs.models.CreateBatchOptions; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.trace.TracerSdkProvider; | ||
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
import io.opentelemetry.trace.Span; | ||
import io.opentelemetry.trace.Tracer; | ||
import reactor.core.Exceptions; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static com.azure.core.util.tracing.Tracer.PARENT_SPAN_KEY; | ||
import static com.azure.messaging.eventhubs.implementation.ClientConstants.OPERATION_TIMEOUT; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
/** | ||
* Sample to demontrate using {@link AzureMonitorExporter} to export telemetry events when sending events to Event Hubs | ||
* using {@link EventHubProducerAsyncClient}. | ||
*/ | ||
public class EventHubsAzureMonitorExporterSample { | ||
private static final Tracer TRACER = configureAzureMonitorExporter(); | ||
private static final String CONNECTION_STRING = "<YOUR_CONNECTION_STRING>"; | ||
|
||
/** | ||
* The main method to run the application. | ||
* @param args Ignored args. | ||
*/ | ||
public static void main(String[] args) { | ||
doClientWork(); | ||
} | ||
|
||
/** | ||
* Configure the OpenTelemetry {@link AzureMonitorExporter} to enable tracing. | ||
* @return The OpenTelemetry {@link Tracer} instance. | ||
*/ | ||
private static Tracer configureAzureMonitorExporter() { | ||
AzureMonitorExporter exporter = new AzureMonitorExporterBuilder() | ||
.connectionString("{connection-string}") | ||
.buildExporter(); | ||
|
||
TracerSdkProvider tracerSdkProvider = OpenTelemetrySdk.getTracerProvider(); | ||
tracerSdkProvider.addSpanProcessor(SimpleSpanProcessor.newBuilder(exporter).build()); | ||
return tracerSdkProvider.get("Sample"); | ||
} | ||
|
||
/** | ||
* Method that creates {@link EventHubProducerAsyncClient} to send events to Event Hubs with distributed | ||
* telemetry enabled and using Azure Monitor exporter to export telemetry events. | ||
*/ | ||
private static void doClientWork() { | ||
EventHubProducerAsyncClient producer = new EventHubClientBuilder() | ||
.connectionString(CONNECTION_STRING) | ||
.buildAsyncProducerClient(); | ||
|
||
Span span = TRACER.spanBuilder("user-parent-span").startSpan(); | ||
final Scope scope = TRACER.withSpan(span); | ||
try { | ||
String firstPartition = producer.getPartitionIds().blockFirst(OPERATION_TIMEOUT); | ||
|
||
final byte[] body = "EventData Sample 1".getBytes(UTF_8); | ||
final byte[] body2 = "EventData Sample 2".getBytes(UTF_8); | ||
|
||
// We will publish three events based on simple sentences. | ||
Flux<EventData> data = Flux.just( | ||
new EventData(body).addContext(PARENT_SPAN_KEY, TRACER.getCurrentSpan()), | ||
new EventData(body2).addContext(PARENT_SPAN_KEY, TRACER.getCurrentSpan())); | ||
|
||
// Create a batch to send the events. | ||
final CreateBatchOptions options = new CreateBatchOptions() | ||
.setPartitionId(firstPartition) | ||
.setMaximumSizeInBytes(256); | ||
|
||
final AtomicReference<EventDataBatch> currentBatch = new AtomicReference<>( | ||
producer.createBatch(options).block(OPERATION_TIMEOUT)); | ||
|
||
data.flatMap(event -> { | ||
final EventDataBatch batch = currentBatch.get(); | ||
if (batch.tryAdd(event)) { | ||
return Mono.empty(); | ||
} | ||
|
||
// The batch is full, so we create a new batch and send the batch. Mono.when completes when both | ||
// operations | ||
// have completed. | ||
return Mono.when( | ||
producer.send(batch), | ||
producer.createBatch(options).map(newBatch -> { | ||
currentBatch.set(newBatch); | ||
|
||
// Add that event that we couldn't before. | ||
if (!newBatch.tryAdd(event)) { | ||
throw Exceptions.propagate(new IllegalArgumentException(String.format( | ||
"Event is too large for an empty batch. Max size: %s. Event: %s", | ||
newBatch.getMaxSizeInBytes(), event.getBodyAsString()))); | ||
} | ||
|
||
return newBatch; | ||
})); | ||
}).then() | ||
.doFinally(signal -> { | ||
final EventDataBatch batch = currentBatch.getAndSet(null); | ||
if (batch != null) { | ||
producer.send(batch).block(OPERATION_TIMEOUT); | ||
} | ||
}) | ||
.subscribe(unused -> System.out.println("Complete"), | ||
error -> System.out.println("Error sending events: " + error), | ||
() -> { | ||
System.out.println("Completed sending events."); | ||
span.end(); | ||
}); | ||
|
||
|
||
// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep | ||
// the thread so the program does not end before the send operation is complete. Using .block() instead of | ||
// .subscribe() will turn this into a synchronous call. | ||
try { | ||
TimeUnit.SECONDS.sleep(5); | ||
} catch (InterruptedException ignored) { | ||
} finally { | ||
// Disposing of our producer. | ||
producer.close(); | ||
} | ||
} finally { | ||
scope.close(); | ||
} | ||
} | ||
} |