-
Notifications
You must be signed in to change notification settings - Fork 280
/
InMemoryChannel.cs
251 lines (224 loc) · 9.03 KB
/
InMemoryChannel.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
namespace Microsoft.ApplicationInsights.Channel
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.Common;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Authentication;
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
/// <summary>
/// Represents a communication channel for sending telemetry to Application Insights via HTTPS. There will be a buffer that will not be persisted, to enforce the
/// queued telemetry items to be sent, <see cref="ITelemetryChannel.Flush"/> should be called.
/// </summary>
public class InMemoryChannel : ITelemetryChannel, IAsyncFlushable, ISupportCredentialEnvelope
{
private readonly TelemetryBuffer buffer;
private readonly InMemoryTransmitter transmitter;
private readonly InterlockedThrottle throttleEmptyIkeyLog = new InterlockedThrottle(interval: TimeSpan.FromSeconds(30));
private bool? developerMode = false;
private int bufferSize;
/// <summary>
/// Indicates if this instance has been disposed of.
/// </summary>
private bool isDisposed = false;
/// <summary>
/// Initializes a new instance of the <see cref="InMemoryChannel" /> class.
/// </summary>
public InMemoryChannel()
{
this.buffer = new TelemetryBuffer();
this.transmitter = new InMemoryTransmitter(this.buffer);
}
/// <summary>
/// Initializes a new instance of the <see cref="InMemoryChannel" /> class. Used in unit tests for constructor injection.
/// </summary>
/// <param name="telemetryBuffer">The telemetry buffer that will be used to enqueue new events.</param>
/// <param name="transmitter">The in memory transmitter that will send the events queued in the buffer.</param>
internal InMemoryChannel(TelemetryBuffer telemetryBuffer, InMemoryTransmitter transmitter)
{
this.buffer = telemetryBuffer;
this.transmitter = transmitter;
}
/// <summary>
/// Gets or sets a value indicating whether developer mode of telemetry transmission is enabled.
/// </summary>
public bool? DeveloperMode
{
get
{
return this.developerMode;
}
set
{
if (value != this.developerMode)
{
if (value.HasValue && value.Value)
{
this.bufferSize = this.buffer.Capacity;
this.buffer.Capacity = 1;
}
else
{
this.buffer.Capacity = this.bufferSize;
}
this.developerMode = value;
}
}
}
/// <summary>
/// Gets or sets the sending interval. Once the interval expires, <see cref="InMemoryChannel"/>
/// serializes the accumulated telemetry items for transmission and sends it over the wire.
/// </summary>
public TimeSpan SendingInterval
{
get
{
return this.transmitter.SendingInterval;
}
set
{
this.transmitter.SendingInterval = value;
}
}
/// <summary>
/// Gets or sets the <see cref="CredentialEnvelope"/> which is used for AAD.
/// </summary>
/// <remarks>
/// <see cref="ISupportCredentialEnvelope.CredentialEnvelope"/> on <see cref="InMemoryChannel"/> sets <see cref="InMemoryTransmitter.CredentialEnvelope"/>
/// which is used to set <see cref="Transmission.CredentialEnvelope"/> just before calling <see cref="Transmission.SendAsync"/>.
/// </remarks>
CredentialEnvelope ISupportCredentialEnvelope.CredentialEnvelope
{
get => this.transmitter.CredentialEnvelope;
set => this.transmitter.CredentialEnvelope = value;
}
/// <summary>
/// Gets or sets the HTTP address where the telemetry is sent.
/// </summary>
public string EndpointAddress
{
get { return this.transmitter.EndpointAddress?.ToString(); }
set { this.transmitter.EndpointAddress = new Uri(value); }
}
/// <summary>
/// Gets or sets the maximum number of telemetry items will accumulate in a memory before
/// the <see cref="InMemoryChannel"/> serializing them for transmission to Application Insights.
/// This is not a hard limit on how many unsent items can be in the buffer.
/// </summary>
public int MaxTelemetryBufferCapacity
{
get { return this.buffer.Capacity; }
set { this.buffer.Capacity = value; }
}
/// <summary>
/// Gets or sets the maximum number of telemetry items that can be in the backlog to send. This is a hard limit
/// and Items will be dropped by the <see cref="InMemoryChannel"/> once this limit is hit until items are drained from the buffer.
/// </summary>
public int BacklogSize
{
get { return this.buffer.BacklogSize; }
set { this.buffer.BacklogSize = value; }
}
internal bool IsDisposed => this.isDisposed;
/// <summary>
/// Sends an instance of ITelemetry through the channel.
/// </summary>
public void Send(ITelemetry item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
if (this.isDisposed)
{
CoreEventSource.Log.InMemoryChannelSendCalledAfterBeingDisposed();
return;
}
if (string.IsNullOrEmpty(item.Context.InstrumentationKey))
{
if (CoreEventSource.IsVerboseEnabled)
{
CoreEventSource.Log.ItemRejectedNoInstrumentationKey(item.ToString());
}
else
{
if (!Debugger.IsAttached)
{
this.throttleEmptyIkeyLog.PerformThrottledAction(() => CoreEventSource.Log.TelemetryChannelNoInstrumentationKey());
}
}
return;
}
try
{
this.buffer.Enqueue(item);
}
catch (Exception e)
{
CoreEventSource.Log.LogVerbose("TelemetryBuffer.Enqueue failed: " + e.ToString());
}
}
/// <summary>
/// Will send all the telemetry items stored in the memory.
/// </summary>
public void Flush()
{
this.Flush(default(TimeSpan)); // when default(TimeSpan) is provided, value is ignored and default timeout of 100 sec is used
}
/// <summary>
/// Will send all the telemetry items stored in the memory.
/// </summary>
/// <param name="timeout">Timeout interval to abort sending.</param>
public void Flush(TimeSpan timeout)
{
this.transmitter.Flush(timeout);
if (this.isDisposed)
{
CoreEventSource.Log.InMemoryChannelFlushedAfterBeingDisposed();
}
}
/// <summary>
/// Will send all the telemetry items stored in the memory asynchronously.
/// </summary>
/// <param name="cancellationToken">CancellationToken.</param>
/// <returns>
/// This method is hard-coded to return true. This channel offers minimal reliability guarantees and doesn't retry sending telemetry after a failure.
/// </returns>
/// <remarks>
/// <a href="https://docs.microsoft.com/azure/azure-monitor/app/telemetry-channels#built-in-telemetry-channels">Learn more</a>
/// </remarks>
public Task<bool> FlushAsync(CancellationToken cancellationToken)
{
return Task<bool>.Run(() =>
{
this.Flush(default(TimeSpan)); // when default(TimeSpan) is provided, value is ignored and default timeout of 100 sec is used
return Task.FromResult(true);
}, cancellationToken);
}
/// <summary>
/// Disposing the channel.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose the channel if not already disposed.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing && !this.isDisposed)
{
this.isDisposed = true;
if (this.transmitter != null)
{
this.transmitter.Dispose();
}
}
}
}
}