Search in sources :

Example 11 with MetricDescriptor

use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtilsTest method createMetricDescriptor_WithCustomConstantLabels.

@Test
public void createMetricDescriptor_WithCustomConstantLabels() {
    Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(LabelKey.create("my_key", "desc"), LabelValue.create("value"));
    MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, "custom.googleapis.com/myorg/", "myorg/", constantLabels);
    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("my_key").setDescription("desc").setValueType(ValueType.STRING).build());
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) LabelValue(io.opencensus.metrics.LabelValue) LabelKey(io.opencensus.metrics.LabelKey) Test(org.junit.Test)

Example 12 with MetricDescriptor

use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.

the class CreateMetricDescriptorExporterTest method export_MetricNameWithCustomDomain.

@Test
public void export_MetricNameWithCustomDomain() {
    FakeMetricExporter fakeMetricExporter = new FakeMetricExporter();
    CreateMetricDescriptorExporter exporter = new CreateMetricDescriptorExporter(PROJECT_ID, new FakeMetricServiceClient(mockStub), null, null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter);
    exporter.export(Arrays.asList(METRIC_5));
    verify(mockStub, times(1)).createMetricDescriptorCallable();
    MetricDescriptor descriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR_5, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
    verify(mockCreateMetricDescriptorCallable, times(1)).call(eq(CreateMetricDescriptorRequest.newBuilder().setName("projects/" + PROJECT_ID).setMetricDescriptor(descriptor).build()));
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) Test(org.junit.Test)

Example 13 with MetricDescriptor

use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.

the class CreateMetricDescriptorExporterTest method export.

@Test
public void export() {
    FakeMetricExporter fakeMetricExporter = new FakeMetricExporter();
    CreateMetricDescriptorExporter exporter = new CreateMetricDescriptorExporter(PROJECT_ID, new FakeMetricServiceClient(mockStub), null, null, DEFAULT_CONSTANT_LABELS, fakeMetricExporter);
    exporter.export(Arrays.asList(METRIC, METRIC_2));
    verify(mockStub, times(2)).createMetricDescriptorCallable();
    MetricDescriptor descriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
    verify(mockCreateMetricDescriptorCallable, times(1)).call(eq(CreateMetricDescriptorRequest.newBuilder().setName("projects/" + PROJECT_ID).setMetricDescriptor(descriptor).build()));
    MetricDescriptor descriptor2 = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR_2, PROJECT_ID, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_DISPLAY_NAME_PREFIX, DEFAULT_CONSTANT_LABELS);
    verify(mockCreateMetricDescriptorCallable, times(1)).call(eq(CreateMetricDescriptorRequest.newBuilder().setName("projects/" + PROJECT_ID).setMetricDescriptor(descriptor2).build()));
    assertThat(fakeMetricExporter.getLastExported()).containsExactly(METRIC, METRIC_2);
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) Test(org.junit.Test)

Example 14 with MetricDescriptor

use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.

the class CreateMetricDescriptorExporter method registerMetricDescriptor.

