generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 8
/
HoneycombOptions.cs
252 lines (216 loc) · 9.96 KB
/
HoneycombOptions.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Instrumentation.Http;
using OpenTelemetry.Instrumentation.SqlClient;
using OpenTelemetry.Instrumentation.StackExchangeRedis;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
namespace Honeycomb.OpenTelemetry
{
/// <summary>
/// Configuration options used to configure <see cref="TracerProviderBuilder"/> to send telemetry data to Honeycomb.
/// </summary>
public class HoneycombOptions
{
/// <summary>
/// Default service name if service name is not provided.
/// </summary>
internal static readonly string SDefaultServiceName = $"unknown_service:{System.Diagnostics.Process.GetCurrentProcess().ProcessName}";
private static readonly string SDefaultServiceVersion = "{unknown_service_version}";
private string _tracesApiKey;
private string _metricsApiKey;
private string _tracesDataset;
private string _tracesEndpoint;
private string _metricsEndpoint;
/// <summary>
/// Name of the Honeycomb section of IConfiguration
/// </summary>
public const string ConfigSectionName = "Honeycomb";
/// <summary>
/// Default API endpoint.
/// </summary>
public const string DefaultEndpoint = "https://api.honeycomb.io:443";
/// <summary>
/// Default sample rate - sample everything.
/// </summary>
public const uint DefaultSampleRate = 1;
/// <summary>
/// API key used to send telemetry data to Honeycomb.
/// <para/>
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// Returns whether API key used to send trace telemetry is a legacy key.
/// </summary>
internal bool IsTracesLegacyKey()
{
// legacy key has 32 characters
return TracesApiKey?.Length == 32;
}
/// <summary>
/// Returns whether API key used to send metrics telemetry is a legacy key.
/// </summary>
internal bool IsMetricsLegacyKey()
{
// legacy key has 32 characters
return MetricsApiKey?.Length == 32;
}
/// <summary>
/// API key used to send trace telemetry data to Honeycomb. Defaults to <see cref="ApiKey"/>.
/// </summary>
public string TracesApiKey
{
get { return _tracesApiKey ?? ApiKey; }
set { _tracesApiKey = value; }
}
/// <summary>
/// API key used to send metrics telemetry data to Honeycomb. Defaults to <see cref="ApiKey"/>.
/// </summary>
public string MetricsApiKey
{
get { return _metricsApiKey ?? ApiKey; }
set { _metricsApiKey = value; }
}
/// <summary>
/// Honeycomb dataset to store telemetry data.
/// <para/>
/// </summary>
public string Dataset { get; set; }
/// <summary>
/// Honeycomb dataset to store trace telemetry data. Defaults to <see cref="Dataset"/>.
/// </summary>
public string TracesDataset
{
get { return _tracesDataset ?? Dataset; }
set { _tracesDataset = value; }
}
/// <summary>
/// Honeycomb dataset to store metrics telemetry data. Defaults to "null".
/// <para/>
/// Required to enable metrics.
/// </summary>
public string MetricsDataset { get; set; }
/// <summary>
/// API endpoint to send telemetry data. Defaults to <see cref="DefaultEndpoint"/>.
/// </summary>
public string Endpoint { get; set; } = DefaultEndpoint;
/// <summary>
/// API endpoint to send telemetry data. Defaults to <see cref="Endpoint"/>.
/// </summary>
public string TracesEndpoint
{
get { return _tracesEndpoint ?? Endpoint; }
set { _tracesEndpoint = value; }
}
/// <summary>
/// API endpoint to send telemetry data. Defaults to <see cref="Endpoint"/>.
/// </summary>
public string MetricsEndpoint
{
get { return _metricsEndpoint ?? Endpoint; }
set { _metricsEndpoint = value; }
}
/// <summary>
/// Sample rate for sending telemetry data. Defaults to <see cref="DefaultSampleRate"/>.
/// <para/>
/// See <see cref="DeterministicSampler"/> for more details on how sampling is applied.
/// </summary>
public uint SampleRate { get; set; } = DefaultSampleRate;
/// <summary>
/// Service name used to identify application. Defaults to unknown_process:processname.
/// </summary>
public string ServiceName { get; set; }
/// <summary>
/// Service version. Defaults to application assembly information version.
/// </summary>
public string ServiceVersion { get; set; } = SDefaultServiceVersion;
/// <summary>
/// Redis <see cref="IConnectionMultiplexer"/>. Set this if you aren't using a DI Container.
/// If you're using a DI Container, then setting this isn't necessary as it will be resolved from the <see cref="IServiceProvider"/>.
/// </summary>
public IConnectionMultiplexer RedisConnection { get; set; }
/// <summary>
/// Controls whether to instrument HttpClient calls.
/// </summary>
public bool InstrumentHttpClient { get; set; } = true;
/// <summary>
/// Controls whether to instrument SqlClient calls.
/// </summary>
public bool InstrumentSqlClient { get; set; } = true;
/// <summary>
/// Controls whether to instrument GrpcClient calls when running on .NET Standard 2.1 or greater.
/// Requires <see cref="InstrumentHttpClient" /> to be <see langword="true"/> due to the underlying implementation.
/// </summary>
public bool InstrumentGrpcClient { get; set; } = true;
/// <summary>
/// Controls whether the Stack Exchange Redis Client is instrumented.
/// Requires that either <see cref="RedisConnection"/> is set, if you're not using a DI Container, or
/// if you are using a DI Container, then it requires that an <see cref="IConnectionMultiplexer"/> has been registered with the <see cref="IServiceProvider"/>.
/// </summary>
public bool InstrumentStackExchangeRedisClient { get; set; } = true;
/// <summary>
/// (Optional) Options delegate to configure HttpClient instrumentation.
/// </summary>
public Action<HttpClientInstrumentationOptions> ConfigureHttpClientInstrumentationOptions { get; set; }
/// <summary>
/// (Optional) Options delegate to configure SqlClient instrumentation.
/// </summary>
public Action<SqlClientInstrumentationOptions> ConfigureSqlClientInstrumentationOptions { get; set; }
/// <summary>
/// (Optional) Options delegate to configure StackExchange.Redis instrumentation.
/// </summary>
public Action<StackExchangeRedisCallsInstrumentationOptions>
ConfigureStackExchangeRedisClientInstrumentationOptions { get; set; }
/// <summary>
/// (Optional) Additional <see cref="Meter"/> names for generating metrics.
/// <see cref="ServiceName"/> is configured as a meter name by default.
/// </summary>
public List<string> MeterNames { get; set; } = new List<string>();
/// <summary>
/// The <see cref="ResourceBuilder" /> to use to add Resource attributes to.
/// A custom ResouceBuilder can be used to set additional resources and then passed here to add
/// Honeycomb attributes.
/// </summary>
public ResourceBuilder ResourceBuilder { get; set; } = ResourceBuilder.CreateDefault();
private static readonly Dictionary<string, string> CommandLineSwitchMap = new Dictionary<string, string>
{
{ "--honeycomb-apikey", "apikey" },
{ "--honeycomb-traces-apikey", "tracesapikey" },
{ "--honeycomb-metrics-apikey", "metricsapikey" },
{ "--honeycomb-dataset", "dataset" },
{ "--honeycomb-traces-dataset", "tracesdataset" },
{ "--honeycomb-metrics-dataset", "metricsdataset" },
{ "--honeycomb-endpoint", "endpoint" },
{ "--honeycomb-traces-endpoint", "tracesendpoint" },
{ "--honeycomb-metrics-endpoint", "metricsendpoint" },
{ "--honeycomb-samplerate", "samplerate" },
{ "--service-name", "servicename" },
{ "--service-version", "serviceversion" },
{ "--instrument-http", "instrumenthttpclient" },
{ "--instrument-sql", "instrumentsqlclient" },
{ "--instrument-grpc", "instrumentgrpcclient" },
{ "--instrument-redis", "instrumentstackexchangeredisclient" },
{ "--meter-names", "meternames" }
};
/// <summary>
/// Creates an instance of <see cref="HoneycombOptions"/> from command line parameters.
/// </summary>
public static HoneycombOptions FromArgs(params string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args, CommandLineSwitchMap)
.Build();
var honeycombOptions = config
.Get<HoneycombOptions>();
var meterNames = config.GetValue<string>("meternames");
if (!string.IsNullOrWhiteSpace(meterNames))
{
honeycombOptions.MeterNames = new List<string>(meterNames.Split(','));
}
return honeycombOptions;
}
}
}