private void WriteSamples(IEnumerable<QuickPulseDataSample> samples, string instrumentationKey, Stream stream)
        {
            var monitoringPoints = new List<MonitoringDataPoint>();

            foreach (var sample in samples)
            {
                var metricPoints = new List<MetricPoint>
                                       {
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Requests/Sec",
                                                   Value = Round(sample.AIRequestsPerSecond),
                                                   Weight = 1
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Request Duration",
                                                   Value = Round(sample.AIRequestDurationAveInMs),
                                                   Weight = sample.AIRequests
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Requests Failed/Sec",
                                                   Value = Round(sample.AIRequestsFailedPerSecond),
                                                   Weight = 1
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Requests Succeeded/Sec",
                                                   Value = Round(sample.AIRequestsSucceededPerSecond),
                                                   Weight = 1
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Dependency Calls/Sec",
                                                   Value = Round(sample.AIDependencyCallsPerSecond),
                                                   Weight = 1
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Dependency Call Duration",
                                                   Value = Round(sample.AIDependencyCallDurationAveInMs),
                                                   Weight = sample.AIDependencyCalls
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Dependency Calls Failed/Sec",
                                                   Value = Round(sample.AIDependencyCallsFailedPerSecond),
                                                   Weight = 1
                                               },
                                           new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Dependency Calls Succeeded/Sec",
                                                   Value = Round(sample.AIDependencyCallsSucceededPerSecond),
                                                   Weight = 1
                                               },
                                            new MetricPoint
                                               {
                                                   Name = @"\ApplicationInsights\Exceptions/Sec",
                                                   Value = Round(sample.AIExceptionsPerSecond),
                                                   Weight = 1
                                               }
                                       };

                metricPoints.AddRange(sample.PerfCountersLookup.Select(counter => new MetricPoint { Name = counter.Key, Value = Round(counter.Value), Weight = 1 }));

                ITelemetryDocument[] documents = sample.TelemetryDocuments.ToArray();
                Array.Reverse(documents);

                var dataPoint = new MonitoringDataPoint
                                    {
                                        Version = this.version,
                                        InvariantVersion = MonitoringDataPoint.CurrentInvariantVersion,
                                        InstrumentationKey = instrumentationKey,
                                        Instance = this.instanceName,
                                        StreamId = this.streamId,
                                        MachineName = this.machineName,
                                        Timestamp = sample.EndTimestamp.UtcDateTime,
                                        IsWebApp = this.isWebApp,
                                        Metrics = metricPoints.ToArray(),
                                        Documents = documents
                                    };

                monitoringPoints.Add(dataPoint);
            }

            this.serializerDataPointArray.WriteObject(stream, monitoringPoints.ToArray());
        }
        private void WritePingData(DateTimeOffset timestamp, Stream stream)
        {
            var dataPoint = new MonitoringDataPoint
            {
                Version = this.version,
                InvariantVersion = MonitoringDataPoint.CurrentInvariantVersion,
                //InstrumentationKey = instrumentationKey, // ikey is currently set in query string parameter
                Instance = this.instanceName,
                StreamId = this.streamId,
                MachineName = this.machineName,
                Timestamp = timestamp.UtcDateTime,
                IsWebApp = this.isWebApp
            };

            this.serializerDataPoint.WriteObject(stream, dataPoint);
        }