// [END monitoring_read_timeseries_fields]

        // [START monitoring_read_timeseries_align]
        public static object ReadTimeSeriesAggregate(string projectId,
                                                     string metricType = "compute.googleapis.com/instance/cpu/utilization")
        {
            // Create client.
            MetricServiceClient metricServiceClient = MetricServiceClient.Create();
            // Initialize request argument(s).
            string filter = $"metric.type=\"{metricType}\"";
            ListTimeSeriesRequest request = new ListTimeSeriesRequest
            {
                ProjectName = new ProjectName(projectId),
                Filter      = filter,
                Interval    = new TimeInterval(),
            };
            // Create timestamp for current time formatted in seconds.
            long      timeStamp      = (long)(DateTime.UtcNow - s_unixEpoch).TotalSeconds;
            Timestamp startTimeStamp = new Timestamp();

            // Set startTime to limit results to the last 20 minutes.
            startTimeStamp.Seconds = timeStamp - (60 * 20);
            Timestamp endTimeStamp = new Timestamp();

            // Set endTime to current time.
            endTimeStamp.Seconds = timeStamp;
            TimeInterval interval = new TimeInterval();

            interval.StartTime = startTimeStamp;
            interval.EndTime   = endTimeStamp;
            request.Interval   = interval;
            // Aggregate results per matching instance
            Aggregation aggregation     = new Aggregation();
            Duration    alignmentPeriod = new Duration();

            alignmentPeriod.Seconds      = 600;
            aggregation.AlignmentPeriod  = alignmentPeriod;
            aggregation.PerSeriesAligner = Aggregation.Types.Aligner.AlignMean;
            // Add the aggregation to the request.
            request.Aggregation = aggregation;
            // Make the request.
            PagedEnumerable <ListTimeSeriesResponse, TimeSeries> response =
                metricServiceClient.ListTimeSeries(request);

            // Iterate over all response items, lazily performing RPCs as required.
            Console.WriteLine($"{projectId} CPU utilization:");
            foreach (var item in response)
            {
                var points = item.Points;
                var labels = item.Metric.Labels;
                Console.WriteLine($"{labels.Values.FirstOrDefault()}");
                if (points.Count > 0)
                {
                    Console.WriteLine($"  Now: {points[0].Value.DoubleValue}");
                }
                if (points.Count > 1)
                {
                    Console.WriteLine($"  10 min ago: {points[1].Value.DoubleValue}");
                }
            }
            return(0);
        }
Ejemplo n.º 2
0
        /// <summary>Snippet for ListTimeSeries</summary>
        public void ListTimeSeries_RequestObject()
        {
            // Snippet: ListTimeSeries(ListTimeSeriesRequest,CallSettings)
            // Create client
            MetricServiceClient metricServiceClient = MetricServiceClient.Create();
            // Initialize request argument(s)
            ListTimeSeriesRequest request = new ListTimeSeriesRequest
            {
                ProjectName = new ProjectName("[PROJECT]"),
                Filter      = "",
                Interval    = new TimeInterval(),
                View        = ListTimeSeriesRequest.Types.TimeSeriesView.Full,
            };
            // Make the request
            PagedEnumerable <ListTimeSeriesResponse, TimeSeries> response =
                metricServiceClient.ListTimeSeries(request);

            // Iterate over all response items, lazily performing RPCs as required
            foreach (TimeSeries item in response)
            {
                // Do something with each item
                Console.WriteLine(item);
            }

            // Or iterate over pages (of server-defined size), performing one RPC per page
            foreach (ListTimeSeriesResponse page in response.AsRawResponses())
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (TimeSeries item in page)
                {
                    Console.WriteLine(item);
                }
            }

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int pageSize = 10;
            Page <TimeSeries> singlePage = response.ReadPage(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (TimeSeries item in singlePage)
            {
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
Ejemplo n.º 3
0
        // [END monitoring_read_timeseries_simple]

        // [START monitoring_read_timeseries_fields]
        public static object ReadTimeSeriesFields(string projectId,
                                                  string metricType = "compute.googleapis.com/instance/cpu/utilization")
        {
            Console.WriteLine($"metricType{ metricType}");
            // Create client.
            MetricServiceClient metricServiceClient = MetricServiceClient.Create();
            // Initialize request argument(s).
            string filter = $"metric.type=\"{metricType}\"";
            ListTimeSeriesRequest request = new ListTimeSeriesRequest
            {
                ProjectName = new ProjectName(projectId),
                Filter      = filter,
                Interval    = new TimeInterval(),
                View        = ListTimeSeriesRequest.Types.TimeSeriesView.Headers,
            };
            // Create timestamp for current time formatted in seconds.
            long      timeStamp      = (long)(DateTime.UtcNow - s_unixEpoch).TotalSeconds;
            Timestamp startTimeStamp = new Timestamp();

            // Set startTime to limit results to the last 20 minutes.
            startTimeStamp.Seconds = timeStamp - (60 * 20);
            Timestamp endTimeStamp = new Timestamp();

            // Set endTime to current time.
            endTimeStamp.Seconds = timeStamp;
            TimeInterval interval = new TimeInterval();

            interval.StartTime = startTimeStamp;
            interval.EndTime   = endTimeStamp;
            request.Interval   = interval;
            // Make the request.
            PagedEnumerable <ListTimeSeriesResponse, TimeSeries> response =
                metricServiceClient.ListTimeSeries(request);

            // Iterate over all response items, lazily performing RPCs as required.
            Console.Write("Found data points for the following instances:");
            foreach (var item in response)
            {
                Console.WriteLine(JObject.Parse($"{item}").ToString());
            }
            return(0);
        }
Ejemplo n.º 4
0
        public void ListTimeSeries()
        {
            // Snippet: ListTimeSeries(string,string,TimeInterval,ListTimeSeriesRequest.Types.TimeSeriesView,string,int?,CallSettings)
            // Create client
            MetricServiceClient metricServiceClient = MetricServiceClient.Create();
            // Initialize request argument(s)
            string       formattedName = MetricServiceClient.FormatProjectName("[PROJECT]");
            string       filter        = "";
            TimeInterval interval      = new TimeInterval();

            ListTimeSeriesRequest.Types.TimeSeriesView view = ListTimeSeriesRequest.Types.TimeSeriesView.Full;
            // Make the request
            IPagedEnumerable <ListTimeSeriesResponse, TimeSeries> response =
                metricServiceClient.ListTimeSeries(formattedName, filter, interval, view);

            // Iterate over all response items, lazily performing RPCs as required
            foreach (TimeSeries item in response)
            {
                // Do something with each item
                Console.WriteLine(item);
            }

            // Or iterate over fixed-sized pages, lazily performing RPCs as required
            int pageSize = 10;

            foreach (FixedSizePage <TimeSeries> page in response.AsPages().WithFixedSize(pageSize))
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (TimeSeries item in page)
                {
                    Console.WriteLine(item);
                }
            }
            // End snippet
        }