-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathRuntimeId.cs
More file actions
51 lines (42 loc) · 1.93 KB
/
RuntimeId.cs
File metadata and controls
51 lines (42 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// <copyright file="RuntimeId.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.Threading;
using Datadog.Trace.Configuration;
using Datadog.Trace.Logging;
namespace Datadog.Trace.Util
{
internal static class RuntimeId
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(RuntimeId));
private static string _runtimeId;
private static string _rootSessionId;
public static string Get() => LazyInitializer.EnsureInitialized(ref _runtimeId, () => GetRuntimeIdImpl());
public static string GetRootSessionId() => LazyInitializer.EnsureInitialized(ref _rootSessionId, () => GetRootSessionIdImpl());
private static string GetRuntimeIdImpl()
{
if (NativeLoader.TryGetRuntimeIdFromNative(out var runtimeId))
{
Log.Information("Runtime id retrieved from native loader: {RuntimeId}", runtimeId);
return runtimeId;
}
var guid = Guid.NewGuid().ToString();
Log.Debug("Unable to get the runtime id from native. Fallback to Guid.NewGuid() : {NewGuid}", guid);
return guid;
}
private static string GetRootSessionIdImpl()
{
var inherited = EnvironmentHelpers.GetEnvironmentVariable(ConfigurationKeys.Telemetry.RootSessionId);
if (!string.IsNullOrEmpty(inherited))
{
Log.Debug("Inherited root session ID from parent: {RootSessionId}", inherited);
return inherited;
}
var rootId = Get();
EnvironmentHelpers.SetEnvironmentVariable(ConfigurationKeys.Telemetry.RootSessionId, rootId);
return rootId;
}
}
}