// Returns true if the given metricDescriptor is successfully registered to Stackdriver
// Monitoring, or the
// exact same metric has already been registered. Returns false otherwise.
private boolean registerMetricDescriptor(io.opencensus.metrics.export.MetricDescriptor metricDescriptor) {
    String metricName = metricDescriptor.getName();
    io.opencensus.metrics.export.MetricDescriptor existingMetricDescriptor = registeredMetricDescriptors.get(metricName);
    if (existingMetricDescriptor != null) {
        if (existingMetricDescriptor.equals(metricDescriptor)) {
            // Ignore metricDescriptor that are already registered.
            return true;
        } else {
            logger.log(Level.WARNING, "A different metric with the same name is already registered: " + existingMetricDescriptor);
            return false;
        }
    }
    registeredMetricDescriptors.put(metricName, metricDescriptor);
    if (isBuiltInMetric(metricName)) {
        // skip creating metric descriptor for stackdriver built-in metrics.
        return true;
    }
    Span span = tracer.getCurrentSpan();
    span.addAnnotation("Create Stackdriver Metric.");
    MetricDescriptor stackDriverMetricDescriptor = StackdriverExportUtils.createMetricDescriptor(metricDescriptor, projectId, domain, displayNamePrefix, constantLabels);
    CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(projectName.toString()).setMetricDescriptor(stackDriverMetricDescriptor).build();
    try {
        metricServiceClient.createMetricDescriptor(request);
        span.addAnnotation("Finish creating MetricDescriptor.");
        return true;
    } catch (ApiException e) {
        logger.log(Level.WARNING, "ApiException thrown when creating MetricDescriptor.", e);
        span.setStatus(Status.CanonicalCode.valueOf(e.getStatusCode().getCode().name()).toStatus().withDescription("ApiException thrown when creating MetricDescriptor: " + StackdriverExportUtils.exceptionMessage(e)));
        return false;
    } catch (Throwable e) {
        logger.log(Level.WARNING, "Exception thrown when creating MetricDescriptor.", e);
        span.setStatus(Status.UNKNOWN.withDescription("Exception thrown when creating MetricDescriptor: " + StackdriverExportUtils.exceptionMessage(e)));
        return false;
    }
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest) Span(io.opencensus.trace.Span) ApiException(com.google.api.gax.rpc.ApiException)

Example 15 with MetricDescriptor

use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtils method createMetricDescriptor.

// Convert a OpenCensus MetricDescriptor to a StackDriver MetricDescriptor
static MetricDescriptor createMetricDescriptor(io.opencensus.metrics.export.MetricDescriptor metricDescriptor, String projectId, String domain, String displayNamePrefix, Map<LabelKey, LabelValue> constantLabels) {
    MetricDescriptor.Builder builder = MetricDescriptor.newBuilder();
    String type = generateType(metricDescriptor.getName(), domain);
    // Name format refers to
    // cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create
    builder.setName("projects/" + projectId + "/metricDescriptors/" + type);
    builder.setType(type);
    builder.setDescription(metricDescriptor.getDescription());
    builder.setDisplayName(createDisplayName(metricDescriptor.getName(), displayNamePrefix));
    for (LabelKey labelKey : metricDescriptor.getLabelKeys()) {
        builder.addLabels(createLabelDescriptor(labelKey));
    }
    for (LabelKey labelKey : constantLabels.keySet()) {
        builder.addLabels(createLabelDescriptor(labelKey));
    }
    builder.setUnit(metricDescriptor.getUnit());
    builder.setMetricKind(createMetricKind(metricDescriptor.getType()));
    builder.setValueType(createValueType(metricDescriptor.getType()));
    return builder.build();
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) LabelKey(io.opencensus.metrics.LabelKey) ByteString(com.google.protobuf.ByteString)

Aggregations

MetricDescriptor (com.google.api.MetricDescriptor)16 Test (org.junit.Test)10 ProjectName (com.google.monitoring.v3.ProjectName)5 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)4 CreateMetricDescriptorRequest (com.google.monitoring.v3.CreateMetricDescriptorRequest)4 ListMetricDescriptorsRequest (com.google.monitoring.v3.ListMetricDescriptorsRequest)3 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)3 ListMetricDescriptorsPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)2 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)2 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)2 TimeSeries (com.google.monitoring.v3.TimeSeries)2 LabelKey (io.opencensus.metrics.LabelKey)2 IOException (java.io.IOException)2 UUID (java.util.UUID)2 Metric (com.google.api.Metric)1 ApiException (com.google.api.gax.grpc.ApiException)1 ApiException (com.google.api.gax.rpc.ApiException)1 ServiceOptions (com.google.cloud.ServiceOptions)1 BigQuery (com.google.cloud.bigquery.BigQuery)1 BigQueryOptions (com.google.cloud.bigquery.BigQueryOptions)1