-
Notifications
You must be signed in to change notification settings - Fork 119
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
Guilherme Ferreira
committed
Nov 27, 2023
1 parent
9c57625
commit d361097
Showing
9 changed files
with
244 additions
and
4 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
samples/KafkaFlow.Sample.OpenTelemetry/KafkaFlow.Sample.OpenTelemetry.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,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.17" /> | ||
<PackageReference Include="OpenTelemetry" Version="1.6.0" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.6.0" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\KafkaFlow.LogHandler.Console\KafkaFlow.LogHandler.Console.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Microsoft.DependencyInjection\KafkaFlow.Microsoft.DependencyInjection.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.OpenTelemetry\KafkaFlow.OpenTelemetry.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Serializer.ProtobufNet\KafkaFlow.Serializer.ProtobufNet.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Serializer\KafkaFlow.Serializer.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.TypedHandler\KafkaFlow.TypedHandler.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Microsoft.DependencyInjection\KafkaFlow.Microsoft.DependencyInjection.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Serializer.ProtobufNet\KafkaFlow.Serializer.ProtobufNet.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.TypedHandler\KafkaFlow.TypedHandler.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
samples/KafkaFlow.Sample.OpenTelemetry/PrintConsoleHandler.cs
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 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using KafkaFlow.TypedHandler; | ||
|
||
namespace KafkaFlow.Sample.OpenTelemetry; | ||
|
||
public class PrintConsoleHandler : IMessageHandler<TestMessage> | ||
{ | ||
public Task Handle(IMessageContext context, TestMessage message) | ||
{ | ||
Console.WriteLine( | ||
"Partition: {0} | Offset: {1} | Message: {2}", | ||
context.ConsumerContext.Partition, | ||
context.ConsumerContext.Offset, | ||
message.Text); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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 System; | ||
using System.Threading.Tasks; | ||
using KafkaFlow.Configuration; | ||
using KafkaFlow.OpenTelemetry; | ||
using KafkaFlow.Producers; | ||
using KafkaFlow.Serializer; | ||
using KafkaFlow.TypedHandler; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace KafkaFlow.Sample.OpenTelemetry; | ||
|
||
public class Program | ||
{ | ||
public static async Task Main() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
const string producerName = "PrintConsole"; | ||
const string topicName = "otel-sample-topic"; | ||
|
||
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( | ||
serviceName: "DemoApp", | ||
serviceVersion: "1.0.0")) | ||
.AddSource(KafkaFlowInstrumentation.ActivitySourceName) | ||
.AddConsoleExporter() | ||
.AddOtlpExporter() | ||
.Build(); | ||
|
||
services.AddKafka( | ||
kafka => kafka | ||
.UseConsoleLog() | ||
.AddOpenTelemetryInstrumentation() | ||
.AddCluster( | ||
cluster => cluster | ||
.WithBrokers(new[] | ||
{ | ||
"localhost:9092" | ||
}) | ||
.CreateTopicIfNotExists(topicName, 6, 1) | ||
.AddProducer( | ||
producerName, | ||
producer => producer | ||
.DefaultTopic(topicName) | ||
.AddMiddlewares(m => m.AddSerializer<ProtobufNetSerializer>()) | ||
) | ||
.AddConsumer( | ||
consumer => consumer | ||
.Topic(topicName) | ||
.WithGroupId("print-console-handler") | ||
.WithBufferSize(100) | ||
.WithWorkersCount(3) | ||
.AddMiddlewares( | ||
middlewares => middlewares | ||
.AddSerializer<ProtobufNetSerializer>() | ||
.AddTypedHandlers(h => h.AddHandler<PrintConsoleHandler>()) | ||
) | ||
) | ||
) | ||
); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
|
||
var bus = provider.CreateKafkaBus(); | ||
|
||
await bus.StartAsync(); | ||
|
||
var producer = provider | ||
.GetRequiredService<IProducerAccessor>() | ||
.GetProducer(producerName); | ||
|
||
while (true) | ||
{ | ||
Console.WriteLine("Type the message you want to send or 'exit' to quit:"); | ||
var input = Console.ReadLine(); | ||
|
||
|
||
if (input!.Equals("exit", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
await bus.StopAsync(); | ||
break; | ||
} | ||
|
||
await producer.ProduceAsync( | ||
topicName, | ||
Guid.NewGuid().ToString(), | ||
new TestMessage { Text = $"Message: {input}" }); | ||
} | ||
|
||
await Task.Delay(3000); | ||
} | ||
} |
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,52 @@ | ||
# KafkaFlow.Sample.OpenTelemetry | ||
|
||
This is a simple sample that shows how to enable [OpenTelemetry](https://opentelemetry.io/) instrumentation when using KafkaFlow. | ||
|
||
## How to run | ||
|
||
### Requirements | ||
|
||
- [.NET 6.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) | ||
- [Docker Desktop](https://www.docker.com/products/docker-desktop/) | ||
|
||
### Start the cluster | ||
|
||
Using your terminal of choice, start the cluster. | ||
You can find a docker-compose file at the root of this repository. | ||
Position the terminal in that folder and run the following command. | ||
|
||
```bash | ||
docker-compose up -d | ||
``` | ||
|
||
### Start Jaeger | ||
|
||
Using your terminal of choice, start the [Jaeger](https://www.jaegertracing.io/). | ||
|
||
``` | ||
docker run --rm --name jaeger \ | ||
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \ | ||
-p 6831:6831/udp \ | ||
-p 6832:6832/udp \ | ||
-p 5778:5778 \ | ||
-p 16686:16686 \ | ||
-p 4317:4317 \ | ||
-p 4318:4318 \ | ||
-p 14250:14250 \ | ||
-p 14268:14268 \ | ||
-p 14269:14269 \ | ||
-p 9411:9411 \ | ||
jaegertracing/all-in-one:1.51 | ||
``` | ||
|
||
### Run the Sample | ||
|
||
Using your terminal of choice, start the sample for the sample folder. | ||
|
||
```bash | ||
dotnet run | ||
``` | ||
|
||
### See the traces collected | ||
|
||
Go to http://localhost:16686/ and observe the traces collected for your application. |
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 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace KafkaFlow.Sample.OpenTelemetry; | ||
|
||
[DataContract] | ||
public class TestMessage | ||
{ | ||
[DataMember(Order = 1)] | ||
public string Text { get; set; } | ||
} |
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