Skip to content

Commit

Permalink
Include process information in metadata stanza when emiting events to…
Browse files Browse the repository at this point in the history
… apm-server (#2272)

* Include process information in metadata stanza when emiting events to apm-server

* fix missing space

* ensure process is available on mock apm server too

* move ProcessInformation to API and implement a ToString()

* add max length validation to process.title
  • Loading branch information
Mpdreamz authored Feb 2, 2024
1 parent bdc8378 commit 5e74fdd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Elastic.Apm/Api/ProcessInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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;
using Elastic.Apm.Api.Constraints;
using Elastic.Apm.Helpers;

namespace Elastic.Apm.Api;

internal class ProcessInformation
{
public int Pid { get; set; }

[MaxLength]
public string Title { get; set; }

public static ProcessInformation Create()
{
var p = Process.GetCurrentProcess();
return new ProcessInformation { Pid = p.Id, Title = p.ProcessName };
}

public override string ToString() => new ToStringBuilder(nameof(Service))
{
{ nameof(Pid), Pid },
{ nameof(Title), Title }
}.ToString();
}
2 changes: 2 additions & 0 deletions src/Elastic.Apm/Model/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
3 changes: 2 additions & 1 deletion src/Elastic.Apm/Report/PayloadSenderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/Elastic.Apm.Tests.MockApmServer/MetadataDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, string> Labels { get; set; }
Expand All @@ -25,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 },
Expand Down

0 comments on commit 5e74fdd

Please sign in to comment.