// [START monitoring_create_metric] public static object CreateMetric(string projectId, string metricType = "custom.googleapis.com/stores/daily_sales") { // Create client. MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Prepare custom metric descriptor. MetricDescriptor metricDescriptor = new MetricDescriptor(); metricDescriptor.DisplayName = "Daily Sales"; metricDescriptor.Description = "Daily sales records from all branch stores."; metricDescriptor.MetricKind = MetricKind.Gauge; metricDescriptor.ValueType = MetricDescriptor.Types.ValueType.Double; metricDescriptor.Type = metricType; metricDescriptor.Unit = "{USD}"; LabelDescriptor labels = new LabelDescriptor(); labels.Key = "store_id"; labels.ValueType = LabelDescriptor.Types.ValueType.String; labels.Description = "The ID of the store."; metricDescriptor.Labels.Add(labels); CreateMetricDescriptorRequest request = new CreateMetricDescriptorRequest { ProjectName = new ProjectName(projectId), }; request.MetricDescriptor = metricDescriptor; // Make the request. MetricDescriptor response = metricServiceClient.CreateMetricDescriptor(request); Console.WriteLine("Done creating metric descriptor:"); Console.WriteLine(JObject.Parse($"{response}").ToString()); return(0); }
public static int Main(string[] args) { // Read projectId from args if (args.Length != 1) { Console.WriteLine("Usage: Project ID must be passed as first argument."); Console.WriteLine(); return(1); } string projectId = args[0]; // Create client MetricServiceClient client = MetricServiceClient.Create(); // Initialize request argument(s) ProjectName name = ProjectName.FromProject(projectId); // Call API method var pagedResponse = client.ListMonitoredResourceDescriptors(name); // Show the result foreach (var item in pagedResponse) { Console.WriteLine(item); } // Success Console.WriteLine("Smoke test passed OK"); return(0); }
public void ListMonitoredResourceDescriptors() { // Snippet: ListMonitoredResourceDescriptors(string,string,int?,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatProjectName("[PROJECT]"); // Make the request IPagedEnumerable <ListMonitoredResourceDescriptorsResponse, MonitoredResourceDescriptor> response = metricServiceClient.ListMonitoredResourceDescriptors(formattedName); // Iterate over all response items, lazily performing RPCs as required foreach (MonitoredResourceDescriptor 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 <MonitoredResourceDescriptor> page in response.AsPages().WithFixedSize(pageSize)) { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (MonitoredResourceDescriptor item in page) { Console.WriteLine(item); } } // End snippet }
public async Task ListMetricDescriptorsAsync() { // Snippet: ListMetricDescriptorsAsync(string,string,int?,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatProjectName("[PROJECT]"); // Make the request IPagedAsyncEnumerable <ListMetricDescriptorsResponse, MetricDescriptor> response = metricServiceClient.ListMetricDescriptorsAsync(formattedName); // Iterate over all response items, lazily performing RPCs as required await response.ForEachAsync((MetricDescriptor item) => { // Do something with each item Console.WriteLine(item); }); // Or iterate over fixed-sized pages, lazily performing RPCs as required int pageSize = 10; IAsyncEnumerable <FixedSizePage <MetricDescriptor> > fixedSizePages = response.AsPages().WithFixedSize(pageSize); await fixedSizePages.ForEachAsync((FixedSizePage <MetricDescriptor> page) => { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (MetricDescriptor item in page) { Console.WriteLine(item); } }); // End snippet }
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); }
// [END monitoring_list_resources] // [START monitoring_get_resource] public static object GetMonitoredResource(string projectId, string resourceId) { MetricServiceClient client = MetricServiceClient.Create(); MonitoredResourceDescriptorName name = new MonitoredResourceDescriptorName(projectId, resourceId); var response = client.GetMonitoredResourceDescriptor(name); string resource = JObject.Parse($"{response}").ToString(); Console.WriteLine($"{ resource }"); return(0); }
public static void Main(string[] args) { // Your Google Cloud Platform project ID. string projectId = "YOUR-PROJECT-ID"; // Create client. MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s). ProjectName name = new ProjectName(projectId); // Prepare a data point. Point dataPoint = new Point(); TypedValue salesTotal = new TypedValue(); salesTotal.DoubleValue = 123.45; dataPoint.Value = salesTotal; // Sets data point's interval end time to current time. Timestamp timeStamp = new Timestamp(); DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); timeStamp.Seconds = (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds; TimeInterval interval = new TimeInterval(); interval.EndTime = timeStamp; dataPoint.Interval = interval; // Prepare custom metric. Metric metric = new Metric(); metric.Type = "custom.googleapis.com/stores/daily_sales"; metric.Labels.Add("store_id", "Pittsburgh"); // Prepare monitored resource. MonitoredResource resource = new MonitoredResource(); resource.Type = "global"; resource.Labels.Add("project_id", projectId); // Create a new time series using inputs. TimeSeries timeSeriesData = new TimeSeries(); timeSeriesData.Metric = metric; timeSeriesData.Resource = resource; timeSeriesData.Points.Add(dataPoint); // Add newly created time series to list of time series to be written. IEnumerable <TimeSeries> timeSeries = new List <TimeSeries> { timeSeriesData }; // Write time series data. metricServiceClient.CreateTimeSeries(name, timeSeries); Console.WriteLine("Done writing time series data."); }
/// <summary>Snippet for GetMonitoredResourceDescriptor</summary> public void GetMonitoredResourceDescriptor() { // Snippet: GetMonitoredResourceDescriptor(MonitoredResourceDescriptorName,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) MonitoredResourceDescriptorName name = new MonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]"); // Make the request MonitoredResourceDescriptor response = metricServiceClient.GetMonitoredResourceDescriptor(name); // End snippet }
/// <summary>Snippet for GetMetricDescriptor</summary> public void GetMetricDescriptor() { // Snippet: GetMetricDescriptor(MetricDescriptorName,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) MetricDescriptorName name = new MetricDescriptorName("[PROJECT]", "[METRIC_DESCRIPTOR]"); // Make the request MetricDescriptor response = metricServiceClient.GetMetricDescriptor(name); // End snippet }
public void DeleteMetricDescriptor() { // Snippet: DeleteMetricDescriptor(string,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatMetricDescriptorName("[PROJECT]", "[METRIC_DESCRIPTOR]"); // Make the request metricServiceClient.DeleteMetricDescriptor(formattedName); // End snippet }
// [END monitoring_create_metric] // [START monitoring_delete_metric] public static object DeleteMetric(string projectId, string metricType) { // Create client. MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s). MetricDescriptorName name = new MetricDescriptorName(projectId, metricType); // Make the request. metricServiceClient.DeleteMetricDescriptor(name); Console.WriteLine($"Done deleting metric descriptor: {name}"); return(0); }
/// <summary>Snippet for CreateMetricDescriptor</summary> public void CreateMetricDescriptor() { // Snippet: CreateMetricDescriptor(ProjectName,MetricDescriptor,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) ProjectName name = new ProjectName("[PROJECT]"); MetricDescriptor metricDescriptor = new MetricDescriptor(); // Make the request MetricDescriptor response = metricServiceClient.CreateMetricDescriptor(name, metricDescriptor); // End snippet }
// [END monitoring_delete_metric] // [START monitoring_list_descriptors] public static object ListMetrics(string projectId) { MetricServiceClient client = MetricServiceClient.Create(); ProjectName projectName = new ProjectName(projectId); PagedEnumerable <ListMetricDescriptorsResponse, MetricDescriptor> metrics = client.ListMetricDescriptors(projectName); foreach (MetricDescriptor metric in metrics) { Console.WriteLine($"{metric.Name}: {metric.DisplayName}"); } return(0); }
public async Task DeleteMetricDescriptorAsync() { // Snippet: DeleteMetricDescriptorAsync(string,CallSettings) // Additional: DeleteMetricDescriptorAsync(string,CancellationToken) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatMetricDescriptorName("[PROJECT]", "[METRIC_DESCRIPTOR]"); // Make the request await metricServiceClient.DeleteMetricDescriptorAsync(formattedName); // End snippet }
/// <summary>Snippet for CreateTimeSeries</summary> public void CreateTimeSeries() { // Snippet: CreateTimeSeries(ProjectName,IEnumerable<TimeSeries>,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) ProjectName name = new ProjectName("[PROJECT]"); IEnumerable <TimeSeries> timeSeries = new List <TimeSeries>(); // Make the request metricServiceClient.CreateTimeSeries(name, timeSeries); // End snippet }
public async Task GetMonitoredResourceDescriptorAsync() { // Snippet: GetMonitoredResourceDescriptorAsync(string,CallSettings) // Additional: GetMonitoredResourceDescriptorAsync(string,CancellationToken) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatMonitoredResourceDescriptorName("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]"); // Make the request MonitoredResourceDescriptor response = await metricServiceClient.GetMonitoredResourceDescriptorAsync(formattedName); // End snippet }
/// <summary>Snippet for GetMetricDescriptor</summary> public void GetMetricDescriptor_RequestObject() { // Snippet: GetMetricDescriptor(GetMetricDescriptorRequest,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) GetMetricDescriptorRequest request = new GetMetricDescriptorRequest { MetricDescriptorName = new MetricDescriptorName("[PROJECT]", "[METRIC_DESCRIPTOR]"), }; // Make the request MetricDescriptor response = metricServiceClient.GetMetricDescriptor(request); // End snippet }
public async Task CreateTimeSeriesAsync() { // Snippet: CreateTimeSeriesAsync(string,IEnumerable<TimeSeries>,CallSettings) // Additional: CreateTimeSeriesAsync(string,IEnumerable<TimeSeries>,CancellationToken) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatProjectName("[PROJECT]"); IEnumerable <TimeSeries> timeSeries = new List <TimeSeries>(); // Make the request await metricServiceClient.CreateTimeSeriesAsync(formattedName, timeSeries); // End snippet }
// [END monitoring_get_descriptor] // [START monitoring_write_timeseries] public static object WriteTimeSeriesData(string projectId) { // Create client. MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s). ProjectName name = new ProjectName(projectId); // Prepare a data point. Point dataPoint = new Point(); TypedValue salesTotal = new TypedValue(); salesTotal.DoubleValue = 123.45; dataPoint.Value = salesTotal; Timestamp timeStamp = new Timestamp(); timeStamp.Seconds = (long)(DateTime.UtcNow - s_unixEpoch).TotalSeconds; TimeInterval interval = new TimeInterval(); interval.EndTime = timeStamp; dataPoint.Interval = interval; // Prepare custom metric. Metric metric = new Metric(); metric.Type = "custom.googleapis.com/stores/daily_sales"; metric.Labels.Add("store_id", "Pittsburgh"); // Prepare monitored resource. MonitoredResource resource = new MonitoredResource(); resource.Type = "global"; resource.Labels.Add("project_id", projectId); // Create a new time series using inputs. TimeSeries timeSeriesData = new TimeSeries(); timeSeriesData.Metric = metric; timeSeriesData.Resource = resource; timeSeriesData.Points.Add(dataPoint); // Add newly created time series to list of time series to be written. IEnumerable <TimeSeries> timeSeries = new List <TimeSeries> { timeSeriesData }; // Write time series data. metricServiceClient.CreateTimeSeries(name, timeSeries); Console.WriteLine("Done writing time series data:"); Console.WriteLine(JObject.Parse($"{timeSeriesData}").ToString()); return(0); }
public CloudMonitoringServices(IConfiguration configuration) { this.configuration = configuration; var helper = new CloudMonitoringHelper(configuration); googleCredential = helper.GetGoogleCredential(); channel = new Channel( MetricServiceClient.DefaultEndpoint.Host, MetricServiceClient.DefaultEndpoint.Port, googleCredential.ToChannelCredentials() ); client = MetricServiceClient.Create(channel); projectId = helper.GetProjectId(); }
public async Task CreateMetricDescriptorAsync() { // Snippet: CreateMetricDescriptorAsync(string,MetricDescriptor,CallSettings) // Additional: CreateMetricDescriptorAsync(string,MetricDescriptor,CancellationToken) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) string formattedName = MetricServiceClient.FormatProjectName("[PROJECT]"); MetricDescriptor metricDescriptor = new MetricDescriptor(); // Make the request MetricDescriptor response = await metricServiceClient.CreateMetricDescriptorAsync(formattedName, metricDescriptor); // End snippet }
/// <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 }
/// <summary>Snippet for CreateMetricDescriptor</summary> public void CreateMetricDescriptor_RequestObject() { // Snippet: CreateMetricDescriptor(CreateMetricDescriptorRequest,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) CreateMetricDescriptorRequest request = new CreateMetricDescriptorRequest { ProjectName = new ProjectName("[PROJECT]"), MetricDescriptor = new MetricDescriptor(), }; // Make the request MetricDescriptor response = metricServiceClient.CreateMetricDescriptor(request); // End snippet }
public void ListMetricDescriptors() { string projectId = _fixture.ProjectId; // Snippet: ListMetricDescriptors MetricServiceClient client = MetricServiceClient.Create(); string projectName = MetricServiceClient.FormatProjectName(projectId); string filter = ""; IPagedEnumerable <ListMetricDescriptorsResponse, MetricDescriptor> metrics = client.ListMetricDescriptors(projectName, filter); foreach (MetricDescriptor metric in metrics.Take(10)) { Console.WriteLine($"{metric.Name}: {metric.DisplayName}"); } // End snippet }
public void ListMetricDescriptors() { string projectId = _fixture.ProjectId; // Sample: ListMetricDescriptors // Additional: ListMetricDescriptors(*,*,*,*) MetricServiceClient client = MetricServiceClient.Create(); ProjectName projectName = new ProjectName(projectId); PagedEnumerable <ListMetricDescriptorsResponse, MetricDescriptor> metrics = client.ListMetricDescriptors(projectName); foreach (MetricDescriptor metric in metrics.Take(10)) { Console.WriteLine($"{metric.Name}: {metric.DisplayName}"); } // End sample }
/// <summary>Snippet for CreateTimeSeries</summary> public void CreateTimeSeries_RequestObject() { // Snippet: CreateTimeSeries(CreateTimeSeriesRequest,CallSettings) // Create client MetricServiceClient metricServiceClient = MetricServiceClient.Create(); // Initialize request argument(s) CreateTimeSeriesRequest request = new CreateTimeSeriesRequest { ProjectName = new ProjectName("[PROJECT]"), TimeSeries = { }, }; // Make the request metricServiceClient.CreateTimeSeries(request); // End snippet }
// [END monitoring_list_descriptors] // [START monitoring_get_descriptor] public static object GetMetricDetails(string projectId, string metricType) { MetricServiceClient client = MetricServiceClient.Create(); MetricDescriptorName name = new MetricDescriptorName(projectId, metricType); try { var response = client.GetMetricDescriptor(name); string metric = JObject.Parse($"{response}").ToString(); Console.WriteLine($"{ metric }"); } catch (Grpc.Core.RpcException ex) when(ex.Status.StatusCode == Grpc.Core.StatusCode.NotFound) { } return(0); }
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}\"" }; }
public MetricClient() { _googleProjectId = Environment.GetEnvironmentVariable(_googleProjectEnvironmentVariableName); if (String.IsNullOrWhiteSpace(_googleProjectId)) { throw new ArgumentNullException($"Environment variable was not found: \"{_googleProjectEnvironmentVariableName}\"!"); } var googleApplicationCredentialsPath = Environment.GetEnvironmentVariable(_googleApplicationCredentialsEnvironmentVariableName); if (String.IsNullOrWhiteSpace(googleApplicationCredentialsPath)) { throw new ArgumentNullException($"Environment variable was not found: \"{_googleApplicationCredentialsEnvironmentVariableName}\"!"); } _projectName = new ProjectName(_googleProjectId); _client = MetricServiceClient.Create(); }