-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathOpenTelemetryConfigurationExtensions.cs
168 lines (151 loc) · 7.59 KB
/
OpenTelemetryConfigurationExtensions.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
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using Azure.Core;
using Azure.Identity;
using Azure.Monitor.OpenTelemetry.Exporter;
using Azure.Monitor.OpenTelemetry.LiveMetrics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using AppInsightsCredentialOptions = Microsoft.Azure.WebJobs.Logging.ApplicationInsights.TokenCredentialOptions;
namespace Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry
{
internal static class OpenTelemetryConfigurationExtensions
{
internal static void ConfigureOpenTelemetry(this ILoggingBuilder loggingBuilder, HostBuilderContext context)
{
string azMonConnectionString = GetConfigurationValue(EnvironmentSettingNames.AppInsightsConnectionString, context.Configuration);
TokenCredential credential = GetTokenCredential(context.Configuration);
bool enableOtlp = false;
if (!string.IsNullOrEmpty(GetConfigurationValue(EnvironmentSettingNames.OtlpEndpoint, context.Configuration)))
{
enableOtlp = true;
}
loggingBuilder
.AddOpenTelemetry(o =>
{
o.SetResourceBuilder(ConfigureResource(ResourceBuilder.CreateDefault()));
if (enableOtlp)
{
o.AddOtlpExporter();
}
if (!string.IsNullOrEmpty(azMonConnectionString))
{
o.AddAzureMonitorLogExporter(options => ConfigureAzureMonitorOptions(options, azMonConnectionString, credential));
}
o.IncludeFormattedMessage = true;
o.IncludeScopes = false;
})
// These are messages piped back to the host from the worker - we don't handle these anymore if the worker has OpenTelemetry enabled.
// Instead, we expect the user's own code to be logging these where they want them to go.
.AddFilter<OpenTelemetryLoggerProvider>("Function.*", _ => !ScriptHost.WorkerOpenTelemetryEnabled)
.AddFilter<OpenTelemetryLoggerProvider>("Azure.*", _ => !ScriptHost.WorkerOpenTelemetryEnabled)
// Host.Results and Host.Aggregator are used to emit metrics, ignoring these categories.
.AddFilter<OpenTelemetryLoggerProvider>("Host.Results", _ => !ScriptHost.WorkerOpenTelemetryEnabled)
.AddFilter<OpenTelemetryLoggerProvider>("Host.Aggregator", _ => !ScriptHost.WorkerOpenTelemetryEnabled)
// Ignoring all Microsoft.Azure.WebJobs.* logs like /getScriptTag and /lock.
.AddFilter<OpenTelemetryLoggerProvider>("Microsoft.Azure.WebJobs.*", _ => !ScriptHost.WorkerOpenTelemetryEnabled);
// Azure SDK instrumentation is experimental.
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
loggingBuilder.Services.AddOpenTelemetry()
.ConfigureResource(r => ConfigureResource(r))
.WithTracing(b =>
{
b.AddSource("Azure.*");
b.AddAspNetCoreInstrumentation();
b.AddHttpClientInstrumentation(o =>
{
o.FilterHttpRequestMessage = _ =>
{
Activity activity = Activity.Current?.Parent;
return activity == null || !activity.Source.Name.Equals("Azure.Core.Http");
};
});
if (enableOtlp)
{
b.AddOtlpExporter();
}
if (!string.IsNullOrEmpty(azMonConnectionString))
{
b.AddAzureMonitorTraceExporter(options => ConfigureAzureMonitorOptions(options, azMonConnectionString, credential));
b.AddLiveMetrics(options => ConfigureAzureMonitorOptions(options, azMonConnectionString, credential));
}
b.AddProcessor(ActivitySanitizingProcessor.Instance);
b.AddProcessor(TraceFilterProcessor.Instance);
});
string eventLogLevel = GetConfigurationValue(EnvironmentSettingNames.OpenTelemetryEventListenerLogLevel, context.Configuration);
if (!string.IsNullOrEmpty(eventLogLevel))
{
if (Enum.TryParse(eventLogLevel, ignoreCase: true, out EventLevel level))
{
loggingBuilder.Services.AddHostedService(service => new OpenTelemetryEventListenerService(level));
}
else
{
throw new InvalidEnumArgumentException($"Invalid '{EnvironmentSettingNames.OpenTelemetryEventListenerLogLevel}' of '{eventLogLevel}'.");
}
}
else
{
// Log all warnings and above by default.
loggingBuilder.Services.AddHostedService(service => new OpenTelemetryEventListenerService(EventLevel.Warning));
}
static ResourceBuilder ConfigureResource(ResourceBuilder r)
{
r.AddDetector(new FunctionsResourceDetector());
// Set the AI SDK to a key so we know all the telemetry came from the Functions Host
// NOTE: This ties to \azure-sdk-for-net\sdk\monitor\Azure.Monitor.OpenTelemetry.Exporter\src\Internals\ResourceExtensions.cs :: AiSdkPrefixKey used in CreateAzureMonitorResource()
return r;
}
}
private static string GetConfigurationValue(string key, IConfiguration configuration = null)
{
if (configuration != null && configuration[key] is string configValue)
{
return configValue;
}
else if (Environment.GetEnvironmentVariable(key) is string envValue)
{
return envValue;
}
else
{
return null;
}
}
private static TokenCredential GetTokenCredential(IConfiguration configuration)
{
if (GetConfigurationValue(EnvironmentSettingNames.AppInsightsAuthenticationString, configuration) is string authString)
{
AppInsightsCredentialOptions credOptions = AppInsightsCredentialOptions.ParseAuthenticationString(authString);
return new ManagedIdentityCredential(credOptions.ClientId);
}
return null;
}
private static void ConfigureAzureMonitorOptions(AzureMonitorExporterOptions options, string connectionString, TokenCredential credential)
{
options.ConnectionString = connectionString;
if (credential is not null)
{
options.Credential = credential;
}
}
private static void ConfigureAzureMonitorOptions(LiveMetricsExporterOptions options, string connectionString, TokenCredential credential)
{
options.ConnectionString = connectionString;
if (credential is not null)
{
options.Credential = credential;
}
}
}
}