public static void Run() { TelemetryConfiguration configuration = new TelemetryConfiguration(); configuration.InstrumentationKey = "fb8a0b03-235a-4b52-b491-307e9fd6b209"; // automatically track dependency calls var dependencies = new DependencyTrackingTelemetryModule(); dependencies.Initialize(configuration); // automatically correlate all telemetry data with request configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); // set default properties for all telemetry items configuration.TelemetryInitializers.Add(new DefaultTelemetryInitializer()); // send all event telemetry to a different iKey configuration.TelemetryInitializers.Add(new BusinessTelemetryInitializer()); // configure all telemetry to be sent to a single node configuration.TelemetryInitializers.Add(new NodeNameTelemetryInitializer()); // initialize price calculation logic var state = new _State(); state.Initialize(); // enable sampling configuration.TelemetryProcessorChainBuilder // this telemetry processor will be executed first for all telemetry items to calculate the size and # of items .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Collected)); }) // this telemetry processor will be execuyted ONLY when telemetry is sampled in .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Sent)); }) .Build(); TelemetryClient client = new TelemetryClient(configuration); var iterations = 0; // configure metrics collection MetricManager metricManager = new MetricManager(client); var itemsProcessed = metricManager.CreateMetric("Iterations"); var processingFailed = metricManager.CreateMetric("Failed processing"); var processingSize = metricManager.CreateMetric("Processing size"); while (!state.IsTerminated) { iterations++; using (var operaiton = client.StartOperation <RequestTelemetry>("Process item")) { client.TrackEvent("test"); client.TrackTrace("Something happened", SeverityLevel.Information); try { HttpClient http = new HttpClient(); var task = http.GetStringAsync("http://bing.com"); task.Wait(); // metrics aggregation. Metrics are aggregated and sent once per minute itemsProcessed.Track(1); processingSize.Track(task.Result.Length); processingFailed.Track(0); // raw metric telemetry. Each call represents a document. client.TrackMetric("[RAW] Response size", task.Result.Length); } catch (Exception exc) { // raw metric telemetry client.TrackMetric("[RAW] Successful responses", 0); // metrics aggregation: processingFailed.Track(1); client.TrackException(exc); operaiton.Telemetry.Success = false; } finally { client.TrackMetric("[RAW] Iterations", 1); itemsProcessed.Track(1); } // client.StopOperation(operaiton); // Console.WriteLine($"Iteration {iterations}. Elapesed time: {operaiton.Telemetry.Duration}"); } } // send all metrics before exiting the program metricManager.Flush(); Console.WriteLine($"Program sent 100K of telemetry in {iterations} iterations!"); Console.ReadLine(); }
static void Main_backup(string[] args) { var state = new _State(); state.Initialize(); TelemetryConfiguration configuration = new TelemetryConfiguration(); configuration.InstrumentationKey = "fb8a0b03-235a-4b52-b491-307e9fd6b209"; var telemetryChannel = new ServerTelemetryChannel(); telemetryChannel.Initialize(configuration); configuration.TelemetryChannel = telemetryChannel; // data collection modules var dependencies = new DependencyTrackingTelemetryModule(); dependencies.Initialize(configuration); // telemetry initializers configuration.TelemetryInitializers.Add(new AppVersionTelemetryInitializer()); configuration.TelemetryInitializers.Add(new DefaultTelemetryInitializer()); configuration.TelemetryInitializers.Add(new BusinessTelemetryInitializer()); configuration.TelemetryInitializers.Add(new NodeNameTelemetryInitializer()); configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); // telemetry processors configuration.TelemetryProcessorChainBuilder .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Collected)); }) // .Use((next) => { return new MyTelemetryProcessor(next); }) // .Use((next) => { return new CleanAutoCollecctedTelemetryProcessor(next); }) // .Use((next) => { return new ExampleTelemetryProcessor(next); }) // send only 10% of all dependency data .Use((next) => { return(new SamplingTelemetryProcessor(next) { IncludedTypes = "Dependency", SamplingPercentage = 10 }); }) // start sampling when load exceeds 2 events per second for all telemetry types except events .Use((next) => { return(new AdaptiveSamplingTelemetryProcessor(next) { ExcludedTypes = "Event", MaxTelemetryItemsPerSecond = 2, SamplingPercentageIncreaseTimeout = TimeSpan.FromSeconds(1), SamplingPercentageDecreaseTimeout = TimeSpan.FromSeconds(1), EvaluationInterval = TimeSpan.FromSeconds(1), InitialSamplingPercentage = 25 }); }) .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Sent)); }) .Build(); TelemetryClient client = new TelemetryClient(configuration); var iterations = 0; MetricManager metricManager = new MetricManager(client); var itemsProcessed = metricManager.CreateMetric("Items processed"); var processingFailed = metricManager.CreateMetric("Failed processing"); var processingSize = metricManager.CreateMetric("Processing size"); while (!state.IsTerminated) { iterations++; using (var operaiton = client.StartOperation <RequestTelemetry>("Process item")) { client.TrackEvent("test"); client.TrackTrace("Something happened"); try { HttpClient http = new HttpClient(); var task = http.GetStringAsync("http://bing.com"); task.Wait(); // metrics aggregation: //itemsProcessed.Track(1); //processingSize.Track(task.Result.Length); //processingFailed.Track(0); //client.TrackMetric("Response size", task.Result.Length); //client.TrackMetric("Successful responses", 1); } catch (Exception exc) { //client.TrackMetric("Successful responses", 0); //operaiton.Telemetry.Success = false; // metrics aggregation: //processingFailed.Track(1); } } } Console.WriteLine($"Program sent 1Mb of telemetry in {iterations} iterations!"); Console.ReadLine(); }
public static void Run() { TelemetryConfiguration configuration = new TelemetryConfiguration(); configuration.InstrumentationKey = "fb8a0b03-235a-4b52-b491-307e9fd6b209"; // automatically track dependency calls var dependencies = new DependencyTrackingTelemetryModule(); dependencies.Initialize(configuration); // automatically correlate all telemetry data with request configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer()); //// initialize price calculation logic var state = new _State(); state.Initialize(); // enable sampling configuration.TelemetryProcessorChainBuilder // this telemetry processor will be executed first for all telemetry items to calculate the size and # of items .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Collected)); }) // this is a standard fixed sampling processor that will let only 10% .Use((next) => { return(new SamplingTelemetryProcessor(next) { IncludedTypes = "Dependency", SamplingPercentage = 10 }); }) // this is a standard adaptive sampling telemetry processor that will sample in/out any telemetry item it receives .Use((next) => { return(new AdaptiveSamplingTelemetryProcessor(next) { ExcludedTypes = "Event", // exclude custom events from being sampled MaxTelemetryItemsPerSecond = 1, //default: 5 calls/sec SamplingPercentageIncreaseTimeout = TimeSpan.FromSeconds(1), //default: 2 min SamplingPercentageDecreaseTimeout = TimeSpan.FromSeconds(1), //default: 30 sec EvaluationInterval = TimeSpan.FromSeconds(1), //default: 15 sec InitialSamplingPercentage = 25 //default: 100% }); }) // this telemetry processor will be execuyted ONLY when telemetry is sampled in .Use((next) => { return(new PriceCalculatorTelemetryProcessor(next, state.Sent)); }) .Build(); TelemetryClient client = new TelemetryClient(configuration); var iterations = 0; while (!state.IsTerminated) { iterations++; using (var operaiton = client.StartOperation <RequestTelemetry>("Process item")) { client.TrackEvent("test"); client.TrackTrace("Something happened", SeverityLevel.Information); try { HttpClient http = new HttpClient(); var task = http.GetStringAsync("http://bing.com"); task.Wait(); } catch (Exception exc) { client.TrackException(exc); operaiton.Telemetry.Success = false; } //client.StopOperation(operaiton); //Console.WriteLine($"Iteration {iterations}. Elapesed time: {operaiton.Telemetry.Duration}"); } } Console.WriteLine($"Program sent 100K of telemetry in {iterations} iterations!"); Console.ReadLine(); }