Skip to content

Commit 9470bc7

Browse files
author
Sergey Kanzhelev
authored
Merge pull request #443 from Microsoft/sergkanz/internalOperations
mark server telemetyr channel with internal SDK operation
2 parents f5ff412 + 0da2232 commit 9470bc7

File tree

8 files changed

+111
-36
lines changed

8 files changed

+111
-36
lines changed

GlobalStaticVersion.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<SemanticVersionMajor>2</SemanticVersionMajor>
99
<SemanticVersionMinor>3</SemanticVersionMinor>
1010
<SemanticVersionPatch>0</SemanticVersionPatch>
11-
<PreReleaseMilestone>beta2</PreReleaseMilestone>
11+
<PreReleaseMilestone>beta3</PreReleaseMilestone>
1212
<!--
1313
Date when Semantic Version was changed.
1414
Update for every public release.

Test/ServerTelemetryChannel.Test/Shared.Tests/ServerTelemetryChannelTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
using Moq;
1414
using Assert = Xunit.Assert;
1515
using Helpers;
16+
using System.Collections.Generic;
17+
using Extensibility.Implementation;
1618

1719
#if NET45
1820
using TaskEx = System.Threading.Tasks.Task;
@@ -348,5 +350,56 @@ public void PassesTelemetryToTelemetryProcessor()
348350
Assert.Equal(telemetry, sentTelemetry);
349351
}
350352
}
353+
354+
[TestClass]
355+
public class InternalOperation : ServerTelemetryChannelTest
356+
{
357+
class TransmissionStubChecksInternalOperation : Transmission
358+
{
359+
public Action<bool> WasCalled;
360+
361+
public override Task<HttpWebResponseWrapper> SendAsync()
362+
{
363+
Assert.True(SdkInternalOperationsMonitor.IsEntered());
364+
this.WasCalled(true);
365+
return base.SendAsync();
366+
}
367+
}
368+
369+
class TelemetrySerializerStub : TelemetrySerializer
370+
{
371+
public Action<bool> WasCalled;
372+
373+
public TelemetrySerializerStub(Transmitter t) : base(t)
374+
{
375+
}
376+
377+
public override void Serialize(ICollection<ITelemetry> items)
378+
{
379+
var transmission = new TransmissionStubChecksInternalOperation();
380+
transmission.WasCalled = this.WasCalled;
381+
base.Transmitter.Enqueue(transmission);
382+
}
383+
}
384+
385+
[TestMethod]
386+
public void SendWillBeMarkedAsInternalOperation()
387+
{
388+
bool wasCalled = false;
389+
var channel = new ServerTelemetryChannel();
390+
channel.TelemetrySerializer = new TelemetrySerializerStub(channel.Transmitter) { WasCalled = (called) => { wasCalled = called; } };
391+
channel.TelemetryBuffer = new TelemetryChannel.Implementation.TelemetryBuffer(channel.TelemetrySerializer, new WebApplicationLifecycle());
392+
channel.TelemetryProcessor = channel.TelemetryBuffer;
393+
channel.MaxTelemetryBufferCapacity = 1;
394+
channel.Initialize(TelemetryConfiguration.CreateDefault());
395+
396+
var telemetry = new StubTelemetry();
397+
channel.Send(telemetry);
398+
Thread.Sleep(TimeSpan.FromSeconds(1));
399+
400+
Assert.True(wasCalled);
401+
}
402+
}
403+
351404
}
352405
}

src/Core/Managed/Net46/Extensibility/Implementation/RichPayloadEventSource.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
using Microsoft.ApplicationInsights.DataContracts;
2020
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
2121
#endif
22+
23+
/// <summary>
24+
/// Event Source exposes Application Insights telemetry information as ETW events.
25+
/// </summary>
2226
internal sealed partial class RichPayloadEventSource : IDisposable
2327
{
2428
/// <summary>RichPayloadEventSource instance.</summary>

src/Core/Managed/Shared/Extensibility/Implementation/RichPayloadEventSource.Keywords.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#else
66
using System.Diagnostics.Tracing;
77
#endif
8-
8+
9+
/// <summary>
10+
/// Event Source exposes Application Insights telemetry information as ETW events.
11+
/// </summary>
912
internal partial class RichPayloadEventSource
1013
{
1114
/// <summary>

src/Core/Managed/Shared/OperationTelemetryExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public static void Start(this OperationTelemetry telemetry, long timestamp)
4040
// Stopwatch.GetTimestamp() is used (instead of ElapsedTicks) as it is thread-safe.
4141
telemetry.BeginTimeInTicks = timestamp;
4242

43+
#if !CORE_PCL
4344
RichPayloadEventSource.Log.ProcessOperationStart(telemetry);
45+
#endif
4446
}
4547

4648
/// <summary>
@@ -111,7 +113,9 @@ private static void StopImpl(OperationTelemetry telemetry, TimeSpan duration)
111113
telemetry.Timestamp = DateTimeOffset.UtcNow;
112114
}
113115

116+
#if !CORE_PCL
114117
RichPayloadEventSource.Log.ProcessOperationStop(telemetry);
118+
#endif
115119
}
116120
}
117121
}

