forked from newrelic/newrelic-telemetry-sdk-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelemetryConfiguration.cs
175 lines (152 loc) · 7.96 KB
/
TelemetryConfiguration.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
169
170
171
172
173
174
175
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
using System;
#if !INTERNALIZE_TELEMETRY_SDK
using Microsoft.Extensions.Configuration;
#endif
namespace NewRelic.Telemetry
{
/// <summary>
/// Configuration settings for the New Relic Telemetry SDK.
/// </summary>
#if INTERNALIZE_TELEMETRY_SDK
internal
#else
public
#endif
class TelemetryConfiguration
{
/// <summary>
/// REQUIRED: Your Insights Insert API Key. This value is required in order to communicate with the
/// New Relic Endpoint.
/// </summary>
/// <see cref="https://docs.newrelic.com/docs/insights/insights-data-sources/custom-data/introduction-event-api#register">for more information.</see>
public string ApiKey { get; set; } = string.Empty;
/// <summary>
/// The New Relic endpoint where Trace/Span information is sent.
/// </summary>
public Uri TraceUrl { get; set; } = new Uri("https://trace-api.newrelic.com/trace/v1");
/// <summary>
/// The New Relic endpoint where Metric information is sent.
/// </summary>
public Uri MetricUrl { get; set; } = new Uri("https://metric-api.newrelic.com/metric/v1");
/// <summary>
/// Logs messages sent-to and received-by the New Relic endpoints. This setting
/// is useful for troubleshooting, but is not recommended in production environments.
/// </summary>
public bool AuditLoggingEnabled { get; set; } = false;
/// <summary>
/// The number of seconds that the DataSender will wait for a response from
/// a New Relic endpoint. Requests that exceed this limit will be assumed to have
/// failed.
/// </summary>
public TimeSpan SendTimeout { get; set; } = TimeSpan.FromSeconds(5);
/// <summary>
/// In the event of a failure, the DataSender will wait a certain amount of time (back-off) and retry.
/// This setting indicates how many times the DataSender will re-attempt to send information
/// prior to failing.
/// </summary>
public int MaxRetryAttempts { get; set; } = 8;
/// <summary>
/// Between each retry, the DataSender waits a variable amount of time. This is a back-off period.
/// This setting indicates the maximum wait time for a back-off.
/// </summary>
public int BackoffMaxSeconds { get; set; } = 80;
/// <summary>
/// Each time the DataSender retries, it backs-off and waits for a longer period of time.
/// The amount of time grows exponentially with each attempt until it exceeds the <see cref="BackoffMaxSeconds"/>.
/// This setting identifies the factor by which the backoff is exponentially increased.
/// </summary>
/// <example>
/// With a BackOffDelayFactorSeconds of 5 and BackoffMaxSeconds of 80.
/// Backoffs would be 5s (5^1), 25s (5^2), 80s (2^3=125 -> 80), 80s (2^4 = 625 -> 80), etc.
/// </example>
public int BackoffDelayFactorSeconds { get; set; } = 5;
/// <summary>
/// Identifies the name of a service for which information is being reported to New Relic.
/// </summary>
public string ServiceName { get; set; } = "New Relic Telemetry SDK";
/// <summary>
/// Initializes a new instance of the <see cref="TelemetryConfiguration"/> class.
/// Creates the Configuration object accepting all default settings.
/// </summary>
public TelemetryConfiguration()
{
}
#if !INTERNALIZE_TELEMETRY_SDK
/// <summary>
/// Initializes a new instance of the <see cref="TelemetryConfiguration"/> class.
/// Constructs a new configuration object using a configuration provider. Allows for an overall
/// New Relic Value
/// by <see cref="Microsoft.Extensions.Configuration">Microsoft.Extensions.Configuration</see>.
/// </summary>
/// <param name="configProvider"></param>
public TelemetryConfiguration(IConfiguration configProvider)
: this(configProvider, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TelemetryConfiguration"/> class.
/// Constructs a new configuration object using a configuration provider. Allows for an overall
/// New Relic Value and can be overriden with a Product Specific Value.
/// by <see cref="Microsoft.Extensions.Configuration">Microsoft.Extensions.Configuration</see>.
/// </summary>
/// <param name="configProvider"></param>
public TelemetryConfiguration(IConfiguration configProvider, string? productSpecificConfig)
{
var newRelicConfigSection = configProvider
.GetSection("NewRelic");
if (newRelicConfigSection == null)
{
return;
}
IConfigurationSection? productConfigSection = null;
if (!string.IsNullOrWhiteSpace(productSpecificConfig))
{
productConfigSection = newRelicConfigSection.GetSection(productSpecificConfig);
}
ApiKey = GetValueString("ApiKey", productConfigSection, newRelicConfigSection) ?? ApiKey;
ServiceName = GetValueString("ServiceName", productConfigSection, newRelicConfigSection) ?? ServiceName;
TraceUrl = GetValueUri("TraceUrlOverride", productConfigSection, newRelicConfigSection) ?? TraceUrl;
MetricUrl = GetValueUri("MetricUrlOverride", productConfigSection, newRelicConfigSection) ?? MetricUrl;
AuditLoggingEnabled = GetValueBool("AuditLoggingEnabled", productConfigSection, newRelicConfigSection) ?? AuditLoggingEnabled;
SendTimeout = GetValueTimeSpan("SendTimeoutSeconds", productConfigSection, newRelicConfigSection) ?? SendTimeout;
MaxRetryAttempts = GetValueInt("MaxRetryAttempts", productConfigSection, newRelicConfigSection) ?? MaxRetryAttempts;
BackoffMaxSeconds = GetValueInt("BackoffMaxSeconds", productConfigSection, newRelicConfigSection) ?? BackoffMaxSeconds;
BackoffDelayFactorSeconds = GetValueInt("BackoffDelayFactorSeconds", productConfigSection, newRelicConfigSection) ?? BackoffDelayFactorSeconds;
}
private Uri? GetValueUri(string key, IConfigurationSection? productConfigSection, IConfigurationSection newRelicConfigSection)
{
Uri.TryCreate(GetValueString(key, productConfigSection, newRelicConfigSection), UriKind.Absolute, out var uri);
return uri;
}
private TimeSpan? GetValueTimeSpan(string key, IConfigurationSection? productConfigSection, IConfigurationSection newRelicConfigSection)
{
var seconds = GetValueInt(key, productConfigSection, newRelicConfigSection);
return seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;
}
private string? GetValueString(string key, IConfigurationSection? productConfigSection, IConfigurationSection newRelicConfigSection)
{
return productConfigSection?[key] ?? newRelicConfigSection[key];
}
private bool? GetValueBool(string key, IConfigurationSection? productConfigSection, IConfigurationSection newRelicConfigSection)
{
string valStr = productConfigSection?[key] ?? newRelicConfigSection[key];
if (!string.IsNullOrEmpty(valStr) && bool.TryParse(valStr, out var valBool))
{
return valBool;
}
return null;
}
private int? GetValueInt(string key, IConfigurationSection? productConfigSection, IConfigurationSection newRelicConfigSection)
{
string valStr = productConfigSection?[key] ?? newRelicConfigSection[key];
if (!string.IsNullOrEmpty(valStr) && int.TryParse(valStr, out var valInt))
{
return valInt;
}
return null;
}
#endif
}
}