-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOptions.cs
221 lines (193 loc) · 7.27 KB
/
Options.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using LightStep.Logging;
namespace LightStep
{
/// <summary>
/// Options for configuring the LightStep tracer.
/// </summary>
public class Options
{
private static readonly ILog _logger = LogProvider.GetCurrentClassLogger();
/// <summary>
/// An identifier for the Tracer.
/// </summary>
public readonly ulong TracerGuid = new Random().NextUInt64();
/// <summary>
/// API key for a LightStep project.
/// </summary>
public string AccessToken { get; private set; }
/// <summary>
/// If true, tracer will start reporting
/// </summary>
///
public bool Run { get; private set; }
/// <summary>
/// True if the satellite connection should use HTTP/2, false otherwise.
/// </summary>
public bool UseHttp2 { get; private set; }
/// <summary>
/// LightStep Satellite endpoint configuration.
/// </summary>
public SatelliteOptions Satellite { get; private set; }
/// <summary>
/// How often the reporter will send spans to a LightStep Satellite.
/// </summary>
public TimeSpan ReportPeriod { get; private set; }
/// <summary>
/// Timeout for sending spans to a LightStep Satellite.
/// </summary>
public TimeSpan ReportTimeout { get; private set; }
/// <summary>
/// Maximum amount of spans to buffer in a single report.
/// </summary>
public int ReportMaxSpans { get; private set;}
/// <summary>
/// Tags that should be applied to each span generated by this tracer.
/// </summary>
public IDictionary<string, object> Tags { get; private set; }
public Options WithToken(string token)
{
_logger.Debug($"Setting access token to {token}");
AccessToken = token;
return this;
}
public Options WithHttp2()
{
_logger.Debug("Enabling HTTP/2 support.");
UseHttp2 = true;
return this;
}
public Options WithSatellite(SatelliteOptions options)
{
_logger.Debug($"Setting satellite to {options}");
Satellite = options;
return this;
}
public Options WithReportPeriod(TimeSpan period)
{
_logger.Debug($"Setting reporting period to {period}");
ReportPeriod = period;
return this;
}
public Options WithReportTimeout(TimeSpan timeout)
{
_logger.Debug($"Setting report timeout to {timeout}");
ReportTimeout = timeout;
return this;
}
public Options WithTags(IDictionary<string, object> tags)
{
_logger.Debug($"Setting default tags to: {tags.Select(kvp => $"{kvp.Key}:{kvp.Value},")}");
Tags = MergeTags(tags);
return this;
}
public Options WithAutomaticReporting(bool shouldRun)
{
_logger.Debug($"Setting automatic reporting to {shouldRun}");
Run = shouldRun;
return this;
}
public Options WithMaxBufferedSpans(int count)
{
_logger.Debug($"Setting max spans per buffer to {count}");
ReportMaxSpans = count;
return this;
}
/// <summary>
/// Creates a new set of options for the LightStep tracer.
/// </summary>
/// <param name="token">Project API key.</param>
/// <exception cref="ArgumentNullException">An API key is required.</exception>
public Options(string token)
{
if (string.IsNullOrWhiteSpace(token)) throw new ArgumentNullException(nameof(token));
Tags = InitializeDefaultTags();
ReportPeriod = TimeSpan.FromMilliseconds(5000);
ReportTimeout = TimeSpan.FromSeconds(30);
AccessToken = token;
Satellite = new SatelliteOptions("collector.lightstep.com");
UseHttp2 = false;
Run = true;
ReportMaxSpans = int.MaxValue;
}
private IDictionary<string, object> MergeTags(IDictionary<string, object> input)
{
var attributes = InitializeDefaultTags();
var mergedAttributes = new Dictionary<string, object>(input);
foreach (var item in attributes)
{
if (!mergedAttributes.ContainsKey(item.Key))
{
mergedAttributes.Add(item.Key, item.Value);
}
}
return mergedAttributes;
}
private IDictionary<string, object> InitializeDefaultTags()
{
var attributes = new Dictionary<string, object>
{
[LightStepConstants.TracerPlatformKey] = LightStepConstants.TracerPlatformValue,
[LightStepConstants.TracerPlatformVersionKey] = GetPlatformVersion(),
[LightStepConstants.TracerVersionKey] = GetTracerVersion(),
[LightStepConstants.ComponentNameKey] = GetComponentName(),
[LightStepConstants.HostnameKey] = GetHostName(),
[LightStepConstants.CommandLineKey] = GetCommandLine()
};
return attributes;
}
private static string GetTracerVersion()
{
return typeof(LightStep.Tracer).Assembly.GetName().Version.ToString();
}
private static string GetComponentName()
{
var entryAssembly = "";
try
{
entryAssembly = Assembly.GetEntryAssembly().GetName().Name;
}
catch (NullReferenceException)
{
// could not get assembly name, possibly because we're running a test
entryAssembly = "unknown";
}
return entryAssembly;
}
private static string GetPlatformVersion()
{
#if NET45
var version = "";
version = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;
// in unit testing scenarios, GetEntryAssembly returns null so make sure we aren't blowing up if this isn't available
if (version == null && Assembly.GetEntryAssembly() != null)
{
TargetFrameworkAttribute tfa = (TargetFrameworkAttribute) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(TargetFrameworkAttribute))
.SingleOrDefault();
if (tfa != null)
{
version = tfa.FrameworkName;
}
}
return version;
#elif NETSTANDARD2_0
return Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
#else
return "Unknown Framework Version";
#endif
}
private static string GetHostName()
{
return Environment.MachineName;
}
private static string GetCommandLine()
{
return Environment.CommandLine;
}
}
}