|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Diagnostics; |
| 6 | +using Elastic.OpenTelemetry.Diagnostics; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using OpenTelemetry; |
| 9 | +using static Elastic.OpenTelemetry.SemanticConventions.TraceSemanticConventions; |
| 10 | + |
| 11 | +namespace Elastic.OpenTelemetry.Processors; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// This processor ensures that the data is compatible with Elastic backends. |
| 15 | +/// <para> |
| 16 | +/// It checks for the presence of the older semantic conventions and if they are not present, it will |
| 17 | +/// add them. This is only necessary for compatibility with older versions of the intake OTel endpoints |
| 18 | +/// on Elastic APM. These issues will be fixed centrally in future versions of the intake code. |
| 19 | +/// </para> |
| 20 | +/// </summary> |
| 21 | +/// <param name="logger"></param> |
| 22 | +public class ElasticCompatibilityProcessor(ILogger logger) : BaseProcessor<Activity> |
| 23 | +{ |
| 24 | + private readonly ILogger _logger = logger; |
| 25 | + |
| 26 | + /// <inheritdoc /> |
| 27 | + public override void OnEnd(Activity activity) |
| 28 | + { |
| 29 | + if (activity.Kind == ActivityKind.Server) |
| 30 | + { |
| 31 | + // For inbound HTTP requests (server), ASP.NET Core sets the newer semantic conventions in |
| 32 | + // the latest versions. For now, we need to ensure the older semantic conventions are also |
| 33 | + // included on the spans sent to the Elastic backend as the intake system is currently |
| 34 | + // unaware of the newer semantic conventions. We send the older attributes to ensure that |
| 35 | + // the UI functions as expected. The http and net host conventions are required to build |
| 36 | + // up the URL displayed in the trace sample UI within Kibana. This will be fixed in future |
| 37 | + // version of apm-data. |
| 38 | + |
| 39 | + string? httpScheme = null; |
| 40 | + string? httpTarget = null; |
| 41 | + string? urlScheme = null; |
| 42 | + string? urlPath = null; |
| 43 | + string? urlQuery = null; |
| 44 | + string? netHostName = null; |
| 45 | + int? netHostPort = null; |
| 46 | + string? serverAddress = null; |
| 47 | + int? serverPort = null; |
| 48 | + |
| 49 | + // We loop once, collecting all the attributes we need for the older and newer |
| 50 | + // semantic conventions. This is a bit more verbose but ensures we don't iterate |
| 51 | + // the tags multiple times. |
| 52 | + foreach (var tag in activity.TagObjects) |
| 53 | + { |
| 54 | + if (tag.Key == HttpScheme) |
| 55 | + httpScheme = ProcessStringAttribute(tag); |
| 56 | + |
| 57 | + if (tag.Key == HttpTarget) |
| 58 | + httpTarget = ProcessStringAttribute(tag); |
| 59 | + |
| 60 | + if (tag.Key == UrlScheme) |
| 61 | + urlScheme = ProcessStringAttribute(tag); |
| 62 | + |
| 63 | + if (tag.Key == UrlPath) |
| 64 | + urlPath = ProcessStringAttribute(tag); |
| 65 | + |
| 66 | + if (tag.Key == UrlQuery) |
| 67 | + urlQuery = ProcessStringAttribute(tag); |
| 68 | + |
| 69 | + if (tag.Key == NetHostName) |
| 70 | + netHostName = ProcessStringAttribute(tag); |
| 71 | + |
| 72 | + if (tag.Key == ServerAddress) |
| 73 | + serverAddress = ProcessStringAttribute(tag); |
| 74 | + |
| 75 | + if (tag.Key == NetHostPort) |
| 76 | + netHostPort = ProcessIntAttribute(tag); |
| 77 | + |
| 78 | + if (tag.Key == ServerPort) |
| 79 | + serverPort = ProcessIntAttribute(tag); |
| 80 | + } |
| 81 | + |
| 82 | + // Set the older semantic convention attributes |
| 83 | + if (httpScheme is null && urlScheme is not null) |
| 84 | + SetStringAttribute(HttpScheme, urlScheme); |
| 85 | + |
| 86 | + if (httpTarget is null && urlPath is not null) |
| 87 | + { |
| 88 | + var target = urlPath; |
| 89 | + |
| 90 | + if (urlQuery is not null) |
| 91 | + target += $"?{urlQuery}"; |
| 92 | + |
| 93 | + SetStringAttribute(HttpTarget, target); |
| 94 | + } |
| 95 | + |
| 96 | + if (netHostName is null && serverAddress is not null) |
| 97 | + SetStringAttribute(NetHostName, serverAddress); |
| 98 | + |
| 99 | + if (netHostPort is null && serverPort is not null) |
| 100 | + SetIntAttribute(NetHostPort, serverPort.Value); |
| 101 | + } |
| 102 | + |
| 103 | + string? ProcessStringAttribute(KeyValuePair<string, object?> tag) |
| 104 | + { |
| 105 | + if (tag.Value is string value) |
| 106 | + { |
| 107 | + _logger.FoundTag(nameof(ElasticCompatibilityProcessor), tag.Key, value); |
| 108 | + return value; |
| 109 | + } |
| 110 | + |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + int? ProcessIntAttribute(KeyValuePair<string, object?> tag) |
| 115 | + { |
| 116 | + if (tag.Value is int value) |
| 117 | + { |
| 118 | + _logger.FoundTag(nameof(ElasticCompatibilityProcessor), tag.Key, value); |
| 119 | + return value; |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + void SetStringAttribute(string attributeName, string value) |
| 126 | + { |
| 127 | + _logger.SetTag(nameof(ElasticCompatibilityProcessor), attributeName, value); |
| 128 | + activity.SetTag(attributeName, value); |
| 129 | + } |
| 130 | + |
| 131 | + void SetIntAttribute(string attributeName, int value) |
| 132 | + { |
| 133 | + _logger.SetTag(nameof(ElasticCompatibilityProcessor), attributeName, value); |
| 134 | + activity.SetTag(attributeName, value); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments