From 973d8b1ea00c79bb9ba15dd84a6b2b43fcb742d7 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Thu, 1 Feb 2024 14:06:18 +0100 Subject: [PATCH 1/5] 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; From 32396f1af5525567d76d6c3032ce9af9f82c2bc2 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Thu, 1 Feb 2024 14:11:10 +0100 Subject: [PATCH 2/5] fix missing space --- src/Elastic.Apm/Report/PayloadSenderV2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Elastic.Apm/Report/PayloadSenderV2.cs b/src/Elastic.Apm/Report/PayloadSenderV2.cs index b3e9b1c31..df4b52fde 100644 --- a/src/Elastic.Apm/Report/PayloadSenderV2.cs +++ b/src/Elastic.Apm/Report/PayloadSenderV2.cs @@ -89,7 +89,7 @@ public PayloadSenderV2( _apmServerInfo = apmServerInfo ?? new ApmServerInfo(); _serverInfoCallback = serverInfoCallback; var process = ProcessInformation.Create(); - _metadata = new Metadata { Service = service, System = System, Process = process}; + _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; From c54294dd8c909c1ae1193df13b798d5c3cf00a7e Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Thu, 1 Feb 2024 14:39:35 +0100 Subject: [PATCH 3/5] ensure process is available on mock apm server too --- test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs b/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs index e72fd9963..490ba8c1b 100644 --- a/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs +++ b/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs @@ -6,6 +6,7 @@ using Elastic.Apm.Api; using Elastic.Apm.Config; using Elastic.Apm.Helpers; +using Elastic.Apm.Model; // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedAutoPropertyAccessor.Global @@ -17,6 +18,7 @@ namespace Elastic.Apm.Tests.MockApmServer internal class MetadataDto : IDto { public Service Service { get; set; } + public ProcessInformation Process { get; set; } public Api.System System { get; set; } public Api.Cloud Cloud { get; set; } public Dictionary Labels { get; set; } From 2bc131f1a8a5c82969813ce0dc57dbfb0aae779b Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Thu, 1 Feb 2024 14:42:33 +0100 Subject: [PATCH 4/5] move ProcessInformation to API and implement a ToString() --- src/Elastic.Apm/{Model => Api}/ProcessInformation.cs | 8 +++++++- test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) rename src/Elastic.Apm/{Model => Api}/ProcessInformation.cs (71%) diff --git a/src/Elastic.Apm/Model/ProcessInformation.cs b/src/Elastic.Apm/Api/ProcessInformation.cs similarity index 71% rename from src/Elastic.Apm/Model/ProcessInformation.cs rename to src/Elastic.Apm/Api/ProcessInformation.cs index d1f916e94..0b1666193 100644 --- a/src/Elastic.Apm/Model/ProcessInformation.cs +++ b/src/Elastic.Apm/Api/ProcessInformation.cs @@ -4,8 +4,9 @@ // See the LICENSE file in the project root for more information using System.Diagnostics; +using Elastic.Apm.Helpers; -namespace Elastic.Apm.Model; +namespace Elastic.Apm.Api; internal class ProcessInformation { @@ -19,4 +20,9 @@ public static ProcessInformation Create() return new ProcessInformation { Pid = p.Id, Title = p.ProcessName }; } + public override string ToString() => new ToStringBuilder(nameof(Service)) + { + { nameof(Pid), Pid }, + { nameof(Title), Title } + }.ToString(); } diff --git a/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs b/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs index 490ba8c1b..93b992bb3 100644 --- a/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs +++ b/test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs @@ -27,6 +27,7 @@ public override string ToString() => new ToStringBuilder(nameof(MetadataDto)) { { nameof(Service), Service }, + { nameof(Process), Process }, { nameof(System), System }, { nameof(Labels), AbstractConfigurationReader.ToLogString(Labels) }, { nameof(Cloud), Cloud }, From 8c3af1bdd3f610c5bb605972435190a764995762 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Thu, 1 Feb 2024 15:02:22 +0100 Subject: [PATCH 5/5] add max length validation to process.title --- src/Elastic.Apm/Api/ProcessInformation.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Elastic.Apm/Api/ProcessInformation.cs b/src/Elastic.Apm/Api/ProcessInformation.cs index 0b1666193..83187979e 100644 --- a/src/Elastic.Apm/Api/ProcessInformation.cs +++ b/src/Elastic.Apm/Api/ProcessInformation.cs @@ -4,6 +4,7 @@ // See the LICENSE file in the project root for more information using System.Diagnostics; +using Elastic.Apm.Api.Constraints; using Elastic.Apm.Helpers; namespace Elastic.Apm.Api; @@ -12,6 +13,7 @@ internal class ProcessInformation { public int Pid { get; set; } + [MaxLength] public string Title { get; set; } public static ProcessInformation Create()