|
| 1 | +using System.Diagnostics; |
1 | 2 | using LaunchDarkly.Observability; |
2 | 3 | using LaunchDarkly.Sdk; |
3 | 4 | using LaunchDarkly.Sdk.Server; |
|
35 | 36 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
36 | 37 | }; |
37 | 38 |
|
| 39 | +app.MapGet("/recordexception", () => |
| 40 | + { |
| 41 | + Observe.RecordException(new InvalidOperationException("this is a recorded exception"), |
| 42 | + new Dictionary<string, object> |
| 43 | + { |
| 44 | + { "key", "value" }, |
| 45 | + }); |
| 46 | + return ""; |
| 47 | + }) |
| 48 | + .WithName("GetRecordException") |
| 49 | + .WithOpenApi(); |
| 50 | + |
| 51 | +app.MapGet("/recordmetrics", () => |
| 52 | + { |
| 53 | + var random = new Random(); |
| 54 | + |
| 55 | + // Record a gauge metric (CPU usage percentage) |
| 56 | + var cpuUsage = Math.Round(random.NextDouble() * 100, 2); |
| 57 | + Observe.RecordMetric("cpu_usage_percent", cpuUsage, new Dictionary<string, object> |
| 58 | + { |
| 59 | + { "environment", "development" }, |
| 60 | + { "service", "asp-sample" } |
| 61 | + }); |
| 62 | + |
| 63 | + // Record a counter with random value (requests processed) |
| 64 | + var requestsProcessed = random.Next(1, 100); |
| 65 | + Observe.RecordCount("requests_processed", requestsProcessed, new Dictionary<string, object> |
| 66 | + { |
| 67 | + { "operation", "test" }, |
| 68 | + { "status", "success" } |
| 69 | + }); |
| 70 | + |
| 71 | + // Record an increment (counter with value 1) |
| 72 | + Observe.RecordIncr("endpoint_hits", new Dictionary<string, object> |
| 73 | + { |
| 74 | + { "endpoint", "/recordmetrics" }, |
| 75 | + { "method", "GET" } |
| 76 | + }); |
| 77 | + |
| 78 | + // Record a histogram value (request duration in seconds) |
| 79 | + var requestDuration = Math.Round(random.NextDouble() * 2.0, 3); // 0-2 seconds |
| 80 | + Observe.RecordHistogram("request_duration_seconds", requestDuration, new Dictionary<string, object> |
| 81 | + { |
| 82 | + { "handler", "recordmetrics" }, |
| 83 | + { "response_code", "200" } |
| 84 | + }); |
| 85 | + |
| 86 | + // Record an up-down counter (active connections - positive) |
| 87 | + var connectionDelta = random.Next(1, 10); |
| 88 | + Observe.RecordUpDownCounter("active_connections", connectionDelta, new Dictionary<string, object> |
| 89 | + { |
| 90 | + { "connection_type", "http" }, |
| 91 | + { "region", "us-east-1" } |
| 92 | + }); |
| 93 | + |
| 94 | + // Record another up-down counter with negative delta (queue items processed) |
| 95 | + var queueDelta = -random.Next(1, 5); |
| 96 | + Observe.RecordUpDownCounter("queue_size", queueDelta, new Dictionary<string, object> |
| 97 | + { |
| 98 | + { "queue_name", "processing" }, |
| 99 | + { "priority", "high" } |
| 100 | + }); |
| 101 | + |
| 102 | + return new |
| 103 | + { |
| 104 | + message = "Metrics recorded successfully", |
| 105 | + metrics = new |
| 106 | + { |
| 107 | + gauge = $"cpu_usage_percent: {cpuUsage}%", |
| 108 | + counter = $"requests_processed: +{requestsProcessed}", |
| 109 | + increment = "endpoint_hits: +1", |
| 110 | + histogram = $"request_duration_seconds: {requestDuration}s", |
| 111 | + upDownCounter1 = $"active_connections: +{connectionDelta}", |
| 112 | + upDownCounter2 = $"queue_size: {queueDelta}" |
| 113 | + } |
| 114 | + }; |
| 115 | + }) |
| 116 | + .WithName("GetRecordMetrics") |
| 117 | + .WithOpenApi(); |
| 118 | + |
38 | 119 | app.MapGet("/weatherforecast", () => |
39 | 120 | { |
40 | 121 | var isMercury = |
|
52 | 133 | .WithName("GetWeatherForecast") |
53 | 134 | .WithOpenApi(); |
54 | 135 |
|
55 | | -app.MapGet("/crash", () => |
56 | | -{ |
57 | | - throw new NotImplementedException(); |
58 | | -}).WithName("Crash").WithOpenApi(); |
| 136 | +app.MapGet("/recordlog", () => |
| 137 | + { |
| 138 | + var random = new Random(); |
| 139 | + var logMessages = new[] |
| 140 | + { |
| 141 | + "User authentication successful", |
| 142 | + "Database connection established", |
| 143 | + "Cache miss occurred, falling back to database", |
| 144 | + "API rate limit approaching threshold", |
| 145 | + "Background job completed successfully" |
| 146 | + }; |
| 147 | + |
| 148 | + var severityLevels = new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Debug }; |
| 149 | + |
| 150 | + var message = logMessages[random.Next(logMessages.Length)]; |
| 151 | + var severity = severityLevels[random.Next(severityLevels.Length)]; |
| 152 | + |
| 153 | + Observe.RecordLog(message, severity, new Dictionary<string, object> |
| 154 | + { |
| 155 | + { "component", "asp-sample-app" }, |
| 156 | + { "endpoint", "/recordlog" }, |
| 157 | + { "timestamp", DateTime.UtcNow.ToString("O") }, |
| 158 | + { "user_id", Guid.NewGuid().ToString() } |
| 159 | + }); |
| 160 | + |
| 161 | + return new |
| 162 | + { |
| 163 | + message = "Log recorded successfully", |
| 164 | + log_entry = new |
| 165 | + { |
| 166 | + message, |
| 167 | + severity = severity.ToString(), |
| 168 | + component = "asp-sample-app" |
| 169 | + } |
| 170 | + }; |
| 171 | + }) |
| 172 | + .WithName("GetRecordLog") |
| 173 | + .WithOpenApi(); |
| 174 | + |
| 175 | +app.MapGet("/manualinstrumentation", () => |
| 176 | + { |
| 177 | + using (Observe.StartActivity("manual-instrumentation", ActivityKind.Internal, |
| 178 | + new Dictionary<string, object> { { "test", "attribute" } })) |
| 179 | + { |
| 180 | + var enableMetrics = client.BoolVariation("enableMetrics", |
| 181 | + Context.New(ContextKind.Of("request"), Guid.NewGuid().ToString())); |
| 182 | + |
| 183 | + if (!enableMetrics) return "Manual instrumentation completed"; |
| 184 | + Observe.RecordIncr("manual_instrumentation_calls"); |
| 185 | + return "Manual instrumentation completed with metrics enabled"; |
| 186 | + } |
| 187 | + }) |
| 188 | + .WithName("GetManualInstrumentation") |
| 189 | + .WithOpenApi(); |
| 190 | + |
| 191 | +app.MapGet("/crash", () => { throw new NotImplementedException(); }).WithName("Crash").WithOpenApi(); |
59 | 192 |
|
60 | 193 | app.Run(); |
61 | 194 |
|
|
0 commit comments