-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OTel and User certificates samples
- Loading branch information
Showing
5 changed files
with
171 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
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,96 @@ | ||
using System.Diagnostics; | ||
using EventStore.Client.Extensions.OpenTelemetry; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using OpenTelemetry.Exporter; | ||
using OpenTelemetry.Trace; | ||
using OpenTelemetry.Resources; | ||
|
||
#pragma warning disable CS8321 // Local function is declared but never used | ||
#pragma warning disable CS1587 // XML comment is not placed on a valid language element | ||
|
||
/** | ||
# region import-required-packages | ||
// required | ||
dotnet add package EventStore.Client.Extensions.OpenTelemetry | ||
// recommended | ||
dotnet add package OpenTelemetry.Exporter.Jaeger | ||
dotnet add package OpenTelemetry.Exporter.Console | ||
dotnet add package OpenTelemetry | ||
dotnet add package Microsoft.Extensions.Hosting | ||
dotnet add package OpenTelemetry.Extensions.Hosting | ||
# endregion import-required-packages | ||
**/ | ||
|
||
var settings = EventStoreClientSettings.Create("esdb://localhost:2113?tls=false"); | ||
|
||
settings.OperationOptions.ThrowOnAppendFailure = false; | ||
|
||
await using var client = new EventStoreClient(settings); | ||
|
||
await TraceAppendToStream(client); | ||
|
||
return; | ||
|
||
static async Task TraceAppendToStream(EventStoreClient client) { | ||
const string serviceName = "sample"; | ||
|
||
var host = Host.CreateDefaultBuilder() | ||
.ConfigureServices( | ||
(_, services) => { | ||
services.AddSingleton(new ActivitySource(serviceName)); | ||
services | ||
.AddOpenTelemetry() | ||
.ConfigureResource(builder => builder.AddService(serviceName)) | ||
.WithTracing(ConfigureTracerProviderBuilder); | ||
} | ||
) | ||
.Build(); | ||
|
||
using (host) { | ||
# region setup-client-for-tracing | ||
|
||
host.Start(); | ||
|
||
var eventData = new EventData( | ||
Uuid.NewUuid(), | ||
"some-event", | ||
"{\"id\": \"1\" \"value\": \"some value\"}"u8.ToArray() | ||
); | ||
|
||
await client.AppendToStreamAsync( | ||
Uuid.NewUuid().ToString(), | ||
StreamState.Any, | ||
new List<EventData> { | ||
eventData | ||
} | ||
); | ||
|
||
# endregion setup-client-for-tracing | ||
} | ||
|
||
return; | ||
|
||
static void ConfigureTracerProviderBuilder(TracerProviderBuilder tracerProviderBuilder) { | ||
#region register-instrumentation | ||
|
||
tracerProviderBuilder | ||
.AddEventStoreClientInstrumentation(); | ||
|
||
#endregion register-instrumentation | ||
|
||
#region setup-exporter | ||
|
||
tracerProviderBuilder | ||
.AddConsoleExporter() | ||
.AddJaegerExporter( | ||
options => { | ||
options.Endpoint = new Uri("http://localhost:4317"); | ||
options.Protocol = JaegerExportProtocol.UdpCompactThrift; | ||
} | ||
); | ||
|
||
#endregion | ||
} | ||
} |
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>connecting_to_a_cluster</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.5.1" /> | ||
<PackageReference Include="OpenTelemetry" Version="1.8.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<!-- Use NuGet instead of ProjectReferences --> | ||
<!-- <PackageReference Include="EventStore.Client.Grpc.Streams" Version="23.1.0" /> --> | ||
<ProjectReference Include="..\..\src\EventStore.Client.Streams\EventStore.Client.Streams.csproj" /> | ||
<ProjectReference Include="..\..\src\EventStore.Client\EventStore.Client.csproj" /> | ||
<ProjectReference Include="..\..\src\EventStore.Client.Extensions.OpenTelemetry\EventStore.Client.Extensions.OpenTelemetry.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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,18 @@ | ||
await ClientWithUserCertificates(); | ||
|
||
return; | ||
|
||
static async Task ClientWithUserCertificates() { | ||
# region client-with-user-certificates | ||
|
||
const string userCertFile = "/path/to/user.crt"; | ||
const string userKeyFile = "/path/to/user.key"; | ||
|
||
var settings = EventStoreClientSettings.Create( | ||
$"esdb://localhost:2113/?tls=true&tlsVerifyCert=true&userCertFile={userCertFile}&userKeyFile={userKeyFile}" | ||
); | ||
|
||
await using var client = new EventStoreClient(settings); | ||
|
||
# endregion client-with-user-certificates | ||
} |
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>user_certificates</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\EventStore.Client.Streams\EventStore.Client.Streams.csproj" /> | ||
<ProjectReference Include="..\..\src\EventStore.Client\EventStore.Client.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |