|
| 1 | +// <copyright file="InstrumentationWithActivitySource.cs" company="OpenTelemetry Authors"> |
| 2 | +// Copyright The OpenTelemetry Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Diagnostics; |
| 20 | +using System.Globalization; |
| 21 | +using System.IO; |
| 22 | +using System.Net; |
| 23 | +using System.Net.Http; |
| 24 | +using System.Text; |
| 25 | +using System.Threading; |
| 26 | +using System.Threading.Tasks; |
| 27 | + |
| 28 | +namespace Samples |
| 29 | +{ |
| 30 | + internal class InstrumentationWithActivitySource : IDisposable |
| 31 | + { |
| 32 | + private const string RequestPath = "/api/request"; |
| 33 | + private SampleServer server = new SampleServer(); |
| 34 | + private SampleClient client = new SampleClient(); |
| 35 | + |
| 36 | + public void Start(ushort port = 19999) |
| 37 | + { |
| 38 | + var url = $"http://localhost:{port.ToString(CultureInfo.InvariantCulture)}{RequestPath}/"; |
| 39 | + this.server.Start(url); |
| 40 | + this.client.Start(url); |
| 41 | + } |
| 42 | + |
| 43 | + public void Dispose() |
| 44 | + { |
| 45 | + this.client.Dispose(); |
| 46 | + this.server.Dispose(); |
| 47 | + } |
| 48 | + |
| 49 | + private class SampleServer : IDisposable |
| 50 | + { |
| 51 | + private HttpListener listener = new HttpListener(); |
| 52 | + |
| 53 | + public void Start(string url) |
| 54 | + { |
| 55 | + this.listener.Prefixes.Add(url); |
| 56 | + this.listener.Start(); |
| 57 | + |
| 58 | + Task.Run(() => |
| 59 | + { |
| 60 | + using var source = new ActivitySource("Samples.SampleServer"); |
| 61 | + |
| 62 | + while (this.listener.IsListening) |
| 63 | + { |
| 64 | + try |
| 65 | + { |
| 66 | + var context = this.listener.GetContext(); |
| 67 | + |
| 68 | + using var activity = source.StartActivity( |
| 69 | + $"{context.Request.HttpMethod}:{context.Request.Url.AbsolutePath}", |
| 70 | + ActivityKind.Server); |
| 71 | + |
| 72 | + var headerKeys = context.Request.Headers.AllKeys; |
| 73 | + foreach (var headerKey in headerKeys) |
| 74 | + { |
| 75 | + string headerValue = context.Request.Headers[headerKey]; |
| 76 | + activity?.AddTag($"http.header.{headerKey}", headerValue); |
| 77 | + } |
| 78 | + |
| 79 | + string requestContent; |
| 80 | + using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding)) |
| 81 | + { |
| 82 | + requestContent = reader.ReadToEnd(); |
| 83 | + } |
| 84 | + |
| 85 | + activity?.AddTag("request.content", requestContent); |
| 86 | + activity?.AddTag("request.length", requestContent.Length.ToString()); |
| 87 | + |
| 88 | + var echo = Encoding.UTF8.GetBytes("echo: " + requestContent); |
| 89 | + context.Response.ContentEncoding = Encoding.UTF8; |
| 90 | + context.Response.ContentLength64 = echo.Length; |
| 91 | + context.Response.OutputStream.Write(echo, 0, echo.Length); |
| 92 | + context.Response.Close(); |
| 93 | + } |
| 94 | + catch (Exception) |
| 95 | + { |
| 96 | + // expected when closing the listener. |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + public void Dispose() |
| 103 | + { |
| 104 | + ((IDisposable)this.listener).Dispose(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private class SampleClient : IDisposable |
| 109 | + { |
| 110 | + private CancellationTokenSource cts; |
| 111 | + private Task requestTask; |
| 112 | + |
| 113 | + public void Start(string url) |
| 114 | + { |
| 115 | + this.cts = new CancellationTokenSource(); |
| 116 | + var cancellationToken = this.cts.Token; |
| 117 | + |
| 118 | + this.requestTask = Task.Run(async () => |
| 119 | + { |
| 120 | + using var source = new ActivitySource("Samples.SampleClient"); |
| 121 | + using var client = new HttpClient(); |
| 122 | + |
| 123 | + var count = 1; |
| 124 | + while (!cancellationToken.IsCancellationRequested) |
| 125 | + { |
| 126 | + var content = new StringContent($"client message: {DateTime.Now}", Encoding.UTF8); |
| 127 | + |
| 128 | + using (var activity = source.StartActivity("POST:" + RequestPath, ActivityKind.Client)) |
| 129 | + { |
| 130 | + count++; |
| 131 | + |
| 132 | + activity?.AddEvent(new ActivityEvent("PostAsync:Started")); |
| 133 | + using var response = await client.PostAsync(url, content, cancellationToken).ConfigureAwait(false); |
| 134 | + activity?.AddEvent(new ActivityEvent("PostAsync:Ended")); |
| 135 | + |
| 136 | + activity?.AddTag("http.status_code", $"{response.StatusCode:D}"); |
| 137 | + |
| 138 | + var responseContent = await response.Content.ReadAsStringAsync(); |
| 139 | + activity?.AddTag("response.content", responseContent); |
| 140 | + activity?.AddTag("response.length", responseContent.Length.ToString(CultureInfo.InvariantCulture)); |
| 141 | + |
| 142 | + foreach (var header in response.Headers) |
| 143 | + { |
| 144 | + if (header.Value is IEnumerable<object> enumerable) |
| 145 | + { |
| 146 | + activity?.AddTag($"http.header.{header.Key}", string.Join(",", enumerable)); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + activity?.AddTag($"http.header.{header.Key}", header.Value.ToString()); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + try |
| 156 | + { |
| 157 | + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false); |
| 158 | + } |
| 159 | + catch (TaskCanceledException) |
| 160 | + { |
| 161 | + return; |
| 162 | + } |
| 163 | + } |
| 164 | + }, |
| 165 | + cancellationToken); |
| 166 | + } |
| 167 | + |
| 168 | + public void Dispose() |
| 169 | + { |
| 170 | + if (this.cts != null) |
| 171 | + { |
| 172 | + this.cts.Cancel(); |
| 173 | + this.requestTask.Wait(); |
| 174 | + this.requestTask.Dispose(); |
| 175 | + this.cts.Dispose(); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments