-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (49 loc) · 1.77 KB
/
Program.cs
File metadata and controls
59 lines (49 loc) · 1.77 KB
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
using Microsoft.AspNetCore.Mvc;
using OpenFeature;
using OpenFeature.DependencyInjection.Providers.Memory;
using OpenFeature.Hooks;
using OpenFeature.Providers.Memory;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddProblemDetails();
// Configure OpenTelemetry
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService("openfeature-aspnetcore-sample"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter())
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddMeter("OpenFeature")
.AddOtlpExporter());
builder.Services.AddOpenFeature(featureBuilder =>
{
featureBuilder.AddHostedFeatureLifecycle()
.AddHook(sp => new LoggingHook(sp.GetRequiredService<ILogger<LoggingHook>>()))
.AddHook<MetricsHook>()
.AddHook<TraceEnricherHook>()
.AddInMemoryProvider("InMemory", _ => new Dictionary<string, Flag>()
{
{
"welcome-message", new Flag<bool>(
new Dictionary<string, bool> { { "show", true }, { "hide", false } }, "show")
}
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseExceptionHandler();
app.MapGet("/welcome", async ([FromServices] IFeatureClient featureClient) =>
{
var welcomeMessageEnabled = await featureClient.GetBooleanValueAsync("welcome-message", false);
if (welcomeMessageEnabled)
{
return TypedResults.Ok("Hello world! The welcome-message feature flag was enabled!");
}
return TypedResults.Ok("Hello world!");
});
app.Run();