Skip to content

Instantly share code, notes, and snippets.

@pksorensen
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save pksorensen/4858e5f3e54db62e8d1b to your computer and use it in GitHub Desktop.

Select an option

Save pksorensen/4858e5f3e54db62e8d1b to your computer and use it in GitHub Desktop.
public class DependencyFilterChannel : ITelemetryChannel, ISupportConfiguration
{
private PersistenceChannel channel;
public int SampleEvery { get; set; }
public DependencyFilterChannel()
{
this.channel = new PersistenceChannel();
}
public void Initialize(TelemetryConfiguration configuration)
{
this.channel.Initialize(configuration);
}
private PropertyInfo _property;
public static PropertyInfo GetNameProperty()
{
var type = Type.GetType("Microsoft.ApplicationInsights.DataContracts.RemoteDependencyTelemetry, Microsoft.ApplicationInsights");
return type.GetProperty("Name");
}
public void Send(ITelemetry item)
{
if (item.GetType().Name == "RemoteDependencyTelemetry")
{
LazyInitializer.EnsureInitialized(ref _property, GetNameProperty);
string name = _property.GetValue(item) as string;
//ignore these:https://ascendxyzdatastagingweu.blob.core.windows.net/0f0f36bcf00244ce8f7096c29076d4cb-test/_FHV2872.jpg?comp=block&blockid=YmxvY2stMDAwMDEx
try
{
var url = new UriBuilder(name);
if (url.Query.Contains("blockid") && url.Query.Contains("block"))
{
return;
}
}
catch (Exception)
{
}
}
else if(item is RequestTelemetry)
{
var requst = item as RequestTelemetry;
if (requst.Url.Query.Contains("blockid") && requst.Url.Query.Contains("block"))
{
return;
}
}
this.channel.Send(item);
}
public bool? DeveloperMode
{
get
{
return this.channel.DeveloperMode;
}
set
{
this.channel.DeveloperMode = value;
}
}
public string EndpointAddress
{
get
{
return this.channel.EndpointAddress;
}
set
{
this.channel.EndpointAddress = value;
}
}
public void Flush()
{
this.channel.Flush();
}
public void Dispose()
{
this.channel.Dispose();
}
}
@pksorensen
Copy link
Author

Cool. I will leave some more feedback here. (just disccussed some owin stuff here https://jabbr.net/#/rooms/owin) not sure if its easy to find again so will just summarize

I was playing with RemoteDependencyModule and on the azure portal the dependencies are not grouped under requests.

I believe this is a result of using

public class ItemCorrelationTelemetryInitializer : ITelemetryInitializer
{

    public const string DEFAULT_CORRELATION_SLOT = "CORRELATION-ID";

    public string CorrelationSlot { get; set; }
    public ItemCorrelationTelemetryInitializer(string callContextCorrelationSlot = DEFAULT_CORRELATION_SLOT)
    {
        CorrelationSlot = callContextCorrelationSlot;
    }
    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.Operation.Id = (string)CallContext.LogicalGetData(CorrelationSlot);
    }
}

and

            app.Use(async (ctx, next) =>
            {
                var sw = new Stopwatch();
                var startTime = DateTimeOffset.Now;
                sw.Start();

                try
                {
                    CallContext.LogicalSetData(ItemCorrelationTelemetryInitializer.DEFAULT_CORRELATION_SLOT, Guid.NewGuid().ToString());

                    await next();
                    sw.Stop();
                    PushTelemetry(ctx, sw.Elapsed, startTime);
                }
                catch (Exception ex)
                {
                    sw.Stop();
                    PushTelemetry(ctx, sw.Elapsed, startTime, ex);

                }
            });

since the initializers are not getting the CallContext from the owin request. (its null for dependency telemetry).

I inspected the code for Web Extensions for WebOperationIdTelemetryInitializer and realized it uses HttpContext.Current to get the operationid (using system.web)

I dont want system.web into my application and at this point it do not seem like it's possible in any way to get the owin context and update the properties.

I havent been able to come up with an extension point that would make sense at this point for how to use the existing code. I am considering to do a simple owin middleware that logs dependencies in the same way that RemoteDependencyModule is getting them from ETW

@SergeyKanzhelev
Copy link

Yes, CallContext will not be saved across async/await. We are working on improving Dependency correlation to Requests and generic items correlation.

For RemoteDependencies - why wouldn't you just use Microsoft.ApplicationInsights.Extensibility.RuntimeTelemetry.RemoteDependencyModule?

@pksorensen
Copy link
Author

I would but its not able to correlate the dependencies with requests unless i use the package Microsoft.ApplicationInsights.Web to set the correlation id. (I dont want to use Microsoft.ApplicationInsights.Web as it pulls in system.web).

@pksorensen
Copy link
Author

For people dropping by and wondering about CallContext: Heres a small demo that shows how it works: https://gist.github.com/s093294/8a60759d9d29d2a33b8b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment