/** * Returns the first page of all metric descriptors. */ void listMetricDescriptors() throws IOException { // [START monitoring_list_descriptors] // Your Google Cloud Platform project ID String projectId = System.getProperty("projectId"); final MetricServiceClient client = MetricServiceClient.create(); ProjectName name = ProjectName.of(projectId); ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest .newBuilder() .setName(name.toString()) .build(); ListMetricDescriptorsPagedResponse response = client.listMetricDescriptors(request); System.out.println("Listing descriptors: "); for (MetricDescriptor d : response.iterateAll()) { System.out.println(d.getName() + " " + d.getDisplayName()); } // [END monitoring_list_descriptors] }
private void validateMetrics(List<MetricDescriptor> metricsList) { // - Metrics list cannot be longer than predefined limit. validateMaxListSize( ResolvedLocation.create(SimpleLocation.TOPLEVEL), "metrics list", metricsList, bounds.getMaxMetrics()); // - Metric name must be unique across merged lists. for (MetricDescriptor metric : metricsList) { if (metrics.containsKey(metric.getName())) { error( MessageLocationContext.create(metric, MetricDescriptor.NAME_FIELD_NUMBER), "The '%s' metric is already defined. The metric name must be unique.", metric.getName()); } metrics.put(metric.getName(), false); validateMetric(metric); } }
private void validateMetrics(List<MetricDescriptor> metricsList) { // - Metrics list cannot be longer than predefined limit. validateMaxListSize( ResolvedLocation.create(SimpleLocation.TOPLEVEL), "metrics list", metricsList, bounds.getMaxMetrics()); // - Metric name must be unique across merged lists. for (MetricDescriptor metric : metricsList) { if (metrics.containsKey(metric.getName())) { error( MessageLocationContext.create(metric, MetricDescriptor.NAME_FIELD_NUMBER), "The '%s' metric is already defined. The metric name must be unique.", metric.getName()); } metrics.put(metric.getName(), false); validateMetric(metric); } }
private void checkMatchingMetrics(Service.Builder serviceBuilder, MetricRule metricRule) { Set<String> definedMetrics = new HashSet<>(); for (MetricDescriptor definedMetric : serviceBuilder.getMetricsList()) { definedMetrics.add(definedMetric.getName()); } Set<String> missingMetrics = Sets.difference(metricRule.getMetricCosts().keySet(), definedMetrics); if (!missingMetrics.isEmpty()) { diagCollector.addDiag( Diag.error( new SimpleLocation(MetricRuleGenerator.QUOTA_SWAGGER_EXTENSION), "Quota Extension for method '%s' references undefined metric(s) '%s'", metricRule.getSelector(), Joiner.on(',').join(missingMetrics))); } }
private void checkMatchingMetrics(Service.Builder serviceBuilder, MetricRule metricRule) { Set<String> definedMetrics = new HashSet<>(); for (MetricDescriptor definedMetric : serviceBuilder.getMetricsList()) { definedMetrics.add(definedMetric.getName()); } Set<String> missingMetrics = Sets.difference(metricRule.getMetricCosts().keySet(), definedMetrics); if (!missingMetrics.isEmpty()) { diagCollector.addDiag( Diag.error( new SimpleLocation(MetricRuleGenerator.QUOTA_SWAGGER_EXTENSION), "Quota Extension for method '%s' references undefined metric(s) '%s'", metricRule.getSelector(), Joiner.on(',').join(missingMetrics))); } }
/** * <pre> * The resource name of the metric descriptor. Depending on the * implementation, the name typically includes: (1) the parent resource name * that defines the scope of the metric type or of its data; and (2) the * metric's URL-encoded type, which also appears in the `type` field of this * descriptor. For example, following is the resource name of a custom * metric within the GCP project `my-project-id`: * "projects/my-project-id/metricDescriptors/custom.googleapis.com%2Finvoice%2Fpaid%2Famount" * </pre> * * <code>string name = 1;</code> */ public Builder clearName() { name_ = getDefaultInstance().getName(); onChanged(); return this; } /**
/** Checks that the metric names and costs in metric rules are valid. */ private void checkMetricRulesAreValid(Service service) { Set<String> metricNames = new HashSet<>(); for (MetricDescriptor metric : service.getMetricsList()) { metricNames.add(metric.getName()); } for (MetricRule rule : service.getQuota().getMetricRulesList()) { for (Entry<String, Long> entry : rule.getMetricCosts().entrySet()) { if (!metricNames.contains(entry.getKey())) { error( MessageLocationContext.createForRepeated(rule, "metric_costs", entry.getKey()), "Metric '%s' referenced by metric rule '%s' does not exist.", entry.getKey(), rule.getSelector()); } if (entry.getValue() < 0) { error( MessageLocationContext.createForRepeated(rule, "metric_costs", entry.getKey()), "Metric cost %d for metric '%s' must not be negative.", entry.getValue(), entry.getKey()); } } } } }
/** Checks that the metric names and costs in metric rules are valid. */ private void checkMetricRulesAreValid(Service service) { Set<String> metricNames = new HashSet<>(); for (MetricDescriptor metric : service.getMetricsList()) { metricNames.add(metric.getName()); } for (MetricRule rule : service.getQuota().getMetricRulesList()) { for (Entry<String, Long> entry : rule.getMetricCosts().entrySet()) { if (!metricNames.contains(entry.getKey())) { error( MessageLocationContext.createForRepeated(rule, "metric_costs", entry.getKey()), "Metric '%s' referenced by metric rule '%s' does not exist.", entry.getKey(), rule.getSelector()); } if (entry.getValue() < 0) { error( MessageLocationContext.createForRepeated(rule, "metric_costs", entry.getKey()), "Metric cost %d for metric '%s' must not be negative.", entry.getValue(), entry.getKey()); } } } } }
public static void clearCustomMetricDescriptors(MetricServiceSettings settings, String projectId) { try { MetricServiceClient client = MetricServiceClient.create(settings); Iterable<MetricServiceClient.ListMetricDescriptorsPage> listMetricDescriptorsPages = client.listMetricDescriptors(ListMetricDescriptorsRequest.newBuilder() .setName("projects/" + projectId) .setFilter("metric.type = starts_with(\"custom.googleapis.com/\")") .build()).iteratePages(); int deleted = 0; for (MetricServiceClient.ListMetricDescriptorsPage page : listMetricDescriptorsPages) { for (MetricDescriptor metricDescriptor : page.getValues()) { System.out.println("deleting " + metricDescriptor.getName()); client.deleteMetricDescriptor(metricDescriptor.getName()); deleted++; } } System.out.println("Deleted " + deleted + " custom metric descriptors"); } catch (IOException e) { throw new UncheckedIOException(e); } }
@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + NAME_FIELD_NUMBER; hash = (53 * hash) + getName().hashCode(); hash = (37 * hash) + TYPE_FIELD_NUMBER; hash = (53 * hash) + getType().hashCode(); if (getLabelsCount() > 0) { hash = (37 * hash) + LABELS_FIELD_NUMBER; hash = (53 * hash) + getLabelsList().hashCode(); } hash = (37 * hash) + METRIC_KIND_FIELD_NUMBER; hash = (53 * hash) + metricKind_; hash = (37 * hash) + VALUE_TYPE_FIELD_NUMBER; hash = (53 * hash) + valueType_; hash = (37 * hash) + UNIT_FIELD_NUMBER; hash = (53 * hash) + getUnit().hashCode(); hash = (37 * hash) + DESCRIPTION_FIELD_NUMBER; hash = (53 * hash) + getDescription().hashCode(); hash = (37 * hash) + DISPLAY_NAME_FIELD_NUMBER; hash = (53 * hash) + getDisplayName().hashCode(); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.api.MetricDescriptor)) { return super.equals(obj); } com.google.api.MetricDescriptor other = (com.google.api.MetricDescriptor) obj; boolean result = true; result = result && getName() .equals(other.getName()); result = result && getType() .equals(other.getType()); result = result && getLabelsList() .equals(other.getLabelsList()); result = result && metricKind_ == other.metricKind_; result = result && valueType_ == other.valueType_; result = result && getUnit() .equals(other.getUnit()); result = result && getDescription() .equals(other.getDescription()); result = result && getDisplayName() .equals(other.getDisplayName()); return result; }
private void checkMetricReferencesExist(Service service) { Set<String> metricNames = new HashSet<>(); for (MetricDescriptor metric : service.getMetricsList()) { metricNames.add(metric.getName()); } Quota quotaConfig = service.getQuota(); for (QuotaLimit limit : quotaConfig.getLimitsList()) { if (!metricNames.contains(limit.getMetric())) { error( MessageLocationContext.create(limit, "metric"), "Metric '%s' referenced by quota limit '%s' does not exist.", limit.getMetric(), limit.getName()); } } } }
private void checkMetricReferencesExist(Service service) { Set<String> metricNames = new HashSet<>(); for (MetricDescriptor metric : service.getMetricsList()) { metricNames.add(metric.getName()); } Quota quotaConfig = service.getQuota(); for (QuotaLimit limit : quotaConfig.getLimitsList()) { if (!metricNames.contains(limit.getMetric())) { error( MessageLocationContext.create(limit, "metric"), "Metric '%s' referenced by quota limit '%s' does not exist.", limit.getMetric(), limit.getName()); } } } }
MessageLocationContext.create(metric, MetricDescriptor.NAME_FIELD_NUMBER), "metric name", metric.getName()); String.format("'%s' metric", metric.getName()), metric.getLabelsList()); "The metric kind of the '%s' metric is not specified. " + "Allowed values are GAUGE, DELTA and CUMULATIVE.", metric.getName()); "The metric value type of the '%s' metric is not specified. " + "Allowed values are BOOL, INT64, DOUBLE, STRING, DISTRIBUTION and MONEY.", metric.getName()); "The '%s' metric is of %s type and of %s kind. " + "Metrics of value type BOOL and STRING must be of GUAGE kind.", metric.getName(), metric.getValueType().getValueDescriptor().getName(), metric.getMetricKind().getValueDescriptor().getName()); metricDescriptors.put(metric.getName(), metric);
MessageLocationContext.create(metric, MetricDescriptor.NAME_FIELD_NUMBER), "metric name", metric.getName()); String.format("'%s' metric", metric.getName()), metric.getLabelsList()); "The metric kind of the '%s' metric is not specified. " + "Allowed values are GAUGE, DELTA and CUMULATIVE.", metric.getName()); "The metric value type of the '%s' metric is not specified. " + "Allowed values are BOOL, INT64, DOUBLE, STRING, DISTRIBUTION and MONEY.", metric.getName()); "The '%s' metric is of %s type and of %s kind. " + "Metrics of value type BOOL and STRING must be of GUAGE kind.", metric.getName(), metric.getValueType().getValueDescriptor().getName(), metric.getMetricKind().getValueDescriptor().getName()); metricDescriptors.put(metric.getName(), metric);
public Builder mergeFrom(com.google.api.MetricDescriptor other) { if (other == com.google.api.MetricDescriptor.getDefaultInstance()) return this; if (!other.getName().isEmpty()) { name_ = other.name_; onChanged();
@Test public void createMetricDescriptor_cumulative() { MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor( METRIC_DESCRIPTOR_2, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX); assertThat(metricDescriptor.getName()) .isEqualTo( "projects/" + PROJECT_ID + "/metricDescriptors/custom.googleapis.com/opencensus/" + METRIC_NAME); assertThat(metricDescriptor.getDescription()).isEqualTo(METRIC_DESCRIPTION); assertThat(metricDescriptor.getDisplayName()).isEqualTo("OpenCensus/" + METRIC_NAME); assertThat(metricDescriptor.getType()) .isEqualTo("custom.googleapis.com/opencensus/" + METRIC_NAME); assertThat(metricDescriptor.getUnit()).isEqualTo("1"); assertThat(metricDescriptor.getMetricKind()).isEqualTo(MetricKind.CUMULATIVE); assertThat(metricDescriptor.getValueType()).isEqualTo(MetricDescriptor.ValueType.INT64); assertThat(metricDescriptor.getLabelsList()) .containsExactly( LabelDescriptor.newBuilder() .setKey(StackdriverExportUtils.OPENCENSUS_TASK) .setDescription(StackdriverExportUtils.OPENCENSUS_TASK_DESCRIPTION) .setValueType(ValueType.STRING) .build()); }
@Test public void createMetricDescriptor() { MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor( METRIC_DESCRIPTOR, PROJECT_ID, "custom.googleapis.com/myorg/", "myorg/"); assertThat(metricDescriptor.getName()) .isEqualTo( "projects/" + PROJECT_ID + "/metricDescriptors/custom.googleapis.com/myorg/" + METRIC_NAME); assertThat(metricDescriptor.getDescription()).isEqualTo(METRIC_DESCRIPTION); assertThat(metricDescriptor.getDisplayName()).isEqualTo("myorg/" + METRIC_NAME); assertThat(metricDescriptor.getType()).isEqualTo("custom.googleapis.com/myorg/" + METRIC_NAME); assertThat(metricDescriptor.getUnit()).isEqualTo(METRIC_UNIT); assertThat(metricDescriptor.getMetricKind()).isEqualTo(MetricKind.CUMULATIVE); assertThat(metricDescriptor.getValueType()).isEqualTo(MetricDescriptor.ValueType.DOUBLE); assertThat(metricDescriptor.getLabelsList()) .containsExactly( LabelDescriptor.newBuilder() .setKey(LABEL_KEY.get(0).getKey()) .setDescription(LABEL_KEY.get(0).getDescription()) .setValueType(ValueType.STRING) .build(), LabelDescriptor.newBuilder() .setKey(StackdriverExportUtils.OPENCENSUS_TASK) .setDescription(StackdriverExportUtils.OPENCENSUS_TASK_DESCRIPTION) .setValueType(ValueType.STRING) .build()); }