src/TelemetryChannels/ServerTelemetryChannel/Shared/Implementation/TelemetrySerializer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
internal class TelemetrySerializer
1010
{
11+
internal readonly Transmitter Transmitter;
12+
1113
private const string DefaultEndpointAddress = "https://dc.services.visualstudio.com/v2/track";
12-
private readonly Transmitter transmitter;
1314
private Uri endpointAddress = new Uri(DefaultEndpointAddress);
1415

1516
public TelemetrySerializer(Transmitter transmitter)
@@ -19,7 +20,7 @@ public TelemetrySerializer(Transmitter transmitter)
1920
throw new ArgumentNullException("transmitter");
2021
}
2122

22-
this.transmitter = transmitter;
23+
this.Transmitter = transmitter;
2324
}
2425

2526
protected TelemetrySerializer()
@@ -64,7 +65,7 @@ public virtual void Serialize(ICollection<ITelemetry> items)
6465
}
6566

6667
var transmission = new Transmission(this.endpointAddress, items);
67-
this.transmitter.Enqueue(transmission);
68+
this.Transmitter.Enqueue(transmission);
6869
}
6970
}
7071
}

src/TelemetryChannels/ServerTelemetryChannel/Shared/Implementation/TransmissionSender.cs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Runtime.Serialization;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Extensibility;
1011
using Microsoft.ApplicationInsights.Channel;
1112
using Microsoft.ApplicationInsights.Extensibility.Implementation;
1213

@@ -143,46 +144,55 @@ protected void OnTransmissionSent(TransmissionProcessedEventArgs args)
143144

144145
private async Task StartSending(Transmission transmission)
145146
{
146-
Exception exception = null;
147-
HttpWebResponseWrapper responseContent = null;
147+
SdkInternalOperationsMonitor.Enter();
148148

149-
// Locally self-throttle this payload before we send it
150-
Transmission acceptedTransmission = this.Throttle(transmission);
151-
152-
// Now that we've self-imposed a throttle, we can try to send the remaining data
153149
try
154150
{
155-
TelemetryChannelEventSource.Log.TransmissionSendStarted(acceptedTransmission.Id);
156-
responseContent = await acceptedTransmission.SendAsync().ConfigureAwait(false);
157-
}
158-
catch (Exception e)
159-
{
160-
exception = e;
161-
}
162-
finally
163-
{
164-
int currentCapacity = Interlocked.Decrement(ref this.transmissionCount);
165-
if (exception == null)
151+
Exception exception = null;
152+
HttpWebResponseWrapper responseContent = null;
153+
154+
// Locally self-throttle this payload before we send it
155+
Transmission acceptedTransmission = this.Throttle(transmission);
156+
157+
// Now that we've self-imposed a throttle, we can try to send the remaining data
158+
try
166159
{
167-
TelemetryChannelEventSource.Log.TransmissionSentSuccessfully(acceptedTransmission.Id, currentCapacity);
160+
TelemetryChannelEventSource.Log.TransmissionSendStarted(acceptedTransmission.Id);
161+
responseContent = await acceptedTransmission.SendAsync().ConfigureAwait(false);
168162
}
169-
else
163+
catch (Exception e)
170164
{
171-
TelemetryChannelEventSource.Log.TransmissionSendingFailedWarning(acceptedTransmission.Id, exception.ToString());
165+
exception = e;
172166
}
173-
174-
if (responseContent == null && exception is WebException)
167+
finally
175168
{
176-
HttpWebResponse response = (HttpWebResponse)((WebException)exception).Response;
177-
responseContent = new HttpWebResponseWrapper()
169+
int currentCapacity = Interlocked.Decrement(ref this.transmissionCount);
170+
if (exception == null)
178171
{
179-
StatusCode = (int)response.StatusCode,
180-
StatusDescription = response.StatusDescription,
181-
RetryAfterHeader = response.Headers?.Get("Retry-After")
182-
};
183-
}
172+
TelemetryChannelEventSource.Log.TransmissionSentSuccessfully(acceptedTransmission.Id, currentCapacity);
173+
}
174+
else
175+
{
176+
TelemetryChannelEventSource.Log.TransmissionSendingFailedWarning(acceptedTransmission.Id, exception.ToString());
177+
}
178+
179+
if (responseContent == null && exception is WebException)
180+
{
181+
HttpWebResponse response = (HttpWebResponse)((WebException)exception).Response;
182+
responseContent = new HttpWebResponseWrapper()
183+
{
184+
StatusCode = (int)response.StatusCode,
185+
StatusDescription = response.StatusDescription,
186+
RetryAfterHeader = response.Headers?.Get("Retry-After")
187+
};
188+
}
184189

185-
this.OnTransmissionSent(new TransmissionProcessedEventArgs(acceptedTransmission, exception, responseContent));
190+
this.OnTransmissionSent(new TransmissionProcessedEventArgs(acceptedTransmission, exception, responseContent));
191+
}
192+
}
193+
finally
194+
{
195+
SdkInternalOperationsMonitor.Exit();
186196
}
187197
}
188198

src/TelemetryChannels/ServerTelemetryChannel/Shared/ServerTelemetryChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// </summary>
1717
public sealed class ServerTelemetryChannel : ITelemetryChannel, ITelemetryModule
1818
{
19-
internal readonly TelemetrySerializer TelemetrySerializer;
19+
internal TelemetrySerializer TelemetrySerializer;
2020
internal TelemetryBuffer TelemetryBuffer;
2121
internal Transmitter Transmitter;
2222

0 commit comments

Comments
 (0)