-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
143 lines (126 loc) · 5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Diagnostics;
using System.Text.Json;
using Common;
using Common.Redis;
using KafkaFlow;
using KafkaFlow.Configuration;
using KafkaFlow.OpenTelemetry;
using KafkaFlow.Producers;
using KafkaFlow.Serializer;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<Connections>(builder.Configuration.GetSection("Connections"));
builder.Services.AddLogging(configure =>
{
configure.AddConsole();
});
builder.Services
.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("kafkawriter.api"))
.WithTracing(tracing =>
{
tracing
.SetSampler<AlwaysOnSampler>()
.AddSource(KafkaFlowInstrumentation.ActivitySourceName)
.AddSource(DistributedTracingInstrumentation.ActivitySourceName)
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddRedisInstrumentation()
.AddOtlpExporter();
tracing.ConfigureRedisInstrumentation((services, configure) =>
{
var nonKeyedLazyMultiplexer =
services.GetRequiredService<Lazy<IConnectionMultiplexer>>();
configure.AddConnection("Multiplexer", nonKeyedLazyMultiplexer.Value);
});
});
builder.Services.AddKafka(kafka =>
{
kafka.UseMicrosoftLog();
var kafkaConfig = builder.Configuration.GetSection("Connections").Get<Connections>();
kafka.AddCluster(cluster => cluster
.WithSecurityInformation(security =>
{
security.EnableSslCertificateVerification = false;
security.SecurityProtocol = SecurityProtocol.Plaintext;
})
.WithBrokers(kafkaConfig!.Kafka.Brokers.Split(','))
.AddProducer("api-producer", producer =>
{
producer.DefaultTopic(kafkaConfig.Kafka.TopicName);
producer.AddMiddlewares(middlewares =>
{
middlewares.AddSerializer<JsonCoreSerializer>();
});
})
)
.AddOpenTelemetryInstrumentation();
});
builder.Services.TryAddSingleton<Lazy<IConnectionMultiplexer>>(provider =>
{
var connectionString = provider.GetRequiredService<IOptions<Connections>>().Value.Redis.ConnectionString;
return new Lazy<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(connectionString));
});
builder.Services.AddSingleton(TimeProvider.System);
builder.Services.TryAddSingleton<IRedisStreamsService, RedisStreamsService>();
builder.Services.TryAddSingleton<IRedisCacheService, RedisCacheService>();
var app = builder.Build();
app.MapPost("/produce", async (IProducerAccessor producers, IOptions<Connections> options,
IRedisStreamsService streamsService,
IRedisCacheService redisCacheService,
ILogger<Program> logger) =>
{
var now = DateTime.UtcNow;
await ProduceKafkaMessage(now, producers, logger);
CacheRedisMessage(now, redisCacheService, logger);
await StreamRedisMessage(now, streamsService, options, logger);
})
.WithName("Produce");
app.Run();
void CacheRedisMessage(DateTime now, IRedisCacheService redisCacheService, ILogger<Program> logger)
{
var message = new TracedMessage()
{
Content = $@"It's {now}",
CreatedOn = now
};
logger.LogInformation($"Creating redis cache entry {message.Content}");
using var activity = DistributedTracingInstrumentation.Source.StartActivity("redis-set", ActivityKind.Producer);
AddActivityToMessage(activity, message);
var serializedMessage = JsonSerializer.Serialize(message);
redisCacheService.Set("redis-key", serializedMessage);
}
async Task StreamRedisMessage(DateTime now, IRedisStreamsService redisStreamsService, IOptions<Connections> options, ILogger<Program> logger)
{
var message = new TracedMessage()
{
Content = $@"It's {now}",
CreatedOn = now
};
logger.LogInformation($"Producing to redis stream {message.Content}");
var serializedMessage = JsonSerializer.Serialize(message);
await redisStreamsService.StreamAddAsync(options.Value.Redis.StreamName, serializedMessage, 1);
}
void AddActivityToMessage(Activity activity, TracedMessage redisMessage)
{
Propagators.DefaultTextMapPropagator.Inject(
new PropagationContext(activity.Context, Baggage.Current),
redisMessage,
(message, key, value) => message.PropagationContext = value);
}
async Task ProduceKafkaMessage(DateTime dateTime, IProducerAccessor producerAccessor, ILogger<Program> logger)
{
var message = new Message()
{
Content = $@"It's {dateTime}",
CreatedOn = dateTime
};
logger.LogInformation($"Producing to kafka stream {message.Content}");
await producerAccessor["api-producer"].ProduceAsync(Guid.NewGuid().ToString(), message);
}