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

Handle possible exception when initializing the default service name #3405

Merged
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
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
`set` methods
([#3378](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3378))

* Handle possible exception when initializing the default service name.
([#3405](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3405))

## 1.3.0

Released 2022-Jun-03
Expand Down
32 changes: 25 additions & 7 deletions src/OpenTelemetry/Resources/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Resources
Expand All @@ -26,17 +27,34 @@ public class ResourceBuilder
{
private readonly List<Resource> resources = new();

private ResourceBuilder()
static ResourceBuilder()
{
var defaultServiceName = "unknown_service";

try
{
var processName = Process.GetCurrentProcess().ProcessName;
if (!string.IsNullOrWhiteSpace(processName))
{
defaultServiceName = $"{defaultServiceName}:{processName}";
}
}
catch
{
// GetCurrentProcess can throw PlatformNotSupportedException
}

DefaultResource = new Resource(new Dictionary<string, object>
{
[ResourceSemanticConventions.AttributeServiceName] = defaultServiceName,
});
}

private static Resource DefaultResource { get; } = new Resource(new Dictionary<string, object>
private ResourceBuilder()
{
[ResourceSemanticConventions.AttributeServiceName] = "unknown_service"
+ (string.IsNullOrWhiteSpace(System.Diagnostics.Process.GetCurrentProcess().ProcessName)
? string.Empty :
":" + System.Diagnostics.Process.GetCurrentProcess().ProcessName),
});
}

private static Resource DefaultResource { get; }

/// <summary>
/// Creates a <see cref="ResourceBuilder"/> instance with Default
Expand Down