opentelemetry-dotnet-instru.../samples/Samples.AspNetCoreMvc31/Controllers/HomeController.cs

88 lines
3.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Samples.AspNetCoreMvc.Shared;
namespace Samples.AspNetCoreMvc.Controllers
{
public class HomeController : Controller
{
private const string CorrelationIdentifierHeaderName = "sample.correlation.identifier";
public IActionResult Index()
{
var instrumentationType = Type.GetType("OpenTelemetry.ClrProfiler.Instrumentation, OpenTelemetry.ClrProfiler.Managed");
ViewBag.ProfilerAttached = instrumentationType?.GetProperty("ProfilerAttached", BindingFlags.Public | BindingFlags.Static)?.GetValue(null) ?? false;
ViewBag.TracerAssemblyLocation = Type.GetType("OpenTelemetry.Trace.Tracer, OpenTelemetry.Api")?.Assembly.Location;
ViewBag.ClrProfilerAssemblyLocation = instrumentationType?.Assembly.Location;
ViewBag.StackTrace = StackTraceHelper.GetUsefulStack();
var prefixes = new[] { "COR_", "CORECLR_", "DOTNET_", "OTEL_" };
var envVars = from envVar in Environment.GetEnvironmentVariables().Cast<DictionaryEntry>()
from prefix in prefixes
let key = (envVar.Key as string)?.ToUpperInvariant()
let value = envVar.Value as string
where key.StartsWith(prefix)
orderby key
select new KeyValuePair<string, string>(key, value);
AddCorrelationIdentifierToResponse();
return View(envVars.ToList());
}
[Route("delay/{seconds}")]
public IActionResult Delay(int seconds)
{
ViewBag.StackTrace = StackTraceHelper.GetUsefulStack();
Thread.Sleep(TimeSpan.FromSeconds(seconds));
AddCorrelationIdentifierToResponse();
return View(seconds);
}
[Route("delay-async/{seconds}")]
public async Task<IActionResult> DelayAsync(int seconds)
{
ViewBag.StackTrace = StackTraceHelper.GetUsefulStack();
await Task.Delay(TimeSpan.FromSeconds(seconds));
AddCorrelationIdentifierToResponse();
return View("Delay", seconds);
}
[Route("bad-request")]
public IActionResult ThrowException()
{
AddCorrelationIdentifierToResponse();
throw new Exception("This was a bad request.");
}
[Route("status-code/{statusCode}")]
public string StatusCodeTest(int statusCode)
{
AddCorrelationIdentifierToResponse();
HttpContext.Response.StatusCode = statusCode;
return $"Status code has been set to {statusCode}";
}
[Route("alive-check")]
public string IsAlive()
{
AddCorrelationIdentifierToResponse();
return "Yes";
}
private void AddCorrelationIdentifierToResponse()
{
if (Request.Headers.ContainsKey(CorrelationIdentifierHeaderName))
{
Response.Headers.Add(CorrelationIdentifierHeaderName, Request.Headers[CorrelationIdentifierHeaderName]);
}
}
}
}