com.google.api.LabelDescriptor Java Examples

The following examples show how to use com.google.api.LabelDescriptor. 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: StackdriverExportUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #2
Source File: StackdriverExportUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static LabelDescriptor createLabelDescriptor(LabelKey labelKey) {
  LabelDescriptor.Builder builder = LabelDescriptor.newBuilder();
  builder.setKey(labelKey.getKey());
  builder.setDescription(labelKey.getDescription());
  // Now we only support String tags
  builder.setValueType(ValueType.STRING);
  return builder.build();
}
 
Example #3
Source File: StackdriverExportUtilsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createLabelDescriptor() {
  assertThat(StackdriverExportUtils.createLabelDescriptor(LabelKey.create("key", "desc")))
      .isEqualTo(
          LabelDescriptor.newBuilder()
              .setKey("key")
              .setDescription("desc")
              .setValueType(ValueType.STRING)
              .build());
}
 
Example #4
Source File: StackdriverExportUtilsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createMetricDescriptor() {
  MetricDescriptor metricDescriptor =
      StackdriverExportUtils.createMetricDescriptor(
          METRIC_DESCRIPTOR,
          PROJECT_ID,
          "custom.googleapis.com/myorg/",
          "myorg/",
          DEFAULT_CONSTANT_LABELS);
  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_KEY.getKey())
              .setDescription(StackdriverExportUtils.OPENCENSUS_TASK_KEY.getDescription())
              .setValueType(ValueType.STRING)
              .build());
}
 
Example #5
Source File: StackdriverExportUtilsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createMetricDescriptor_cumulative() {
  MetricDescriptor metricDescriptor =
      StackdriverExportUtils.createMetricDescriptor(
          METRIC_DESCRIPTOR_2,
          PROJECT_ID,
          CUSTOM_OPENCENSUS_DOMAIN,
          DEFAULT_DISPLAY_NAME_PREFIX,
          DEFAULT_CONSTANT_LABELS);
  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_KEY.getKey())
              .setDescription(StackdriverExportUtils.OPENCENSUS_TASK_KEY.getDescription())
              .setValueType(ValueType.STRING)
              .build());
}
 
Example #6
Source File: ServiceControlConfigValidator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private void validateLabels(
    LocationContext location, String fieldRef, List<LabelDescriptor> labelsList) {
  // - Labels list cannot be longer than predefined limit.
  validateMaxListSize(
      location,
      String.format("label list of the %s", fieldRef),
      labelsList,
      bounds.getMaxLabels());
  for (LabelDescriptor label : labelsList) {
    validateLabel(location, fieldRef, label);
  }
}
 
Example #7
Source File: ServiceControlConfigValidator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private void validateLabel(LocationContext location, String fieldRef, LabelDescriptor label) {
  // - Label key cannot be empty.
  // - Label key cannot be longer than predefined limit.
  validateNonNullStringLength(location, String.format("%s label key", fieldRef), label.getKey());
  // - Label description cannot be longer than predefined limit.
  validateStringLength(
      location, String.format("%s label description", fieldRef), label.getDescription());
}
 
Example #8
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]
}