diff --git a/SampleApi/Controllers/DemoController.cs b/SampleApi/Controllers/DemoController.cs index 7052ec2..5860145 100644 --- a/SampleApi/Controllers/DemoController.cs +++ b/SampleApi/Controllers/DemoController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -8,10 +9,17 @@ namespace SampleApi.Controllers [ApiController] public class DemoController : ControllerBase { + private static readonly ActivitySource MyActivitySource = new("SampleApi"); + // GET: api/ [HttpGet] - public IEnumerable Get() + public IEnumerable Get(ILogger logger) { + using var activity = MyActivitySource.StartActivity("ManualTraceTest"); + activity?.SetTag("test.status", "success"); + + logger.LogInformation("sample api log"); + return new string[] { "value1", "value2" }; } diff --git a/SampleApi/Program.cs b/SampleApi/Program.cs index 6613678..8581ec2 100644 --- a/SampleApi/Program.cs +++ b/SampleApi/Program.cs @@ -1,12 +1,67 @@ +using OpenTelemetry.Exporter; +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; +using System.Diagnostics.Metrics; + var builder = WebApplication.CreateBuilder(args); +// 1. Define Resource Information +var resourceBuilder = ResourceBuilder.CreateDefault() + .AddService("SampleApi"); + +// 2. Configuration Variables +var oltpApiKey = "mc3G63K2j5ZOEsi0AjMojLTXm1KEZFctzIIjSwDiTGut8qGSkPuWwxGP1RbscJUo"; +var oltpAddress = "http://aspire-dashboard-service.aspire.svc.cluster.local:18889"; + builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddHealthChecks(); +// 3. Configure OpenTelemetry Logging +builder.Logging.AddOpenTelemetry(logging => +{ + logging.SetResourceBuilder(resourceBuilder); + logging.AddOtlpExporter(opt => + { + opt.Endpoint = new Uri(oltpAddress); + opt.Protocol = OtlpExportProtocol.Grpc; + opt.Headers = $"x-otlp-api-key={oltpApiKey}"; + }); +}); + +// 4. Configure Tracing and Metrics (Merged into one registration) +builder.Services.AddOpenTelemetry() + .WithTracing(tracing => tracing + .SetResourceBuilder(resourceBuilder) + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddOtlpExporter(opt => + { + opt.Endpoint = new Uri(oltpAddress); + opt.Protocol = OtlpExportProtocol.Grpc; + opt.Headers = $"x-otlp-api-key={oltpApiKey}"; + })) + .WithMetrics(metrics => metrics + .SetResourceBuilder(resourceBuilder) + .AddMeter("SampleApi") // Register your custom meter here + .AddAspNetCoreInstrumentation() + .AddRuntimeInstrumentation() + .AddOtlpExporter(opt => + { + opt.Endpoint = new Uri(oltpAddress); + opt.Protocol = OtlpExportProtocol.Grpc; + opt.Headers = $"x-otlp-api-key={oltpApiKey}"; + })); + var app = builder.Build(); +// 5. Initialize your Meter for use in the app +var myMeter = new Meter("SampleApi", "1.0.0"); +var transactionCounter = myMeter.CreateCounter("CustomCounter"); + if (app.Environment.IsDevelopment()) { app.UseSwagger(); @@ -15,8 +70,7 @@ if (app.Environment.IsDevelopment()) app.MapHealthChecks("/health"); app.UseRouting(); -app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/SampleApi/SampleApi.csproj b/SampleApi/SampleApi.csproj index 998149e..fafe367 100644 --- a/SampleApi/SampleApi.csproj +++ b/SampleApi/SampleApi.csproj @@ -10,6 +10,11 @@ + + + + +