| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Runtime.InteropServices; |
| | 7 | |
|
| | 8 | | namespace Azure.Core |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Exposes client options related to logging, telemetry and distributed tracing. |
| | 12 | | /// </summary> |
| | 13 | | public class DiagnosticsOptions |
| | 14 | | { |
| | 15 | | private const int MaxApplicationIdLength = 24; |
| | 16 | |
|
| | 17 | | private string? _applicationId; |
| | 18 | |
|
| 96 | 19 | | internal DiagnosticsOptions() |
| | 20 | | { |
| 96 | 21 | | IsTelemetryEnabled = !EnvironmentVariableToBool(Environment.GetEnvironmentVariable("AZURE_TELEMETRY_DISABLED |
| 96 | 22 | | IsDistributedTracingEnabled = !EnvironmentVariableToBool(Environment.GetEnvironmentVariable("AZURE_TRACING_D |
| 96 | 23 | | ApplicationId = DefaultApplicationId; |
| 96 | 24 | | LoggedHeaderNames = new List<string>() |
| 96 | 25 | | { |
| 96 | 26 | | "x-ms-client-request-id", |
| 96 | 27 | | "x-ms-return-client-request-id", |
| 96 | 28 | | "traceparent", |
| 96 | 29 | |
|
| 96 | 30 | | "Accept", |
| 96 | 31 | | "Cache-Control", |
| 96 | 32 | | "Connection", |
| 96 | 33 | | "Content-Length", |
| 96 | 34 | | "Content-Type", |
| 96 | 35 | | "Date", |
| 96 | 36 | | "ETag", |
| 96 | 37 | | "Expires", |
| 96 | 38 | | "If-Match", |
| 96 | 39 | | "If-Modified-Since", |
| 96 | 40 | | "If-None-Match", |
| 96 | 41 | | "If-Unmodified-Since", |
| 96 | 42 | | "Last-Modified", |
| 96 | 43 | | "Pragma", |
| 96 | 44 | | "Request-Id", |
| 96 | 45 | | "Retry-After", |
| 96 | 46 | | "Server", |
| 96 | 47 | | "Transfer-Encoding", |
| 96 | 48 | | "User-Agent" |
| 96 | 49 | | }; |
| 96 | 50 | | LoggedQueryParameters = new List<string>(); |
| 96 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Get or sets value indicating whether HTTP pipeline logging is enabled. |
| | 55 | | /// </summary> |
| 154 | 56 | | public bool IsLoggingEnabled { get; set; } = true; |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Gets or sets value indicating whether distributed tracing spans are going to be created for this clients met |
| | 60 | | /// </summary> |
| 260 | 61 | | public bool IsDistributedTracingEnabled { get; set; } = true; |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Gets or sets value indicating whether the "User-Agent" header containing <see cref="ApplicationId"/>, client |
| | 65 | | /// and <see cref="RuntimeInformation.OSDescription"/> should be sent. |
| | 66 | | /// The default value can be controlled process wide by setting <c>AZURE_TELEMETRY_DISABLED</c> to <c>true</c>, |
| | 67 | | /// </summary> |
| 158 | 68 | | public bool IsTelemetryEnabled { get; set; } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Gets or sets value indicating if request or response content should be logged. |
| | 72 | | /// </summary> |
| 60 | 73 | | public bool IsLoggingContentEnabled { get; set; } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Gets or sets value indicating maximum size of content to log in bytes. Defaults to 4096. |
| | 77 | | /// </summary> |
| 0 | 78 | | public int LoggedContentSizeLimit { get; set; } = 4 * 1024; |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Gets a list of headers names that are not redacted during logging. |
| | 82 | | /// </summary> |
| 92 | 83 | | public IList<string> LoggedHeaderNames { get; } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Gets a list of query parameter names that are not redacted during logging. |
| | 87 | | /// </summary> |
| 82 | 88 | | public IList<string> LoggedQueryParameters { get; } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Gets or sets the value sent a the first part of "User-Agent" headers for all requests issues by this client. |
| | 92 | | /// </summary> |
| | 93 | | public string? ApplicationId |
| | 94 | | { |
| 60 | 95 | | get => _applicationId; |
| | 96 | | set |
| | 97 | | { |
| 100 | 98 | | if (value != null && value.Length > MaxApplicationIdLength) |
| | 99 | | { |
| 2 | 100 | | throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(ApplicationId)} must be shorter than |
| | 101 | | } |
| 98 | 102 | | _applicationId = value; |
| 98 | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Gets or sets the default application id. Default application id would be set on all instances. |
| | 108 | | /// </summary> |
| 100 | 109 | | public static string? DefaultApplicationId { get; set; } |
| | 110 | |
|
| | 111 | | private static bool? EnvironmentVariableToBool(string value) |
| | 112 | | { |
| 192 | 113 | | if (string.Equals("true", value, StringComparison.OrdinalIgnoreCase) || |
| 192 | 114 | | string.Equals("1", value, StringComparison.OrdinalIgnoreCase)) |
| | 115 | | { |
| 12 | 116 | | return true; |
| | 117 | | } |
| | 118 | |
|
| 180 | 119 | | if (string.Equals("false", value, StringComparison.OrdinalIgnoreCase) || |
| 180 | 120 | | string.Equals("0", value, StringComparison.OrdinalIgnoreCase)) |
| | 121 | | { |
| 0 | 122 | | return false; |
| | 123 | | } |
| | 124 | |
|
| 180 | 125 | | return null; |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |