-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ResourceExtensions.cs
156 lines (138 loc) · 5.86 KB
/
ResourceExtensions.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
using Azure.Monitor.OpenTelemetry.Exporter.Models;
using OpenTelemetry.Resources;
namespace Azure.Monitor.OpenTelemetry.Exporter.Internals;
internal static class ResourceExtensions
{
private const string AiSdkPrefixKey = "ai.sdk.prefix";
private const string TelemetryDistroNameKey = "telemetry.distro.name";
private const string DefaultServiceName = "unknown_service";
private const int Version = 2;
internal static AzureMonitorResource? CreateAzureMonitorResource(this Resource resource, string? instrumentationKey = null)
{
if (resource == null)
{
return null;
}
AzureMonitorResource azureMonitorResource = new AzureMonitorResource();
MetricsData? metricsData = null;
AksResourceProcessor? aksResourceProcessor = null;
string? serviceName = null;
string? serviceNamespace = null;
string? serviceInstance = null;
string? serviceVersion = null;
bool? hasDefaultServiceName = null;
if (instrumentationKey != null && resource.Attributes.Any())
{
metricsData = new MetricsData(Version);
}
foreach (var attribute in resource.Attributes)
{
switch (attribute.Key)
{
case SemanticConventions.AttributeServiceName when attribute.Value is string _serviceName:
serviceName = _serviceName;
if (serviceName.StartsWith(DefaultServiceName))
{
hasDefaultServiceName = true;
break;
}
hasDefaultServiceName = false;
break;
case SemanticConventions.AttributeServiceNamespace when attribute.Value is string _serviceNamespace:
serviceNamespace = $"[{_serviceNamespace}]";
break;
case SemanticConventions.AttributeServiceInstance when attribute.Value is string _serviceInstance:
serviceInstance = _serviceInstance;
break;
case AiSdkPrefixKey when attribute.Value is string _aiSdkPrefixValue:
SdkVersionUtils.SdkVersionPrefix = _aiSdkPrefixValue;
continue;
case SemanticConventions.AttributeServiceVersion when attribute.Value is string _serviceVersion:
serviceVersion = _serviceVersion;
break;
case TelemetryDistroNameKey when attribute.Value is string _aiSdkDistroValue:
if (_aiSdkDistroValue == "Azure.Monitor.OpenTelemetry.AspNetCore")
{
SdkVersionUtils.IsDistro = true;
}
break;
default:
if (attribute.Key.StartsWith("k8s"))
{
aksResourceProcessor = aksResourceProcessor ?? new AksResourceProcessor();
aksResourceProcessor.MapAttributeToProperty(attribute);
}
break;
}
if (metricsData != null && attribute.Key.Length <= SchemaConstants.MetricsData_Properties_MaxKeyLength && attribute.Value != null)
{
// Note: if Key exceeds MaxLength or if Value is null, the entire KVP will be dropped.
metricsData.Properties.Add(new KeyValuePair<string, string>(attribute.Key, attribute.Value.ToString().Truncate(SchemaConstants.MetricsData_Properties_MaxValueLength) ?? "null"));
}
}
// TODO: Check if service.name as unknown_service should be sent.
// (2023-07) we need to drop the "unknown_service."
if (serviceName != null && serviceNamespace != null)
{
azureMonitorResource.RoleName = string.Concat(serviceNamespace, "/", serviceName);
}
else
{
azureMonitorResource.RoleName = serviceName;
}
try
{
azureMonitorResource.RoleInstance = serviceInstance ?? Dns.GetHostName();
}
catch (Exception ex)
{
AzureMonitorExporterEventSource.Log.ErrorInitializingRoleInstanceToHostName(ex);
}
if (serviceVersion != null)
{
azureMonitorResource.ServiceVersion = serviceVersion;
}
if (aksResourceProcessor != null)
{
var aksRoleName = aksResourceProcessor.GetRoleName();
var aksRoleInstanceName = aksResourceProcessor.GetRoleInstance();
if (hasDefaultServiceName != false && aksRoleName != null)
{
azureMonitorResource.RoleName = aksRoleName;
}
if (serviceInstance == null && aksRoleInstanceName != null)
{
azureMonitorResource.RoleInstance = aksRoleInstanceName;
}
}
bool shouldReportMetricTelemetry = false;
try
{
var exportResource = Environment.GetEnvironmentVariable(EnvironmentVariableConstants.EXPORT_RESOURCE_METRIC);
if (exportResource != null && exportResource.Equals("true", StringComparison.OrdinalIgnoreCase))
{
shouldReportMetricTelemetry = true;
}
}
catch
{
}
if (shouldReportMetricTelemetry && metricsData != null)
{
azureMonitorResource.MonitorBaseData = new MonitorBase
{
BaseType = "MetricData",
BaseData = metricsData
};
}
return azureMonitorResource;
}
}