-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
397 additions
and
9 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Temporalio.Activities; | ||
|
||
namespace TemporalioSamples.OpenTelemetry.Common; | ||
|
||
public static class Activities | ||
{ | ||
[Activity] | ||
public static void MyActivity(string input) | ||
{ | ||
ActivityExecutionContext.Current.Logger.LogInformation("Executing activity for OpenTelemetry sample."); | ||
|
||
ActivityExecutionContext.Current.MetricMeter.CreateCounter<int>("my-activity-counter", description: "Counter used to instrument an activity.").Add(123); | ||
} | ||
} |
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,24 @@ | ||
namespace TemporalioSamples.OpenTelemetry.Common; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Temporalio.Workflows; | ||
|
||
[Workflow] | ||
public class MyWorkflow | ||
{ | ||
[WorkflowRun] | ||
public async Task<string> RunAsync() | ||
{ | ||
Workflow.Logger.LogInformation("Running workflow {WorkflowId}.", Workflow.Info.WorkflowId); | ||
|
||
Workflow.MetricMeter.CreateCounter<int>("my-workflow-counter", description: "Replay-safe counter for instrumentation inside a workflow.").Add(123); | ||
await Workflow.ExecuteActivityAsync( | ||
() => Activities.MyActivity("input"), | ||
new() | ||
{ | ||
StartToCloseTimeout = TimeSpan.FromMinutes(5), | ||
}); | ||
|
||
return "complete!"; | ||
} | ||
} |
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,95 @@ | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
using Temporalio.Client; | ||
using Temporalio.Extensions.OpenTelemetry; | ||
using Temporalio.Runtime; | ||
using Temporalio.Worker; | ||
using TemporalioSamples.OpenTelemetry.Common; | ||
|
||
var assemblyName = typeof(TemporalClient).Assembly.GetName(); | ||
|
||
var instanceId = args.ElementAtOrDefault(0) ?? throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument"); | ||
|
||
var resourceBuilder = ResourceBuilder. | ||
CreateDefault(). | ||
AddService("TemporalioSamples.OpenTelemetry", serviceInstanceId: instanceId); | ||
|
||
using var tracerProvider = Sdk. | ||
CreateTracerProviderBuilder(). | ||
SetResourceBuilder(resourceBuilder). | ||
AddSource(TracingInterceptor.ClientSource.Name, TracingInterceptor.WorkflowsSource.Name, TracingInterceptor.ActivitiesSource.Name). | ||
AddOtlpExporter(). | ||
Build(); | ||
|
||
// Create a client to localhost on default namespace | ||
var client = await TemporalClient.ConnectAsync(new("localhost:7233") | ||
{ | ||
LoggerFactory = LoggerFactory.Create(builder => | ||
builder. | ||
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] "). | ||
SetMinimumLevel(LogLevel.Information)), | ||
Interceptors = new[] { new TracingInterceptor() }, | ||
Runtime = new TemporalRuntime(new TemporalRuntimeOptions() | ||
{ | ||
Telemetry = new TelemetryOptions() | ||
{ | ||
Metrics = new MetricsOptions() | ||
{ | ||
OpenTelemetry = new OpenTelemetryOptions() | ||
{ | ||
Url = new Uri("http://localhost:4317"), | ||
}, | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
async Task RunWorkerAsync() | ||
{ | ||
// Cancellation token cancelled on ctrl+c | ||
using var tokenSource = new CancellationTokenSource(); | ||
Console.CancelKeyPress += (_, eventArgs) => | ||
{ | ||
tokenSource.Cancel(); | ||
eventArgs.Cancel = true; | ||
}; | ||
|
||
// Run worker until cancelled | ||
Console.WriteLine("Running worker"); | ||
using var worker = new TemporalWorker( | ||
client, | ||
new TemporalWorkerOptions(taskQueue: "opentelemetry-sample-core-sdk-forwarding"). | ||
AddWorkflow<MyWorkflow>(). | ||
AddActivity(Activities.MyActivity)); | ||
try | ||
{ | ||
await worker.ExecuteAsync(tokenSource.Token); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
Console.WriteLine("Worker cancelled"); | ||
} | ||
} | ||
|
||
async Task ExecuteWorkflowAsync() | ||
{ | ||
Console.WriteLine("Executing workflow"); | ||
await client.ExecuteWorkflowAsync( | ||
(MyWorkflow wf) => wf.RunAsync(), | ||
new(id: "opentelemetry-sample-core-sdk-workflow-id", taskQueue: "opentelemetry-sample-core-sdk-forwarding")); | ||
} | ||
|
||
switch (args.ElementAtOrDefault(0)) | ||
{ | ||
case "worker": | ||
await RunWorkerAsync(); | ||
break; | ||
case "workflow": | ||
await ExecuteWorkflowAsync(); | ||
break; | ||
default: | ||
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument"); | ||
} |
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,41 @@ | ||
# OpenTelemetry - .Core SDK Forwarding | ||
|
||
This sample shows how to configure the SDK to forward metrics from the Core SDK. | ||
|
||
The main advantage over using .NET metrics is simplicity. | ||
|
||
This sample also shows how to configure custom metrics from both an activity and a workflow in a replay-safe manner. | ||
|
||
To run, first see [README.md](../../../README.md) for prerequisites. | ||
|
||
Then, run the following from [one directory up ](../docker-compose.yaml) to start the .NET Aspire Dashboard which will collect telemetry. The dashboard UI is available at http://localhost:18888. | ||
|
||
docker compose up | ||
|
||
Then, run the following from this directory in a separate terminal to start the worker: | ||
|
||
dotnet run worker | ||
|
||
Then in another terminal, run the workflow from this directory: | ||
|
||
dotnet run workflow | ||
|
||
The workflow will complete. | ||
|
||
## Traces | ||
|
||
Traces can be viewed at http://localhost:18888/traces. | ||
|
||
You can select either `worker` or `workflow` for traces; both should show the same trace. The workflow should appear and when clicked, may look something like: | ||
|
||
![Tracing Screenshot](tracing-screenshot.png) | ||
|
||
## Metrics | ||
|
||
Metrics can be viewed by clicking the metrics tab on the dashboard. | ||
|
||
Select `temporal-core-sdk`. | ||
|
||
All metrics emitted by the Core SDK will be shown. It may look something like: | ||
|
||
![Metrics Screenshot](metrics-screenshot.png) |
15 changes: 15 additions & 0 deletions
15
src/OpenTelemetry/CoreSdkForwarding/TemporalioSamples.OpenTelemetry.CoreSdkForwarding.csproj
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Common\**\*.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,103 @@ | ||
using System.Diagnostics.Metrics; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
using Temporalio.Client; | ||
using Temporalio.Extensions.DiagnosticSource; | ||
using Temporalio.Extensions.OpenTelemetry; | ||
using Temporalio.Runtime; | ||
using Temporalio.Worker; | ||
using TemporalioSamples.OpenTelemetry.Common; | ||
|
||
var assemblyName = typeof(TemporalClient).Assembly.GetName(); | ||
|
||
using var meter = new Meter(assemblyName.Name!, assemblyName.Version!.ToString()); | ||
|
||
var instanceId = args.ElementAtOrDefault(0) ?? throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument"); | ||
|
||
var resourceBuilder = ResourceBuilder. | ||
CreateDefault(). | ||
AddService("TemporalioSamples.OpenTelemetry", serviceInstanceId: instanceId); | ||
|
||
using var tracerProvider = Sdk. | ||
CreateTracerProviderBuilder(). | ||
SetResourceBuilder(resourceBuilder). | ||
AddSource(TracingInterceptor.ClientSource.Name, TracingInterceptor.WorkflowsSource.Name, TracingInterceptor.ActivitiesSource.Name). | ||
AddOtlpExporter(). | ||
Build(); | ||
|
||
using var meterProvider = Sdk. | ||
CreateMeterProviderBuilder(). | ||
SetResourceBuilder(resourceBuilder). | ||
AddMeter(assemblyName.Name!). | ||
AddOtlpExporter(). | ||
Build(); | ||
|
||
// Create a client to localhost on default namespace | ||
var client = await TemporalClient.ConnectAsync(new("localhost:7233") | ||
{ | ||
LoggerFactory = LoggerFactory.Create(builder => | ||
builder. | ||
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] "). | ||
SetMinimumLevel(LogLevel.Information)), | ||
Interceptors = new[] { new TracingInterceptor() }, | ||
Runtime = new TemporalRuntime(new TemporalRuntimeOptions() | ||
{ | ||
Telemetry = new TelemetryOptions() | ||
{ | ||
Metrics = new MetricsOptions() | ||
{ | ||
CustomMetricMeter = new CustomMetricMeter(meter), | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
async Task RunWorkerAsync() | ||
{ | ||
// Cancellation token cancelled on ctrl+c | ||
using var tokenSource = new CancellationTokenSource(); | ||
Console.CancelKeyPress += (_, eventArgs) => | ||
{ | ||
tokenSource.Cancel(); | ||
eventArgs.Cancel = true; | ||
}; | ||
|
||
// Run worker until cancelled | ||
Console.WriteLine("Running worker"); | ||
using var worker = new TemporalWorker( | ||
client, | ||
new TemporalWorkerOptions(taskQueue: "opentelemetry-sample-dotnet-metrics"). | ||
AddWorkflow<MyWorkflow>(). | ||
AddActivity(Activities.MyActivity)); | ||
try | ||
{ | ||
await worker.ExecuteAsync(tokenSource.Token); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
Console.WriteLine("Worker cancelled"); | ||
} | ||
} | ||
|
||
async Task ExecuteWorkflowAsync() | ||
{ | ||
Console.WriteLine("Executing workflow"); | ||
await client.ExecuteWorkflowAsync( | ||
(MyWorkflow wf) => wf.RunAsync(), | ||
new(id: "opentelemetry-sample-dotnet-workflow-id", taskQueue: "opentelemetry-sample-dotnet-metrics")); | ||
} | ||
|
||
switch (args.ElementAtOrDefault(0)) | ||
{ | ||
case "worker": | ||
await RunWorkerAsync(); | ||
break; | ||
case "workflow": | ||
await ExecuteWorkflowAsync(); | ||
break; | ||
default: | ||
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument"); | ||
} |
Oops, something went wrong.