/// <summary> /// /// </summary> /// <param name="telemetryPipeline"></param> /// <returns></returns> public static MetricManager Metrics(this TelemetryConfiguration telemetryPipeline) { if (telemetryPipeline == null) { return(null); } // Fast path for the default configuration: if (telemetryPipeline == TelemetryConfiguration.Active) { MetricManager manager = s_defaultMetricManager; if (manager == null) { var pipelineAdapter = new ApplicationInsightsTelemetryPipeline(telemetryPipeline); MetricManager newManager = new MetricManager(pipelineAdapter); MetricManager prevManager = Interlocked.CompareExchange(ref s_defaultMetricManager, newManager, null); if (prevManager == null) { return(newManager); } else { Task fireAndForget = newManager.StopDefaultAggregationCycleAsync(); return(prevManager); } } return(manager); } // Ok, we have a non-default config. Get the table: ConditionalWeakTable <TelemetryConfiguration, MetricManager> metricManagers = s_metricManagers; if (metricManagers == null) { ConditionalWeakTable <TelemetryConfiguration, MetricManager> newTable = new ConditionalWeakTable <TelemetryConfiguration, MetricManager>(); ConditionalWeakTable <TelemetryConfiguration, MetricManager> prevTable = Interlocked.CompareExchange(ref s_metricManagers, newTable, null); metricManagers = prevTable ?? newTable; } // Get the manager from the table: { MetricManager manager = GetOrGreateFromTable(telemetryPipeline, metricManagers); return(manager); } }
internal MetricManager GetMetricManager(bool createIfNotExists) { MetricManager manager = this.metricManager; if (manager == null && createIfNotExists) { var pipelineAdapter = new ApplicationInsightsTelemetryPipeline(this); MetricManager newManager = new MetricManager(pipelineAdapter); MetricManager prevManager = Interlocked.CompareExchange(ref this.metricManager, newManager, null); if (prevManager == null) { manager = newManager; } else { // We just created a new manager that we are not using. Stop is before discarding. Task fireAndForget = newManager.StopDefaultAggregationCycleAsync(); manager = prevManager; } } return(manager); }