Last active
May 23, 2018 01:01
-
-
Save lmolkova/ca0ac9d68ad7e3dc93760913cda5abc3 to your computer and use it in GitHub Desktop.
Demo/prototype for preventing certain dependency calls based on the scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.ApplicationInsights.Channel; | |
using Microsoft.ApplicationInsights.DependencyCollector; | |
using Microsoft.ApplicationInsights.Extensibility; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Queue; | |
namespace filtering | |
{ | |
class MyTelemetryInitializer : ITelemetryInitializer | |
{ | |
public void Initialize(ITelemetry telemetry) | |
{ | |
if (Program.myMonitor.Value == null) | |
{ | |
telemetry.Context.Properties["block"] = "true"; | |
} | |
} | |
} | |
class MyTelemetryFilter : ITelemetryProcessor | |
{ | |
private readonly ITelemetryProcessor next; | |
public int ProcessedTelemtryItems { get; set; } | |
public MyTelemetryFilter(ITelemetryProcessor next) | |
{ | |
this.next = next; | |
} | |
public void Process(ITelemetry item) | |
{ | |
if (item.Context.Properties.ContainsKey("block")) | |
return; | |
ProcessedTelemtryItems++; | |
this.next.Process(item); | |
} | |
} | |
class Program | |
{ | |
public static AsyncLocal<object> myMonitor = new AsyncLocal<object>(); | |
private static CloudQueue _queue; | |
static void Main(string[] args) | |
{ | |
MyTelemetryFilter filter = null; | |
TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer()); | |
var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; | |
builder.Use(next => { return filter = new MyTelemetryFilter(next); }); | |
builder.Build(); | |
DependencyTrackingTelemetryModule module = new DependencyTrackingTelemetryModule(); | |
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("http://127.0.0.1"); | |
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net"); | |
module.Initialize(TelemetryConfiguration.Active); | |
// will be tracked | |
myMonitor.Value = new object(); | |
SetupQueue(); | |
Task.Run(async () => | |
{ | |
for (int i = 0; i < 50; i++) | |
{ | |
await _queue.AddMessageAsync(new CloudQueueMessage("hello")); | |
} | |
}).Wait(); | |
myMonitor.Value = null; | |
Console.WriteLine($"Tracking enabled: tracked {filter.ProcessedTelemtryItems} items"); | |
filter.ProcessedTelemtryItems = 0; | |
// won't be tracked | |
Task.Run(async () => | |
{ | |
for (int i = 0; i < 50; i++) | |
{ | |
await _queue.AddMessageAsync(new CloudQueueMessage("hello")); | |
} | |
}).Wait(); | |
Console.WriteLine($"Tracking disabled: tracked {filter.ProcessedTelemtryItems} items"); | |
Console.ReadKey(); | |
} | |
private static void SetupQueue() | |
{ | |
string connString = "UseDevelopmentStorage=true"; | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connString); | |
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); | |
_queue = queueClient.GetQueueReference("asynclocal"); | |
_queue.CreateIfNotExistsAsync().Wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or even better example that demonstrates that calls done under
myMonitor.Value != null
are tracked...