Java Code Examples for com.google.cloud.monitoring.v3.MetricServiceClient#createMetricDescriptor()

The following examples show how to use com.google.cloud.monitoring.v3.MetricServiceClient#createMetricDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StackdriverMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private void createMetricDescriptorIfNecessary(MetricServiceClient client, Meter.Id id,
                                               MetricDescriptor.ValueType valueType, @Nullable String statistic) {

    if (verifiedDescriptors.isEmpty()) {
        prePopulateVerifiedDescriptors();
    }

    final String metricType = metricType(id, statistic);
    if (!verifiedDescriptors.contains(metricType)) {
        MetricDescriptor descriptor = MetricDescriptor.newBuilder()
                .setType(metricType)
                .setDescription(id.getDescription() == null ? "" : id.getDescription())
                .setMetricKind(MetricDescriptor.MetricKind.GAUGE)
                .setValueType(valueType)
                .build();

        ProjectName name = ProjectName.of(config.projectId());

        CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder()
                .setName(name.toString())
                .setMetricDescriptor(descriptor)
                .build();

        logger.trace("creating metric descriptor:{}{}", System.lineSeparator(), request);

        try {
            client.createMetricDescriptor(request);
            verifiedDescriptors.add(metricType);
        } catch (ApiException e) {
            logger.warn("failed to create metric descriptor in Stackdriver for meter " + id, e);
        }
    }
}
 
Example 2
Source File: Snippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a metric descriptor.
 *
 * <p>See:
 * https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create
 *
 * @param type The metric type
 */
void createMetricDescriptor(String type) throws IOException {
  // [START monitoring_create_metric]
  // Your Google Cloud Platform project ID
  String projectId = System.getProperty("projectId");
  String metricType = CUSTOM_METRIC_DOMAIN + "/" + type;

  final MetricServiceClient client = MetricServiceClient.create();
  ProjectName name = ProjectName.of(projectId);

  MetricDescriptor descriptor =
      MetricDescriptor.newBuilder()
          .setType(metricType)
          .addLabels(
              LabelDescriptor.newBuilder()
                  .setKey("store_id")
                  .setValueType(LabelDescriptor.ValueType.STRING))
          .setDescription("This is a simple example of a custom metric.")
          .setMetricKind(MetricDescriptor.MetricKind.GAUGE)
          .setValueType(MetricDescriptor.ValueType.DOUBLE)
          .build();

  CreateMetricDescriptorRequest request =
      CreateMetricDescriptorRequest.newBuilder()
          .setName(name.toString())
          .setMetricDescriptor(descriptor)
          .build();

  client.createMetricDescriptor(request);
  // [END monitoring_create_metric]
}