-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
DiagnosticsOptions.cs
157 lines (140 loc) · 6.61 KB
/
DiagnosticsOptions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Azure.Core
{
/// <summary>
/// Exposes client options related to logging, telemetry, and distributed tracing.
/// </summary>
public class DiagnosticsOptions
{
private const int MaxApplicationIdLength = 24;
private string? _applicationId;
/// <summary>
/// Creates a new instance of <see cref="DiagnosticsOptions"/> with default values.
/// </summary>
protected internal DiagnosticsOptions() : this(ClientOptions.Default.Diagnostics)
{ }
/// <summary>
/// Initializes the newly created <see cref="DiagnosticsOptions"/> with the same settings as the specified <paramref name="diagnosticsOptions"/>.
/// </summary>
/// <param name="diagnosticsOptions">The <see cref="DiagnosticsOptions"/> to model the newly created instance on.</param>
internal DiagnosticsOptions(DiagnosticsOptions? diagnosticsOptions)
{
if (diagnosticsOptions != null)
{
ApplicationId = diagnosticsOptions.ApplicationId;
IsLoggingEnabled = diagnosticsOptions.IsLoggingEnabled;
IsTelemetryEnabled = diagnosticsOptions.IsTelemetryEnabled;
LoggedHeaderNames = new List<string>(diagnosticsOptions.LoggedHeaderNames);
LoggedQueryParameters = new List<string>(diagnosticsOptions.LoggedQueryParameters);
LoggedContentSizeLimit = diagnosticsOptions.LoggedContentSizeLimit;
IsDistributedTracingEnabled = diagnosticsOptions.IsDistributedTracingEnabled;
IsLoggingContentEnabled = diagnosticsOptions.IsLoggingContentEnabled;
}
else
{
LoggedHeaderNames = new List<string>()
{
"x-ms-request-id",
"x-ms-client-request-id",
"x-ms-return-client-request-id",
"traceparent",
"MS-CV",
"Accept",
"Cache-Control",
"Connection",
"Content-Length",
"Content-Type",
"Date",
"ETag",
"Expires",
"If-Match",
"If-Modified-Since",
"If-None-Match",
"If-Unmodified-Since",
"Last-Modified",
"Pragma",
"Request-Id",
"Retry-After",
"Server",
"Transfer-Encoding",
"User-Agent",
"WWW-Authenticate" // OAuth Challenge header.
};
LoggedQueryParameters = new List<string> { "api-version" };
IsTelemetryEnabled = !EnvironmentVariableToBool(Environment.GetEnvironmentVariable("AZURE_TELEMETRY_DISABLED")) ?? true;
IsDistributedTracingEnabled = !EnvironmentVariableToBool(Environment.GetEnvironmentVariable("AZURE_TRACING_DISABLED")) ?? true;
}
}
/// <summary>
/// Get or sets value indicating whether HTTP pipeline logging is enabled.
/// </summary>
public bool IsLoggingEnabled { get; set; } = true;
/// <summary>
/// Gets or sets value indicating whether distributed tracing activities (<see cref="System.Diagnostics.Activity"/>) are going to be created for the clients methods calls and HTTP calls.
/// </summary>
public bool IsDistributedTracingEnabled { get; set; } = true;
/// <summary>
/// Gets or sets value indicating whether the "User-Agent" header containing <see cref="ApplicationId"/>, client library package name and version, <see cref="RuntimeInformation.FrameworkDescription"/>
/// and <see cref="RuntimeInformation.OSDescription"/> should be sent.
/// The default value can be controlled process wide by setting <c>AZURE_TELEMETRY_DISABLED</c> to <c>true</c>, <c>false</c>, <c>1</c> or <c>0</c>.
/// </summary>
public bool IsTelemetryEnabled { get; set; }
/// <summary>
/// Gets or sets value indicating if request or response content should be logged.
/// </summary>
public bool IsLoggingContentEnabled { get; set; }
/// <summary>
/// Gets or sets value indicating maximum size of content to log in bytes. Defaults to 4096.
/// </summary>
public int LoggedContentSizeLimit { get; set; } = 4 * 1024;
/// <summary>
/// Gets a list of header names that are not redacted during logging.
/// </summary>
public IList<string> LoggedHeaderNames { get; internal set; }
/// <summary>
/// Gets a list of query parameter names that are not redacted during logging.
/// </summary>
public IList<string> LoggedQueryParameters { get; internal set; }
/// <summary>
/// Gets or sets the value sent as the first part of "User-Agent" headers for all requests issues by this client. Defaults to <see cref="DefaultApplicationId"/>.
/// </summary>
public string? ApplicationId
{
get => _applicationId;
set
{
if (value != null && value.Length > MaxApplicationIdLength)
{
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(ApplicationId)} must be shorter than {MaxApplicationIdLength + 1} characters");
}
_applicationId = value;
}
}
/// <summary>
/// Gets or sets the default application id. Default application id would be set on all instances.
/// </summary>
public static string? DefaultApplicationId
{
get => ClientOptions.Default.Diagnostics.ApplicationId;
set => ClientOptions.Default.Diagnostics.ApplicationId = value;
}
private static bool? EnvironmentVariableToBool(string? value)
{
if (string.Equals(bool.TrueString, value, StringComparison.OrdinalIgnoreCase) ||
string.Equals("1", value, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (string.Equals(bool.FalseString, value, StringComparison.OrdinalIgnoreCase) ||
string.Equals("0", value, StringComparison.OrdinalIgnoreCase))
{
return false;
}
return null;
}
}
}