public GooglePubSubGapMeasure(PubSubSubscriptionOptions options) { _subscriptionId = options.SubscriptionId; var emulationEnabled = !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST")); _monitoringEnabled = !emulationEnabled && options.EnableMonitoring; if (_monitoringEnabled) { _metricClient = MetricServiceClient.Create(); } _undeliveredCountRequest = GetFilteredRequest(PubSubMetricUndeliveredMessagesCount); _oldestAgeRequest = GetFilteredRequest(PubSubMetricOldestUnackedMessageAge); ListTimeSeriesRequest GetFilteredRequest(string metric) => new() { Name = $"projects/{options.ProjectId}", Filter = $"metric.type = \"pubsub.googleapis.com/subscription/{metric}\" " + $"AND resource.label.subscription_id = \"{options.SubscriptionId}\"" }; }
// [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); }
/// <summary>Snippet for ListTimeSeriesAsync</summary> public async Task ListTimeSeriesAsync_RequestObject() { // Snippet: ListTimeSeriesAsync(ListTimeSeriesRequest,CallSettings) // Create client MetricServiceClient metricServiceClient = await MetricServiceClient.CreateAsync(); // Initialize request argument(s) ListTimeSeriesRequest request = new ListTimeSeriesRequest { ProjectName = new ProjectName("[PROJECT]"), Filter = "", Interval = new TimeInterval(), View = ListTimeSeriesRequest.Types.TimeSeriesView.Full, }; // Make the request PagedAsyncEnumerable <ListTimeSeriesResponse, TimeSeries> response = metricServiceClient.ListTimeSeriesAsync(request); // Iterate over all response items, lazily performing RPCs as required await response.ForEachAsync((TimeSeries item) => { // Do something with each item Console.WriteLine(item); }); // Or iterate over pages (of server-defined size), performing one RPC per page await response.AsRawResponses().ForEachAsync((ListTimeSeriesResponse page) => { // 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 = await response.ReadPageAsync(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 }
public double ReadBucketSize(string bucketName) { // Initialize request argument(s) string metricType = "storage.googleapis.com/storage/total_bytes"; string filter = $"metric.type=\"{metricType}\""; filter += $" AND resource.labels.bucket_name =\"{bucketName}\""; ListTimeSeriesRequest request = new ListTimeSeriesRequest { ProjectName = new ProjectName(Config.ProjectId), Filter = filter, Interval = new TimeInterval(), View = ListTimeSeriesRequest.Types.TimeSeriesView.Full, }; // 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 8*60 minutes. // Gcs bucket size will be caculated every 24 hours. startTimeStamp.Seconds = timeStamp - (1 * 60 * 60); 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 = this.metric.ListTimeSeries(request); // Iterate over all response items, lazily performing RPCs as required. if (response.Count() == 0) { throw new Exception(string.Format("failed to get bucket: {0} size", bucketName)); } TimeSeries item = response.First(); var points = item.Points; double size = points[0].Value.DoubleValue; return(size); }
public Monitoring() { _metricClient = MetricServiceClient.Create(); const string subId = "test-7ffc718448eb41478252677b6889332f"; _undeliveredCountRequest = new ListTimeSeriesRequest { Name = $"projects/{PubSubFixture.ProjectId}", Filter = "metric.type = \"pubsub.googleapis.com/subscription/num_undelivered_messages\" " + $"AND resource.label.subscription_id = \"{subId}\"" }; _oldestAgeRequest = new ListTimeSeriesRequest { Name = $"projects/{PubSubFixture.ProjectId}", Filter = "metric.type = \"pubsub.googleapis.com/subscription/oldest_unacked_message_age\" " + $"AND resource.label.subscription_id = \"{subId}\"" }; }
// [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); }