// extension method to include EnvironmentTelemetry in TelemetryCollection
 public static void Aggregate(this TelemetryCollection telemetryCollection, EnvironmentTelemetry environmentTelemetry)
 {
     telemetryCollection.Aggregate(
         environmentTelemetry.TraceAggregationCfg,
         string.Empty,
         environmentTelemetry.Metrics,
         environmentTelemetry.Properties,
         DateTime.UtcNow);
 }
        public TelemetryEtwSink(
            ITelemetryWriter telemetryWriter,
            string clusterId,
            string tenantId,
            string clusterType,
            string serviceFabricVersion,
            string nodeName,
            bool isScaleMin,
            TelemetryScheduler telemetryScheduler)
        {
            string clusterDevType = isScaleMin.ToString();

            this.telemetryWriter = telemetryWriter;

            // creating filter to only aggregate traces which are white-listed
            this.telemetryFilter = new TelemetryFilter();

            // creating the unique identifier of the cluster/node
            this.telemetryIdentifier = new TelemetryIdentifiers(clusterId, tenantId, nodeName);

            // getting path to where telemetry will be persisted
            Utility.CreateWorkSubDirectory(
                Utility.TraceSource,
                LogSourceId,
                TelemetryWorkSubDirectoryKey,
                AggregationPersistenceFolder,
                Utility.DcaWorkFolder,
                out this.telemetryPersistFilePath);

            this.telemetryPersistFilePath = Path.Combine(this.telemetryPersistFilePath, TelemetryEtwSink.TelemetryPersistFileName);

            // initializing the collection of telemetry (first it tries to load it from file, if fails an empty collection is created)
            bool telCollectionloadedFromDisk;

            this.telemetryCollection = TelemetryCollection.LoadOrCreateTelemetryCollection(this.telemetryPersistFilePath, this.telemetryIdentifier, telemetryScheduler.DailyPushes, out telCollectionloadedFromDisk, Utility.TraceSource.WriteError);

            // updating the scheduling for sending telemetry based on the persisted (or not persisted) telemetryCollection.
            telemetryScheduler.UpdateScheduler(telCollectionloadedFromDisk, this.telemetryCollection.TotalPushes > 0, this.telemetryCollection.PushTime, DateTime.Now.ToLocalTime().TimeOfDay);
            this.telemetryCollection.PushTime = telemetryScheduler.PushTime;

            // creating environment telemetry and including it in the collection
            this.environmentTelemetry = new EnvironmentTelemetry(
                clusterType,
                serviceFabricVersion,
                clusterDevType);

            this.telemetryCollection.Aggregate(this.environmentTelemetry);

            this.telemetryPerformanceInstrumentation = new TelemetryPerformanceInstrumentation();
            this.telemetryCollection.Aggregate(this.telemetryPerformanceInstrumentation);

            this.telemetryLock = new object();

            Utility.TraceSource.WriteInfo(LogSourceId, "{0}, Initial send telemetry delay of {1} at a frequency of every {2} minutes.", this.telemetryIdentifier, telemetryScheduler.SendDelay, telemetryScheduler.SendInterval.TotalMinutes);
            this.telemetrySendTimer = new System.Threading.Timer(this.SendTelemetry, null, telemetryScheduler.SendDelay, telemetryScheduler.SendInterval);
        }
        private static bool GetPhysicallyInstalledSystemMemory(out long totalMemoryInKilobytes)
        {
            int pageSize = EnvironmentTelemetry.getpagesize();

            long physPages = EnvironmentTelemetry.get_phys_pages();

            totalMemoryInKilobytes = physPages * pageSize / 1024;

            return(true);
        }
        public EnvironmentTelemetry(
            string clusterType,
            string serviceFabricVersion,
            string clusterDevType)
        {
            // getting hardware information
            int  processorCount;
            long physicalMemoryInKB;

            EnvironmentTelemetry.GetSystemInfo(out processorCount, out physicalMemoryInKB);

            // getting disk and drive info
            string disks  = "[]";
            string drives = "[]";

            try
            {
#if !DotNetCoreClrLinux && !DotNetCoreClrIOT
                disks  = JsonConvert.SerializeObject(DiskInfoTelemetry.GetAllDisks());
                drives = JsonConvert.SerializeObject(DriveInfoTelemetry.GetAllDrives());
#endif
            }
            catch (Exception e)
            {
                // catching any exception for safety in case it fails for some reason
                // otherwise it will crash DCA everytime and prevent other telemetry to be sent
                EnvironmentTelemetry.traceSource.WriteError(LogSourceId, "Failed trying to get information about disks and drives. {0}", e);
            }

            // getting OS information
            string operatingSystem;
            string operatingSystemVersion;
            EnvironmentTelemetry.GetOperatingSystemInfo(out operatingSystem, out operatingSystemVersion);

            // populating the properties list with arguments
            this.Properties = new Dictionary <string, string>();
            this.Properties.Add(ClusterTypeStr, clusterType);
            this.Properties.Add(ServiceFabricVersionStr, serviceFabricVersion);
            this.Properties.Add(ClusterDevTypeStr, clusterDevType);
            this.Properties.Add(DisksStr, disks);
            this.Properties.Add(DrivesStr, drives);
            this.Properties.Add(OperatingSystemStr, operatingSystem);
            this.Properties.Add(OperatingSystemVersionStr, operatingSystemVersion);

            // populating the metrics list with arguments
            this.Metrics = new Dictionary <string, double>();
            this.Metrics.Add(ProcessorCountStr, (double)processorCount);
            this.Metrics.Add(PhysicalMemoryInKBStr, (double)physicalMemoryInKB);

            this.TraceAggregationCfg = TraceAggregationConfig.CreateSnapshotTraceAggregatorConfig(TaskNameStr, EventNameStr, string.Empty, this.Properties.Keys, this.Metrics.Keys);
        }
 private static void GetSystemInfo(out int processorCount, out long physicalMemoryInKB)
 {
     // processor count for windows and linux
     processorCount = Environment.ProcessorCount;
     try
     {
         // getting physical memory from kernel32.dll native method
         if (!EnvironmentTelemetry.GetPhysicallyInstalledSystemMemory(out physicalMemoryInKB))
         {
             physicalMemoryInKB = -1;
         }
     }
     catch (Exception e)
     {
         EnvironmentTelemetry.traceSource.WriteExceptionAsError(
             LogSourceId,
             e,
             "Failed to read amount of physical memory");
         physicalMemoryInKB = -1;
     }
 }