-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: sample otel * chore: upgrade sample to v3
- Loading branch information
Gui Ferreira
authored
Dec 6, 2023
1 parent
453996e
commit 0697853
Showing
13 changed files
with
13,388 additions
and
422 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
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
<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="6.0.1" /> | ||
<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" /> | ||
</ItemGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
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,94 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using KafkaFlow.Configuration; | ||
using KafkaFlow.OpenTelemetry; | ||
using KafkaFlow.Producers; | ||
using KafkaFlow.Serializer; | ||
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 | ||
.AddDeserializer<ProtobufNetDeserializer>() | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 20 | ||
sidebar_position: 11 | ||
--- | ||
|
||
# Dependency Injection | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"label": "Migration", | ||
"position": 11, | ||
"position": 12, | ||
"link": { | ||
"type": "generated-index" | ||
} | ||
|
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
Oops, something went wrong.