TheFreeman
TheFreeman

Reputation: 181

Application Insights - TelemetryClient - DependencyTelemetry - UseSampling

I am trying to enable sampling with my AppInsightsHelper class which starts Depedancy operations to track performance.

This is how I am initializing my TelematryClient:

 public ApplicationInsightsHelper(string key)
 {
        var config = TelemetryConfiguration.CreateDefault();
        config.InstrumentationKey = key;
        config.DefaultTelemetrySink.TelemetryProcessorChainBuilder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 1);

        _telemetryClient = new TelemetryClient(config);
 }

and then Starting and Stopping the operation:

   IOperationHolder<DependencyTelemetry> operation = null;
   operation = _telemetryClient.StartOperation<DependencyTelemetry>(friendlyName);
   operation.Telemetry.Name = friendlyName;
   operation.Telemetry.Type = type;
   operation.Telemetry.Timestamp = DateTime.UtcNow;

   operation.Telemetry.Duration = DateTime.UtcNow - operation.Telemetry.Timestamp;
   _telemetryClient.StopOperation(operation);

The issue is that the above code seems to ignore the Sampling setting and all operations are traced. I have also included : excludedTypes: "Dependency" within the UseAdaptiveSampling to see if anything happens and as expected the Dependencies are not ignored.

Upvotes: 0

Views: 4014

Answers (2)

yuvraj
yuvraj

Reputation: 197

This has been worked for me for ASP.NET web application. I have added below configuration and specifically added my 'MaksingTelemetryInitializer'.

    public void StartApplicationInsights(string logType)
    {
        string appInsightsComponentId = string.Empty;
        try
        {
            telemetryClient = new TelemetryClient();
            TelemetryConfiguration.Active.InstrumentationKey = GetConfigvalue("AppInsightsAppId"); ;
            TelemetryConfiguration.Active.TelemetryInitializers.Add(new MaskingTelemetryInitializer());
        }
        catch (Exception exception)
        {
            // Log Exception to WadLog if logging to Wadlog is enabled
            if (logType != LoggingType.Both) return;
            WadLogWriter.LogToWadLogs(Logger.BuildErrorString(exception), EventLevel.Error);
        }
    }

Here I wanted mask PII data email id, it is working.

Upvotes: 0

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29985

If it's an azure function, you can set sampling via host.json, see here and here for details. An example as below:

{
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond" : 1
      }
    }
  }
}

And if you want to use TelemetryClient with the settings, you should follow this article. In the constructor of the azure function, use code like below:

        /// Using dependency injection will guarantee that you use the same configuration for telemetry collected automatically and manually.
        public HttpTrigger2(TelemetryConfiguration telemetryConfiguration)
        {
            this.telemetryClient = new TelemetryClient(telemetryConfiguration);
        }

But as of now, there is an issue by using telemetryConfiguration.

Upvotes: 2

Related Questions