Beispiel #1
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            GetMetricDataResponse response = new GetMetricDataResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("GetMetricDataResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonCloudWatchConfig config = new AmazonCloudWatchConfig();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonCloudWatchClient client = new AmazonCloudWatchClient(creds, config);

            GetMetricDataResponse resp = new GetMetricDataResponse();

            do
            {
                GetMetricDataRequest req = new GetMetricDataRequest
                {
                    NextToken = resp.NextToken
                    ,
                    MaxDatapoints = maxItems
                };

                resp = client.GetMetricData(req);
                CheckError(resp.HttpStatusCode, "200");

                foreach (var obj in resp.Messages)
                {
                    AddObject(obj);
                }

                foreach (var obj in resp.MetricDataResults)
                {
                    AddObject(obj);
                }
            }while (!string.IsNullOrEmpty(resp.NextToken));
        }
Beispiel #3
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            GetMetricDataResponse response = new GetMetricDataResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("MetricResults", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <HistoricalMetricResult, HistoricalMetricResultUnmarshaller>(HistoricalMetricResultUnmarshaller.Instance);
                    response.MetricResults = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("NextToken", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.NextToken = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
Beispiel #4
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetMetricDataResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("MetricDataResults/member", targetDepth))
                    {
                        var unmarshaller = MetricDataResultUnmarshaller.Instance;
                        var item         = unmarshaller.Unmarshall(context);
                        response.MetricDataResults.Add(item);
                        continue;
                    }
                    if (context.TestExpression("NextToken", targetDepth))
                    {
                        var unmarshaller = StringUnmarshaller.Instance;
                        response.NextToken = unmarshaller.Unmarshall(context);
                        continue;
                    }
                }
            }

            return;
        }
Beispiel #5
0
        private static double GetMetricCount(GetMetricDataResponse resp)
        {
            double result = 0;

            if (resp.MetricDataResults.Any())
            {
                var values = resp.MetricDataResults[0].Values;
                if (values.Any())
                {
                    result = values[0];
                }
            }
            return(result);
        }
Beispiel #6
0
        private static async Task ReadMetrics(Program program, int noOfDays = DEFAULT_NUMBER_OF_DAYS_TO_READ)
        {
            Console.WriteLine(">>>> Reading metrics");

            List <(string, string)> metrics = new List <(string, string)> {
                ("metricRequestMetricRead", METRIC_METRIC_READ),
                ("metricRequestClientBlocked", METRIC_CLIENT_BLOCKED),
                ("metricRequestReportRequestCancelledAll", METRIC_REPORT_REQUEST_CANCELLED_ALL),
                ("metricRequestReportRequestCancelledDailyLimit", METRIC_REPORT_REQUEST_CANCELLED_DAILY_LIMIT),
                ("metricRequestReportRequestCancelledErrorRate", METRIC_REPORT_REQUEST_CANCELLED_ERROR_RATE),
                ("metricRequestReportRequestAvoided", METRIC_REPORT_REQUEST_AVOIDED),
                ("metricRequestReportRequest", METRIC_REPORT_REQUEST),
                ("metricRequestSegmentWritten", METRIC_SEGMENT_WRITTEN),
                ("metricRequest400", METRIC_HTTP_400),
                ("metricRequest401", METRIC_HTTP_401),
                ("metricRequest403", METRIC_HTTP_403),
                ("metricRequest429FailedRequestsTooHigh", METRIC_HTTP_429_FAILED_REQUESTS_TOO_HIGH),
                ("metricRequest429Other", METRIC_HTTP_429_OTHER),
                ("metricRequest429RequestsPerDay", METRIC_HTTP_429_REQUESTS_PER_DAY),
                ("metricRequest429RequestsPerUserPer100s", METRIC_HTTP_429_REQUESTS_PER_USER_PER_100s),
                ("metricRequest429RequestsPerViewPerDay", METRIC_HTTP_429_REQUESTS_PER_VIEW_PER_DAY),
                ("metricRequest429TooManyConcurrentConnections", METRIC_HTTP_429_TOO_MANY_CONCURRENT_CONNECTIONS),
                ("metricRequest500", METRIC_HTTP_500),
                ("metricRequest502", METRIC_HTTP_502),
                ("metricRequest503Other", METRIC_HTTP_503_OTHER),
                ("metricRequest503Unavailable", METRIC_HTTP_503_UNAVAILABLE),
                ("metricRequestOther", METRIC_HTTP_OTHER)
            };

            TimeZoneInfo?pst = GetTimeZoneInfo(PACIFIC_STANDARD_TIME);

            if (pst != null)
            {
                Console.WriteLine($"TimeZone: {pst}");
                Console.WriteLine($"REPORT_REQUEST_QUOTA_OFFSET_MINUTES: {REPORT_REQUEST_QUOTA_OFFSET_MINUTES}");
                var currentDateInTimeZone = CurrentDateInTimeZone(pst);
                List <(string startTime, List <double> metrics)> metricsByDay = new List <(string, List <double>)>();
                for (int i = 0; i < noOfDays; i++)
                {
                    DateTime currentDateInPST = currentDateInTimeZone.AddDays(-i).AddMinutes(REPORT_REQUEST_QUOTA_OFFSET_MINUTES);
                    DateTime nextDateInPST    = currentDateInPST.AddDays(1).AddSeconds(-1);
                    DateTime startTimeUTC     = ConvertDateToUTC(currentDateInPST, pst);
                    DateTime endTimeUTC       = ConvertDateToUTC(nextDateInPST, pst);

                    Console.WriteLine();
                    Console.WriteLine($"Getting metrics from [{startTimeUTC}] to [{endTimeUTC}]");
                    Console.WriteLine();

                    List <double> metricsForDay = new List <double>();
                    foreach ((string metricQueryId, string metricName) in metrics)
                    {
                        // TODO - Inefficient to send a single metric request per metric
                        GetMetricDataResponse resp = await program.GetMetricsAsync(startTimeUTC, endTimeUTC, metricQueryId, metricName);

                        double metricCount = GetMetricCount(resp);
                        Console.WriteLine($"Metric [{metricName}] count is [{metricCount}]");
                        metricsForDay.Add(metricCount);
                    }
                    metricsByDay.Add((startTimeUTC.ToShortDateString(), metricsForDay));
                }
                Console.WriteLine();
                Console.WriteLine("Metrics in CSV format (in same date order):");
                foreach (var item in metricsByDay)
                {
                    Console.WriteLine($"{item.startTime},{String.Join(",", item.metrics)}");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Oops, no timezone");
            }
        }