metricLabels.put("store_id", "Pittsburg"); Metric metric = Metric.newBuilder() .setType("custom.googleapis.com/stores/daily_sales") .putAllLabels(metricLabels) .build();
.setType("custom.googleapis.com/my_metric") .putAllLabels(metricLabels) .build();
@VisibleForTesting static Metric createMetric( io.opencensus.metrics.export.MetricDescriptor metricDescriptor, List<LabelValue> labelValues, String domain) { Metric.Builder builder = Metric.newBuilder(); builder.setType(generateType(metricDescriptor.getName(), domain)); Map<String, String> stringTagMap = Maps.newHashMap(); List<LabelKey> labelKeys = metricDescriptor.getLabelKeys(); for (int i = 0; i < labelValues.size(); i++) { String value = labelValues.get(i).getValue(); if (value == null) { continue; } stringTagMap.put(labelKeys.get(i).getKey(), value); } stringTagMap.put(OPENCENSUS_TASK, OPENCENSUS_TASK_VALUE_DEFAULT); builder.putAllLabels(stringTagMap); return builder.build(); }
private TimeSeries createTimeSeries(Meter.Id id, TypedValue typedValue, MetricDescriptor.ValueType valueType, @Nullable String statistic) { if (client != null) createMetricDescriptorIfNecessary(client, id, valueType, statistic); String metricType = metricType(id, statistic); Map<String, String> metricLabels = getConventionTags(id).stream() .collect(Collectors.toMap(Tag::getKey, Tag::getValue)); return TimeSeries.newBuilder() .setMetric(Metric.newBuilder() .setType(metricType) .putAllLabels(metricLabels) .build()) .setResource(MonitoredResource.newBuilder() .setType(RESOURCE_TYPE) .putLabels("project_id", config.projectId()) .build()) .setMetricKind(MetricDescriptor.MetricKind.GAUGE) // https://cloud.google.com/monitoring/api/v3/metrics-details#metric-kinds .setValueType(valueType) .addPoints(Point.newBuilder() .setInterval(interval) .setValue(typedValue) .build()) .build(); }
@Test public void createMetric_EmptyLabel() { assertThat( StackdriverExportUtils.createMetric( METRIC_DESCRIPTOR_2, EMPTY_LABEL_VALUE, CUSTOM_OPENCENSUS_DOMAIN)) .isEqualTo( Metric.newBuilder() .setType("custom.googleapis.com/opencensus/" + METRIC_NAME) .putLabels(StackdriverExportUtils.OPENCENSUS_TASK, DEFAULT_TASK_VALUE) .build()); }
@Test public void createMetric_WithExternalMetricDomain() { String prometheusDomain = "external.googleapis.com/prometheus/"; assertThat( StackdriverExportUtils.createMetric(METRIC_DESCRIPTOR, LABEL_VALUE, prometheusDomain)) .isEqualTo( Metric.newBuilder() .setType(prometheusDomain + METRIC_NAME) .putLabels("KEY1", "VALUE1") .putLabels(StackdriverExportUtils.OPENCENSUS_TASK, DEFAULT_TASK_VALUE) .build()); }
@Test public void createMetric() { assertThat( StackdriverExportUtils.createMetric( METRIC_DESCRIPTOR, LABEL_VALUE, CUSTOM_OPENCENSUS_DOMAIN)) .isEqualTo( Metric.newBuilder() .setType("custom.googleapis.com/opencensus/" + METRIC_NAME) .putLabels("KEY1", "VALUE1") .putLabels(StackdriverExportUtils.OPENCENSUS_TASK, DEFAULT_TASK_VALUE) .build()); }