From 973d8b1ea00c79bb9ba15dd84a6b2b43fcb742d7 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Thu, 1 Feb 2024 14:06:18 +0100 Subject: [PATCH] Include process information in metadata stanza when emiting events to apm-server --- src/Elastic.Apm/Model/Metadata.cs | 2 ++ src/Elastic.Apm/Model/ProcessInformation.cs | 22 +++++++++++++++++++++ src/Elastic.Apm/Report/PayloadSenderV2.cs | 3 ++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/Elastic.Apm/Model/ProcessInformation.cs diff --git a/src/Elastic.Apm/Model/Metadata.cs b/src/Elastic.Apm/Model/Metadata.cs index 7ac8940b9..38fc9eeed 100644 --- a/src/Elastic.Apm/Model/Metadata.cs +++ b/src/Elastic.Apm/Model/Metadata.cs @@ -18,6 +18,8 @@ internal class Metadata // ReSharper disable once UnusedAutoPropertyAccessor.Global - used by Json.Net public Service Service { get; set; } + public ProcessInformation Process { get; set; } + // ReSharper disable once UnusedAutoPropertyAccessor.Global public Api.System System { get; set; } diff --git a/src/Elastic.Apm/Model/ProcessInformation.cs b/src/Elastic.Apm/Model/ProcessInformation.cs new file mode 100644 index 000000000..d1f916e94 --- /dev/null +++ b/src/Elastic.Apm/Model/ProcessInformation.cs @@ -0,0 +1,22 @@ +// Licensed to Elasticsearch B.V under +// one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information + +using System.Diagnostics; + +namespace Elastic.Apm.Model; + +internal class ProcessInformation +{ + public int Pid { get; set; } + + public string Title { get; set; } + + public static ProcessInformation Create() + { + var p = Process.GetCurrentProcess(); + return new ProcessInformation { Pid = p.Id, Title = p.ProcessName }; + } + +} diff --git a/src/Elastic.Apm/Report/PayloadSenderV2.cs b/src/Elastic.Apm/Report/PayloadSenderV2.cs index 65ac6461b..b3e9b1c31 100644 --- a/src/Elastic.Apm/Report/PayloadSenderV2.cs +++ b/src/Elastic.Apm/Report/PayloadSenderV2.cs @@ -88,7 +88,8 @@ public PayloadSenderV2( _cloudMetadataProviderCollection = new CloudMetadataProviderCollection(configuration.CloudProvider, _logger, environmentVariables); _apmServerInfo = apmServerInfo ?? new ApmServerInfo(); _serverInfoCallback = serverInfoCallback; - _metadata = new Metadata { Service = service, System = System }; + var process = ProcessInformation.Create(); + _metadata = new Metadata { Service = service, System = System, Process = process}; foreach (var globalLabelKeyValue in configuration.GlobalLabels) _metadata.Labels.Add(globalLabelKeyValue.Key, globalLabelKeyValue.Value); _cachedActivationMethod = _metadata.Service?.Agent.ActivationMethod;