The MetricDatum data type encapsulates the information sent with PutMetricData to either create a new metric or add new values to be aggregated into an existing metric.

        public void TrackEvent(string eventName, TimeSpan elapsed, IDictionary <string, string> properties = null)
        {
            var dimension = new Amazon.CloudWatch.Model.Dimension
            {
                Name  = eventName,
                Value = eventName
            };

            var metric1 = new Amazon.CloudWatch.Model.MetricDatum
            {
                Dimensions = new List <Amazon.CloudWatch.Model.Dimension> {
                    dimension
                },
                MetricName      = eventName,
                StatisticValues = new Amazon.CloudWatch.Model.StatisticSet(),
                Timestamp       = DateTime.Today,
                Unit            = StandardUnit.Count
            };

            if (properties?.ContainsKey("value") == true)
            {
                metric1.Value = long.Parse(properties["value"]);
            }
            else
            {
                metric1.Value = 1;
            }

            var request = new Amazon.CloudWatch.Model.PutMetricDataRequest
            {
                MetricData = new List <Amazon.CloudWatch.Model.MetricDatum>()
                {
                    metric1
                },
                Namespace = eventName
            };

            _telemetryClient.PutMetricDataAsync(request).Wait();
        }
Пример #2
0
        /// <summary>
        /// Reports the metric that is defined by <paramref name="state"/> to the Amazon CloudWatch service.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="timeout">if set to <c>true</c> a timeout occured.</param>
        private void ReportMetric(object state, bool timeout)
        {
            var metric = state as Metric;
            if (timeout)
            {
                //
                // Retrieve current value of metric
                //
                MetricDatum datum = null;
                if (metric is PerformanceCounterMetric)
                {
                    var description = ((PerformanceCounterMetric)metric).PerformanceCounter;
                    if (!PerformanceCounters.ContainsKey(description))
                    {
                        LoadPerformanceCounter(description);
                    }

                    var unitConverter = TypeDescriptor.GetConverter(typeof(MetricUnit));

                    var counter = PerformanceCounters[description];
                    datum = new MetricDatum().WithMetricName(metric.Name)
                                             .WithTimestamp(DateTime.Now)
                                             .WithUnit((string)unitConverter.ConvertTo(metric.Unit, typeof(string)))
                                             .WithValue(counter.NextValue());
                }

                //
                // Mark datum with Instance ID and Availability Zone
                //
                if (datum != null)
                {
                    if (!string.IsNullOrEmpty(InstanceId))
                    {
                        datum.Dimensions.Add(
                            new Dimension().WithName("InstanceId").WithValue(InstanceId)
                        );
                    }

                    if (!string.IsNullOrEmpty(AvailabilityZone))
                    {
                        datum.Dimensions.Add(
                            new Dimension().WithName("Region").WithValue(AvailabilityZone)
                        );
                    }
                }

                //
                // Report value to CloudWatch service
                //
                if (datum != null)
                {
                    IList<MetricDatum> data = new List<MetricDatum>();
                    data.Add(datum);
                    lock (awsClientSyncRoot)
                    {
                        try
                        {
                            logger.Info("Reporting Metric '{0}' to CloudWatch", datum.MetricName);
                            AwsClient.PutMetricData(new PutMetricDataRequest()
                                .WithMetricData(data)
                                .WithNamespace("System/Windows")
                            );
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    }
                }

                //
                // Register for next report interval
                //
                RegisterMetricForProcessing(metric);
            }
        }
        private void SetupClient()
        {
            if (_client != null)
                return;

            AmazonCloudWatchConfig cloudWatchConfig = null;
            RegionEndpoint regionEndpoint = null;

            if (string.IsNullOrEmpty(EndPoint) && ConfigurationManager.AppSettings["AWSServiceEndpoint"] != null)
                EndPoint = ConfigurationManager.AppSettings["AWSServiceEndpoint"];

            if (string.IsNullOrEmpty(AccessKey) && ConfigurationManager.AppSettings["AWSAccessKey"] != null)
                AccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];

            if (string.IsNullOrEmpty(Secret) && ConfigurationManager.AppSettings["AWSSecretKey"] != null)
                Secret = ConfigurationManager.AppSettings["AWSSecretKey"];

            _client = AWSClientFactory.CreateAmazonCloudWatchClient(AccessKey, Secret);

            try
            {

                if (!string.IsNullOrEmpty(EndPoint))
                {
                    if (EndPoint.StartsWith("http"))
                    {
                        cloudWatchConfig = new AmazonCloudWatchConfig { ServiceURL = EndPoint };
                        if (string.IsNullOrEmpty(AccessKey))
                            _client = AWSClientFactory.CreateAmazonCloudWatchClient(cloudWatchConfig);
                    }
                    else
                    {
                        regionEndpoint = RegionEndpoint.GetBySystemName(EndPoint);
                        if (string.IsNullOrEmpty(AccessKey))
                            _client = AWSClientFactory.CreateAmazonCloudWatchClient(regionEndpoint);
                    }
                }
            }
            catch (AmazonServiceException)
            {
            }

            if (!string.IsNullOrEmpty(AccessKey))
                if (regionEndpoint != null)
                    _client = AWSClientFactory.CreateAmazonCloudWatchClient(AccessKey, Secret, regionEndpoint);
                else if (cloudWatchConfig != null)
                    _client = AWSClientFactory.CreateAmazonCloudWatchClient(AccessKey, Secret, cloudWatchConfig);
                else
                    _client = AWSClientFactory.CreateAmazonCloudWatchClient(AccessKey, Secret);

            //Debug
            var metricDatum = new Amazon.CloudWatch.Model.MetricDatum().WithMetricName("CloudWatchAppender").WithValue(1).WithUnit("Count");
            //_client.PutMetricData(new PutMetricDataRequest().WithNamespace("CloudWatchAppender").WithMetricData(metricDatum));
        }
Пример #4
-1
    public static void CWPutMetricData()
    {
      #region CWPutMetricData
      var client = new AmazonCloudWatchClient();

      var dimension = new Dimension
      {
        Name = "Desktop Machine Metrics",
        Value = "Virtual Desktop Machine Usage"
      };

      var metric1 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Online",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 14
      };

      var metric2 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Offline",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 7
      };

      var metric3 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Online",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 12
      };

      var metric4 = new MetricDatum
      {
        Dimensions = new List<Dimension>(),
        MetricName = "Desktop Machines Offline",
        StatisticValues = new StatisticSet(),
        Timestamp = DateTime.Today,
        Unit = StandardUnit.Count,
        Value = 9
      };

      var request = new PutMetricDataRequest
      {
        MetricData = new List<MetricDatum>() { metric1, metric2, 
          metric3, metric4 },
        Namespace = "Example.com Custom Metrics"
      };

      client.PutMetricData(request);
      #endregion
    }