|
| 1 | +using System; |
| 2 | +using Amazon.S3; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Net.Http; |
| 6 | +using Microsoft.AspNetCore.Http.Extensions; |
| 7 | +using OpenTelemetry; |
| 8 | +using OpenTelemetry.Metrics; |
| 9 | +using OpenTelemetry.Instrumentation; |
| 10 | +using System.Diagnostics.Metrics; |
| 11 | +using OpenTelemetry.Resources; |
| 12 | +using OpenTelemetry.Trace; |
| 13 | + |
| 14 | +namespace dotnet_sample_app.Controllers |
| 15 | +{ |
| 16 | + [ApiController] |
| 17 | + [Route("[controller]")] |
| 18 | + public class AppController : ControllerBase |
| 19 | + { |
| 20 | + private readonly AmazonS3Client s3Client = new AmazonS3Client(); |
| 21 | + private readonly HttpClient httpClient = new HttpClient(); |
| 22 | + private static Random rand = new Random(DateTime.Now.Millisecond); |
| 23 | + |
| 24 | + public static readonly ActivitySource tracer = new( |
| 25 | + "dotnet-sample-app"); |
| 26 | + |
| 27 | + public AppController() {} |
| 28 | + |
| 29 | + [HttpGet] |
| 30 | + [Route("/outgoing-http-call")] |
| 31 | + public string OutgoingHttp() |
| 32 | + { |
| 33 | + using var activity = tracer.StartActivity("outgoing-http-call"); |
| 34 | + activity?.SetTag("language", "dotnet"); |
| 35 | + activity?.SetTag("signal", "trace"); |
| 36 | + |
| 37 | + var res = httpClient.GetAsync("https://aws.amazon.com/").Result; |
| 38 | + string statusCode = res.StatusCode.ToString(); |
| 39 | + |
| 40 | + // Request Based Metrics |
| 41 | + Startup.metricEmitter.emitReturnTimeMetric(MimicLatency()); |
| 42 | + int loadSize = MimicPayLoadSize(); |
| 43 | + Startup.metricEmitter.apiRequestSentMetric(); |
| 44 | + Startup.metricEmitter.updateTotalBytesSentMetric(loadSize); |
| 45 | + |
| 46 | + return GetTraceId(); |
| 47 | + } |
| 48 | + |
| 49 | + [HttpGet] |
| 50 | + [Route("/test")] |
| 51 | + public string AWSSDKCall() |
| 52 | + { |
| 53 | + using var activity = tracer.StartActivity("test_parent_span"); |
| 54 | + activity?.SetTag("language", "dotnet"); |
| 55 | + activity?.SetTag("signal", "trace"); |
| 56 | + activity?.SetTag("test.attribute", "test_value"); |
| 57 | + |
| 58 | + string traceId = activity?.TraceId.ToString() ?? "no-trace-available"; |
| 59 | + |
| 60 | + return traceId; |
| 61 | + } |
| 62 | + |
| 63 | + [HttpGet] |
| 64 | + [Route("/")] |
| 65 | + public string Default() |
| 66 | + { |
| 67 | + return "Application started!"; |
| 68 | + } |
| 69 | + |
| 70 | + [HttpGet] |
| 71 | + [Route("/outgoing-sampleapp")] |
| 72 | + public string OutgoingSampleApp() |
| 73 | + { |
| 74 | + using var activity = tracer.StartActivity("outgoing-sampleapp"); |
| 75 | + activity?.SetTag("language", "dotnet"); |
| 76 | + activity?.SetTag("signal", "trace"); |
| 77 | + string statusCode = ""; |
| 78 | + |
| 79 | + if (Program.cfg.SampleAppPorts.Length == 0) { |
| 80 | + var res = httpClient.GetAsync("https://aws.amazon.com/").Result; |
| 81 | + statusCode = res.StatusCode.ToString(); |
| 82 | + } |
| 83 | + else { |
| 84 | + foreach (string port in Program.cfg.SampleAppPorts) { |
| 85 | + if (!String.IsNullOrEmpty(port)) { |
| 86 | + string uri = string.Format("http://127.0.0.1:{0}/outgoing-sampleapp", port); |
| 87 | + var res = httpClient.GetAsync(uri).Result; |
| 88 | + statusCode = res.StatusCode.ToString(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Request Based Metrics |
| 94 | + Startup.metricEmitter.emitReturnTimeMetric(MimicLatency()); |
| 95 | + int loadSize = MimicPayLoadSize(); |
| 96 | + Startup.metricEmitter.apiRequestSentMetric(); |
| 97 | + Startup.metricEmitter.updateTotalBytesSentMetric(loadSize); |
| 98 | + |
| 99 | + return GetTraceId(); |
| 100 | + } |
| 101 | + |
| 102 | + private string GetTraceId() |
| 103 | + { |
| 104 | + var traceId = Activity.Current.TraceId.ToHexString(); |
| 105 | + var version = "1"; |
| 106 | + var epoch = traceId.Substring(0, 8); |
| 107 | + var random = traceId.Substring(8); |
| 108 | + return "{" + "\"traceId\"" + ": " + "\"" + version + "-" + epoch + "-" + random + "\"" + "}"; |
| 109 | + } |
| 110 | + |
| 111 | + private static int MimicPayLoadSize() |
| 112 | + { |
| 113 | + return rand.Next(101); |
| 114 | + } |
| 115 | + |
| 116 | + private static int MimicLatency() |
| 117 | + { |
| 118 | + return rand.Next(100,500); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments