Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OTel and User certificates samples #307

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions samples/Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventStore.Client.Projectio
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "projection-management", "projection-management\projection-management.csproj", "{9DEA2684-C38B-465C-91A6-ED2AB67A4338}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "diagnostics", "diagnostics\diagnostics.csproj", "{546496AD-E355-4C20-930C-30D0FC76D26F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "user-certificates", "user-certificates\user-certificates.csproj", "{28112410-D02D-427A-9D36-3FE3A6DC6B0D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventStore.Client.Extensions.OpenTelemetry", "..\src\EventStore.Client.Extensions.OpenTelemetry\EventStore.Client.Extensions.OpenTelemetry.csproj", "{29E3F07A-6676-45C1-805C-46BDF6CF325B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -101,6 +107,18 @@ Global
{9DEA2684-C38B-465C-91A6-ED2AB67A4338}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DEA2684-C38B-465C-91A6-ED2AB67A4338}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DEA2684-C38B-465C-91A6-ED2AB67A4338}.Release|Any CPU.Build.0 = Release|Any CPU
{546496AD-E355-4C20-930C-30D0FC76D26F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{546496AD-E355-4C20-930C-30D0FC76D26F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{546496AD-E355-4C20-930C-30D0FC76D26F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{546496AD-E355-4C20-930C-30D0FC76D26F}.Release|Any CPU.Build.0 = Release|Any CPU
{28112410-D02D-427A-9D36-3FE3A6DC6B0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28112410-D02D-427A-9D36-3FE3A6DC6B0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28112410-D02D-427A-9D36-3FE3A6DC6B0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28112410-D02D-427A-9D36-3FE3A6DC6B0D}.Release|Any CPU.Build.0 = Release|Any CPU
{29E3F07A-6676-45C1-805C-46BDF6CF325B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29E3F07A-6676-45C1-805C-46BDF6CF325B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29E3F07A-6676-45C1-805C-46BDF6CF325B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29E3F07A-6676-45C1-805C-46BDF6CF325B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -110,6 +128,7 @@ Global
{A71A13F7-8480-4E48-B88D-A2364F7C95B6} = {EBB93BBC-42A7-48E4-B1EA-0EA3953D347C}
{7200BB01-A405-45D5-A6E8-A8FA8DE39DA0} = {EBB93BBC-42A7-48E4-B1EA-0EA3953D347C}
{79992D7B-C311-4E8A-856F-896C1EA61042} = {EBB93BBC-42A7-48E4-B1EA-0EA3953D347C}
{29E3F07A-6676-45C1-805C-46BDF6CF325B} = {EBB93BBC-42A7-48E4-B1EA-0EA3953D347C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2E7E3A6C-21DA-4DBD-9EB3-68DFC5CDE48F}
Expand Down
96 changes: 96 additions & 0 deletions samples/diagnostics/Program.cs
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
}
}
22 changes: 22 additions & 0 deletions samples/diagnostics/diagnostics.csproj
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>
22 changes: 22 additions & 0 deletions samples/user-certificates/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
await ClientWithUserCertificates();

return;

static async Task ClientWithUserCertificates() {
try {
# 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
} catch (InvalidClientCertificateException) {
// ignore for sample purposes
}
}
16 changes: 16 additions & 0 deletions samples/user-certificates/user-certificates.csproj
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>
Loading