|
| 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; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Text.Encodings.Web; |
| 10 | +using System.Text.Json; |
| 11 | +using System.Text.Json.Serialization; |
| 12 | +using System.Threading; |
| 13 | +using System.Threading.Tasks; |
| 14 | +using Elastic.Apm.Api; |
| 15 | +using Elastic.Apm.Report; |
| 16 | +using Elastic.Channels; |
| 17 | +using Elastic.Ingest.Transport; |
| 18 | +using Elastic.Transport; |
| 19 | + |
| 20 | +namespace Elastic.Apm.Ingest; |
| 21 | + |
| 22 | +internal static class ApmChannelStatics |
| 23 | +{ |
| 24 | + public static readonly byte[] LineFeed = { (byte)'\n' }; |
| 25 | + |
| 26 | + public static readonly DefaultRequestParameters RequestParams = new() |
| 27 | + { |
| 28 | + RequestConfiguration = new RequestConfiguration { ContentType = "application/x-ndjson" } |
| 29 | + }; |
| 30 | + |
| 31 | + public static readonly JsonSerializerOptions SerializerOptions = new() |
| 32 | + { |
| 33 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, MaxDepth = 64, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/// <summary> |
| 38 | +/// An <see cref="TransportChannelBase{TChannelOptions,TEvent,TResponse,TBulkResponseItem}"/> implementation that sends V2 intake API data |
| 39 | +/// to APM server. |
| 40 | +/// </summary> |
| 41 | +public class ApmChannel |
| 42 | + : TransportChannelBase<ApmChannelOptions, IIntakeRoot, EventIntakeResponse, IntakeErrorItem> |
| 43 | + , IPayloadSender |
| 44 | +{ |
| 45 | + /// <inheritdoc cref="ApmChannel"/> |
| 46 | + public ApmChannel(ApmChannelOptions options) : base(options) { } |
| 47 | + |
| 48 | + void IPayloadSender.QueueError(IError error) => TryWrite(error); |
| 49 | + |
| 50 | + void IPayloadSender.QueueMetrics(IMetricSet metrics) => TryWrite(metrics); |
| 51 | + |
| 52 | + void IPayloadSender.QueueSpan(ISpan span) => TryWrite(span); |
| 53 | + |
| 54 | + void IPayloadSender.QueueTransaction(ITransaction transaction) => TryWrite(transaction); |
| 55 | + |
| 56 | + //retry if APM server returns 429 |
| 57 | + /// <inheritdoc cref="ResponseItemsBufferedChannelBase{TChannelOptions,TEvent,TResponse,TBulkResponseItem}.Retry"/> |
| 58 | + protected override bool Retry(EventIntakeResponse response) => response.ApiCallDetails.HttpStatusCode == 429; |
| 59 | + |
| 60 | + /// <inheritdoc cref="ResponseItemsBufferedChannelBase{TChannelOptions,TEvent,TResponse,TBulkResponseItem}.RetryAllItems"/> |
| 61 | + protected override bool RetryAllItems(EventIntakeResponse response) => response.ApiCallDetails.HttpStatusCode == 429; |
| 62 | + |
| 63 | + //APM does not return the status for all events sent. Therefor we always return an empty set for individual items to retry |
| 64 | + /// <inheritdoc cref="ResponseItemsBufferedChannelBase{TChannelOptions,TEvent,TResponse,TBulkResponseItem}.Zip"/> |
| 65 | + protected override List<(IIntakeRoot, IntakeErrorItem)> Zip(EventIntakeResponse response, IReadOnlyCollection<IIntakeRoot> page) => |
| 66 | + _emptyZip; |
| 67 | + |
| 68 | + private readonly List<(IIntakeRoot, IntakeErrorItem)> _emptyZip = new(); |
| 69 | + |
| 70 | + /// <inheritdoc cref="ResponseItemsBufferedChannelBase{TChannelOptions,TEvent,TResponse,TBulkResponseItem}.RetryEvent"/> |
| 71 | + protected override bool RetryEvent((IIntakeRoot, IntakeErrorItem) @event) => false; |
| 72 | + |
| 73 | + /// <inheritdoc cref="ResponseItemsBufferedChannelBase{TChannelOptions,TEvent,TResponse,TBulkResponseItem}.RejectEvent"/> |
| 74 | + protected override bool RejectEvent((IIntakeRoot, IntakeErrorItem) @event) => false; |
| 75 | + |
| 76 | + /// <inheritdoc cref="BufferedChannelBase{TChannelOptions,TEvent,TResponse}.ExportAsync"/> |
| 77 | + protected override Task<EventIntakeResponse> ExportAsync(HttpTransport transport, ArraySegment<IIntakeRoot> page, CancellationToken ctx = default) => |
| 78 | + transport.RequestAsync<EventIntakeResponse>(HttpMethod.POST, "/intake/v2/events", |
| 79 | + PostData.StreamHandler(page, |
| 80 | + (_, _) => |
| 81 | + { |
| 82 | + /* NOT USED */ |
| 83 | + }, |
| 84 | + async (b, stream, ctx) => { await WriteBufferToStreamAsync(b, stream, ctx).ConfigureAwait(false); }) |
| 85 | + , ApmChannelStatics.RequestParams, ctx); |
| 86 | + |
| 87 | + private async Task WriteStanzaToStreamAsync(Stream stream, CancellationToken ctx) |
| 88 | + { |
| 89 | + // {"metadata":{"process":{"pid":1234,"title":"/usr/lib/jvm/java-10-openjdk-amd64/bin/java","ppid":1,"argv":["-v"]}, |
| 90 | + // "system":{"architecture":"amd64","detected_hostname":"8ec7ceb99074","configured_hostname":"host1","platform":"Linux","container":{"id":"8ec7ceb990749e79b37f6dc6cd3628633618d6ce412553a552a0fa6b69419ad4"}, |
| 91 | + // "kubernetes":{"namespace":"default","pod":{"uid":"b17f231da0ad128dc6c6c0b2e82f6f303d3893e3","name":"instrumented-java-service"},"node":{"name":"node-name"}}}, |
| 92 | + // "service":{"name":"1234_service-12a3","version":"4.3.0","node":{"configured_name":"8ec7ceb990749e79b37f6dc6cd3628633618d6ce412553a552a0fa6b69419ad4"},"environment":"production","language":{"name":"Java","version":"10.0.2"}, |
| 93 | + // "agent":{"version":"1.10.0","name":"java","ephemeral_id":"e71be9ac-93b0-44b9-a997-5638f6ccfc36"},"framework":{"name":"spring","version":"5.0.0"},"runtime":{"name":"Java","version":"10.0.2"}},"labels":{"group":"experimental","ab_testing":true,"segment":5}}} |
| 94 | + // TODO cache |
| 95 | + var p = Process.GetCurrentProcess(); |
| 96 | + var metadata = new |
| 97 | + { |
| 98 | + metadata = new |
| 99 | + { |
| 100 | + process = new { pid = p.Id, title = p.ProcessName }, |
| 101 | + service = new |
| 102 | + { |
| 103 | + name = System.Text.RegularExpressions.Regex.Replace(p.ProcessName, "[^a-zA-Z0-9 _-]", "_"), |
| 104 | + version = "1.0.0", |
| 105 | + agent = new { name = "dotnet", version = "0.0.1" } |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | + await JsonSerializer.SerializeAsync(stream, metadata, metadata.GetType(), ApmChannelStatics.SerializerOptions, ctx) |
| 110 | + .ConfigureAwait(false); |
| 111 | + await stream.WriteAsync(ApmChannelStatics.LineFeed, 0, 1, ctx).ConfigureAwait(false); |
| 112 | + } |
| 113 | + |
| 114 | + private async Task WriteBufferToStreamAsync(IReadOnlyCollection<IIntakeRoot> b, Stream stream, CancellationToken ctx) |
| 115 | + { |
| 116 | + await WriteStanzaToStreamAsync(stream, ctx).ConfigureAwait(false); |
| 117 | + foreach (var @event in b) |
| 118 | + { |
| 119 | + if (@event == null) continue; |
| 120 | + |
| 121 | + var type = @event switch |
| 122 | + { |
| 123 | + ITransaction => "transaction", |
| 124 | + ISpan => "span", |
| 125 | + IError => "error", |
| 126 | + IMetricSet => "metricset", |
| 127 | + _ => "unknown" |
| 128 | + }; |
| 129 | + var dictionary = new Dictionary<string, object>() { { type, @event } }; |
| 130 | + |
| 131 | + await JsonSerializer.SerializeAsync(stream, dictionary, dictionary.GetType(), ApmChannelStatics.SerializerOptions, ctx) |
| 132 | + .ConfigureAwait(false); |
| 133 | + |
| 134 | + await stream.WriteAsync(ApmChannelStatics.LineFeed, 0, 1, ctx).ConfigureAwait(false); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments