-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
58 lines (46 loc) · 2.17 KB
/
Program.cs
File metadata and controls
58 lines (46 loc) · 2.17 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Examples.Service;
using Microsoft.Data.SqlClient;
// .NET Diagnostics: create the span factory
using var activitySource = new ActivitySource("Examples.Service");
// .NET Diagnostics: create a metric
using var meter = new Meter("Examples.Service", "1.0");
var successCounter = meter.CreateCounter<long>("srv.successes.count", description: "Number of successful responses");
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var connectionString = System.Environment.GetEnvironmentVariable("DB_CONNECTION");
app.MapGet("/", Handler);
app.Run();
async Task<string> Handler(ILogger<Program> logger)
{
await ExecuteSql("SELECT 1").ConfigureAwait(false);
// .NET Diagnostics: create a manual span
using (var activity = activitySource.StartActivity("SayHello"))
{
activity?.SetTag("foo", 1);
activity?.SetTag("bar", "Hello, World!");
activity?.SetTag("baz", (int[])[1, 2, 3]);
#pragma warning disable CA5394 // Do not use insecure randomness. Not related to security, just a demo.
var waitTime = Random.Shared.NextDouble(); // max 1 seconds
#pragma warning restore CA5394 // Do not use insecure randomness. Not related to security, just a demo.
await Task.Delay(TimeSpan.FromSeconds(waitTime)).ConfigureAwait(false);
activity?.SetStatus(ActivityStatusCode.Ok);
// .NET Diagnostics: update the metric
successCounter.Add(1);
}
// .NET ILogger: create a log
logger.Success(DateTimeOffset.UtcNow);
return "Hello there";
}
async Task ExecuteSql(string sql)
{
using var connection = new SqlConnection(connectionString);
await connection.OpenAsync().ConfigureAwait(false);
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities. It is static SQL for demo purposes.
using var command = new SqlCommand(sql, connection);
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities. It is static SQL for demo purposes.
using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
}