Skip to content

Commit

Permalink
Handle possible exception when initializing the default service name (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored Jun 24, 2022
1 parent 1f4d97e commit b7a3a83
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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

0 comments on commit b7a3a83

Please sign in to comment.