Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading