|
1 | | -// <copyright file="TelemetryHttpHeaderNames.cs" company="Datadog"> |
| 1 | +// <copyright file="TelemetryHttpHeaderNames.cs" company="Datadog"> |
2 | 2 | // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
3 | 3 | // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
4 | 4 | // </copyright> |
5 | 5 |
|
6 | 6 | using System.Collections.Generic; |
7 | 7 | using Datadog.Trace.HttpOverStreams; |
| 8 | +using Datadog.Trace.Util; |
8 | 9 |
|
9 | 10 | namespace Datadog.Trace.Telemetry |
10 | 11 | { |
11 | 12 | internal static class TelemetryHttpHeaderNames |
12 | 13 | { |
13 | 14 | /// <summary> |
14 | | - /// Returns <see cref="GetDefaultAgentHeaders"/>, in the format <c>Key: Value\r\n</c>. For use in HTTP headers |
| 15 | + /// Gets the default agent headers in the format <c>Key: Value\r\n</c>. For use in HTTP headers. |
| 16 | + /// Not a const because session headers depend on runtime values. |
15 | 17 | /// </summary> |
16 | | - internal const string HttpSerializedDefaultAgentHeaders = |
17 | | - $"{TelemetryConstants.ClientLibraryLanguageHeader}: {TracerConstants.Language}" + DatadogHttpValues.CrLf + |
18 | | - $"{TelemetryConstants.ClientLibraryVersionHeader}: {TracerConstants.AssemblyVersion}" + DatadogHttpValues.CrLf + |
19 | | - $"{HttpHeaderNames.TracingEnabled}: false" + DatadogHttpValues.CrLf; |
| 18 | + internal static string HttpSerializedDefaultAgentHeaders { get; } = BuildSerializedAgentHeaders(); |
20 | 19 |
|
21 | 20 | /// <summary> |
22 | 21 | /// Gets the default constant headers that should be added to any request to the agent |
23 | 22 | /// </summary> |
24 | 23 | internal static KeyValuePair<string, string>[] GetDefaultAgentHeaders() |
25 | | - => |
26 | | - [ |
| 24 | + { |
| 25 | + var headers = new List<KeyValuePair<string, string>> |
| 26 | + { |
27 | 27 | new(TelemetryConstants.ClientLibraryLanguageHeader, TracerConstants.Language), |
28 | 28 | new(TelemetryConstants.ClientLibraryVersionHeader, TracerConstants.AssemblyVersion), |
29 | | - new(HttpHeaderNames.TracingEnabled, "false") // don't add automatic instrumentation to requests directed to the agent |
30 | | - ]; |
| 29 | + new(HttpHeaderNames.TracingEnabled, "false"), // don't add automatic instrumentation to requests directed to the agent |
| 30 | + new(TelemetryConstants.SessionIdHeader, RuntimeId.Get()), |
| 31 | + }; |
| 32 | + |
| 33 | + AddRootSessionIdHeader(headers); |
| 34 | + |
| 35 | + return headers.ToArray(); |
| 36 | + } |
31 | 37 |
|
32 | 38 | /// <summary> |
33 | 39 | /// Gets the default constant headers that should be added to any request to the direct telemetry intake |
34 | 40 | /// </summary> |
35 | 41 | internal static KeyValuePair<string, string>[] GetDefaultIntakeHeaders(TelemetrySettings.AgentlessSettings settings) |
36 | 42 | { |
37 | | - var headerCount = settings.Cloud is null ? 4 : 7; |
| 43 | + var headers = new List<KeyValuePair<string, string>> |
| 44 | + { |
| 45 | + new(TelemetryConstants.ClientLibraryLanguageHeader, TracerConstants.Language), |
| 46 | + new(TelemetryConstants.ClientLibraryVersionHeader, TracerConstants.AssemblyVersion), |
| 47 | + new(HttpHeaderNames.TracingEnabled, "false"), |
| 48 | + new(TelemetryConstants.ApiKeyHeader, settings.ApiKey), |
| 49 | + new(TelemetryConstants.SessionIdHeader, RuntimeId.Get()), |
| 50 | + }; |
38 | 51 |
|
39 | | - var headers = new KeyValuePair<string, string>[headerCount]; |
| 52 | + if (settings.Cloud is { } cloud) |
| 53 | + { |
| 54 | + headers.Add(new(TelemetryConstants.CloudProviderHeader, cloud.Provider)); |
| 55 | + headers.Add(new(TelemetryConstants.CloudResourceTypeHeader, cloud.ResourceType)); |
| 56 | + headers.Add(new(TelemetryConstants.CloudResourceIdentifierHeader, cloud.ResourceIdentifier)); |
| 57 | + } |
40 | 58 |
|
41 | | - headers[0] = new(TelemetryConstants.ClientLibraryLanguageHeader, TracerConstants.Language); |
42 | | - headers[1] = new(TelemetryConstants.ClientLibraryVersionHeader, TracerConstants.AssemblyVersion); |
43 | | - headers[2] = new(HttpHeaderNames.TracingEnabled, "false"); // don't add automatic instrumentation to requests directed to the agent |
44 | | - headers[3] = new(TelemetryConstants.ApiKeyHeader, settings.ApiKey); |
| 59 | + AddRootSessionIdHeader(headers); |
45 | 60 |
|
46 | | - if (settings.Cloud is { } cloud) |
| 61 | + return headers.ToArray(); |
| 62 | + } |
| 63 | + |
| 64 | + private static void AddRootSessionIdHeader(List<KeyValuePair<string, string>> headers) |
| 65 | + { |
| 66 | + var sessionId = RuntimeId.Get(); |
| 67 | + var rootSessionId = RuntimeId.GetRootSessionId(); |
| 68 | + if (rootSessionId != sessionId) |
| 69 | + { |
| 70 | + headers.Add(new(TelemetryConstants.RootSessionIdHeader, rootSessionId)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static string BuildSerializedAgentHeaders() |
| 75 | + { |
| 76 | + var serialized = |
| 77 | + $"{TelemetryConstants.ClientLibraryLanguageHeader}: {TracerConstants.Language}" + DatadogHttpValues.CrLf + |
| 78 | + $"{TelemetryConstants.ClientLibraryVersionHeader}: {TracerConstants.AssemblyVersion}" + DatadogHttpValues.CrLf + |
| 79 | + $"{HttpHeaderNames.TracingEnabled}: false" + DatadogHttpValues.CrLf + |
| 80 | + $"{TelemetryConstants.SessionIdHeader}: {RuntimeId.Get()}" + DatadogHttpValues.CrLf; |
| 81 | + |
| 82 | + var sessionId = RuntimeId.Get(); |
| 83 | + var rootSessionId = RuntimeId.GetRootSessionId(); |
| 84 | + if (rootSessionId != sessionId) |
47 | 85 | { |
48 | | - headers[4] = new(TelemetryConstants.CloudProviderHeader, cloud.Provider); |
49 | | - headers[5] = new(TelemetryConstants.CloudResourceTypeHeader, cloud.ResourceType); |
50 | | - headers[6] = new(TelemetryConstants.CloudResourceIdentifierHeader, cloud.ResourceIdentifier); |
| 86 | + serialized += $"{TelemetryConstants.RootSessionIdHeader}: {rootSessionId}" + DatadogHttpValues.CrLf; |
51 | 87 | } |
52 | 88 |
|
53 | | - return headers; |
| 89 | + return serialized; |
54 | 90 | } |
55 | 91 | } |
56 | 92 | } |
0 commit comments