Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (62 loc) · 2.27 KB
/
Copy pathProgram.cs
File metadata and controls
82 lines (62 loc) · 2.27 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Microsoft.FeatureManagement;
using OpenTelemetry;
var builder = WebApplication.CreateBuilder(args);
// Use cookie auth for simplicity and randomizing user
builder.Services.AddAuthentication("CookieAuth")
.AddCookie("CookieAuth", options =>
{
options.LoginPath = "/RandomizeUser";
});
//
// What a web app using OpenTelemetry looks like
//
// Add services to the container.
builder.Services.AddRazorPages();
//
// UseAzureMonitor() (from Azure.Monitor.OpenTelemetry.AspNetCore) wires up tracing, logging, and
// metrics along with the Azure Monitor exporter for all three in a single call.
// It requires a valid connection string, so it's only added when one is configured
// (via "APPLICATIONINSIGHTS_CONNECTION_STRING" in appsettings.json below,
// or as an environment variable of the same name); otherwise it would throw at startup.
string connectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
OpenTelemetryBuilder openTelemetryBuilder = builder.Services.AddOpenTelemetry();
// Register targeting processors before exporters so TargetingId is added before telemetry is exported.
// Keep AddFeatureManagementProcessors() before the UseAzureMonitor() exporter configuration below.
openTelemetryBuilder.AddFeatureManagementProcessors();
if (!string.IsNullOrEmpty(connectionString))
{
openTelemetryBuilder.UseAzureMonitor(o => o.ConnectionString = connectionString);
}
openTelemetryBuilder
.WithMetrics(metrics => metrics.AddMeter("OpenTelemetryDemo"));
//
// Enter feature management
//
// Enhance a web application with feature management
// Including user targeting capability
// OpenTelemetry evaluation event emission is configured above.
builder.Services.AddFeatureManagement()
.WithTargeting();
//
// Default code from .NET template below
//
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
//
// Add Targeting Id to HttpContext
app.UseMiddleware<TargetingHttpContextMiddleware>();
app.Run();