com.google.api.services.monitoring.v3.model.MetricDescriptor Java Examples

The following examples show how to use com.google.api.services.monitoring.v3.model.MetricDescriptor. 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: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 6 votes vote down vote up
@Test
public void getEncodedTimeSeries_nullLabels_encodes() throws Exception {
  ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));
  HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream);
  HttpResponseException.Builder httpResponseExceptionBuilder =
      new HttpResponseException.Builder(response);
  httpResponseExceptionBuilder.setStatusCode(400);
  httpResponseExceptionBuilder.setStatusMessage("ALREADY_EXISTS");
  GoogleJsonResponseException exception =
      new GoogleJsonResponseException(httpResponseExceptionBuilder, null);
  when(metricDescriptorCreate.execute()).thenThrow(exception);
  when(metricDescriptorGet.execute())
      .thenReturn(new MetricDescriptor().setName("foo").setLabels(null));
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
  writer.registerMetric(metric);

  TimeSeries timeSeries =
      writer.getEncodedTimeSeries(
          MetricPoint.create(metric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), 10L));

  assertThat(timeSeries.getMetric().getLabels()).isEmpty();
}
 
Example #2
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 6 votes vote down vote up
@Test
public void getEncodedTimeSeries_booleanMetric_encodes() throws Exception {
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);

  MetricDescriptor boolDescriptor = StackdriverWriter.encodeMetricDescriptor(boolMetric);
  when(metricDescriptorCreate.execute()).thenReturn(boolDescriptor);
  MetricPoint<Boolean> nativePoint =
      MetricPoint.create(boolMetric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), true);

  TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);

  assertThat(timeSeries.getValueType()).isEqualTo("BOOL");
  assertThat(timeSeries.getMetricKind()).isEqualTo("GAUGE");
  List<Point> points = timeSeries.getPoints();
  assertThat(points).hasSize(1);
  Point point = points.get(0);
  assertThat(point.getValue().getBoolValue()).isEqualTo(true);
  assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z");
  assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
}
 
Example #3
Source File: StackdriverWriter.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static MetricDescriptor encodeMetricDescriptor(com.google.monitoring.metrics.Metric<?> metric) {
  return new MetricDescriptor()
      .setType(METRIC_DOMAIN + metric.getMetricSchema().name())
      .setDescription(metric.getMetricSchema().description())
      .setDisplayName(metric.getMetricSchema().valueDisplayName())
      .setValueType(ENCODED_METRIC_TYPES.get(metric.getValueClass()))
      .setLabels(encodeLabelDescriptors(metric.getMetricSchema().labels()))
      .setMetricKind(ENCODED_METRIC_KINDS.get(metric.getMetricSchema().kind().name()));
}
 
Example #4
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void encodeMetricDescriptor_simpleMetric_encodes() {
  MetricDescriptor descriptor = StackdriverWriter.encodeMetricDescriptor(metric);

  assertThat(descriptor.getType()).isEqualTo("custom.googleapis.com/name");
  assertThat(descriptor.getValueType()).isEqualTo("INT64");
  assertThat(descriptor.getDescription()).isEqualTo("desc");
  assertThat(descriptor.getDisplayName()).isEqualTo("vdn");
  assertThat(descriptor.getLabels())
      .containsExactly(
          new com.google.api.services.monitoring.v3.model.LabelDescriptor()
              .setValueType("STRING")
              .setKey("label1")
              .setDescription("desc1"));
}
 
Example #5
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void getEncodedTimeSeries_distributionMetricCustomFitter_encodes() throws Exception {
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);

  MetricDescriptor descriptor = StackdriverWriter.encodeMetricDescriptor(distributionMetric);
  when(metricDescriptorCreate.execute()).thenReturn(descriptor);
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(5.0)));
  distribution.add(10.0, 5L);
  distribution.add(0.0, 5L);
  MetricPoint<Distribution> nativePoint =
      MetricPoint.create(
          distributionMetric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), distribution);

  TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);

  assertThat(timeSeries.getValueType()).isEqualTo("DISTRIBUTION");
  assertThat(timeSeries.getMetricKind()).isEqualTo("GAUGE");
  List<Point> points = timeSeries.getPoints();
  assertThat(points).hasSize(1);
  Point point = points.get(0);
  assertThat(point.getValue().getDistributionValue())
      .isEqualTo(
          new com.google.api.services.monitoring.v3.model.Distribution()
              .setMean(5.0)
              .setSumOfSquaredDeviation(250.0)
              .setCount(10L)
              .setBucketCounts(ImmutableList.of(5L, 5L))
              .setBucketOptions(
                  new BucketOptions()
                      .setExplicitBuckets(new Explicit().setBounds(ImmutableList.of(5.0)))));
  assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z");
  assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
}
 
Example #6
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void getEncodedTimeSeries_distributionMetricLinearFitter_encodes() throws Exception {
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);

  MetricDescriptor descriptor = StackdriverWriter.encodeMetricDescriptor(distributionMetric);
  when(metricDescriptorCreate.execute()).thenReturn(descriptor);
  MutableDistribution distribution = new MutableDistribution(LinearFitter.create(2, 5.0, 3.0));
  distribution.add(0.0, 1L);
  distribution.add(3.0, 2L);
  distribution.add(10.0, 5L);
  distribution.add(20.0, 5L);
  MetricPoint<Distribution> nativePoint =
      MetricPoint.create(
          distributionMetric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), distribution);

  TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);

  assertThat(timeSeries.getValueType()).isEqualTo("DISTRIBUTION");
  assertThat(timeSeries.getMetricKind()).isEqualTo("GAUGE");
  List<Point> points = timeSeries.getPoints();
  assertThat(points).hasSize(1);
  Point point = points.get(0);
  assertThat(point.getValue().getDistributionValue())
      .isEqualTo(
          new com.google.api.services.monitoring.v3.model.Distribution()
              .setMean(12.0)
              .setSumOfSquaredDeviation(646.0)
              .setCount(13L)
              .setBucketCounts(ImmutableList.of(1L, 2L, 5L, 5L))
              .setBucketOptions(
                  new BucketOptions()
                      .setLinearBuckets(
                          new Linear().setNumFiniteBuckets(2).setWidth(5.0).setOffset(3.0))));
  assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z");
  assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
}
 
Example #7
Source File: StackdriverWriter.java    From java-monitoring-client-library with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a metric's {@link MetricDescriptor} with the Monitoring API.
 *
 * @param metric the metric to be registered.
 * @see <a
 *     href="https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors">Stackdriver
 *     MetricDescriptor API</a>
 */
@VisibleForTesting
MetricDescriptor registerMetric(final com.google.monitoring.metrics.Metric<?> metric)
    throws IOException {
  if (registeredDescriptors.containsKey(metric)) {
    logger.fine(
        String.format("Using existing metric descriptor %s", metric.getMetricSchema().name()));
    return registeredDescriptors.get(metric);
  }

  MetricDescriptor descriptor = encodeMetricDescriptor(metric);

  rateLimiter.acquire();
  // We try to create a descriptor, but it may have been created already, so we re-fetch it on
  // failure
  try {
    descriptor =
        monitoringClient
            .projects()
            .metricDescriptors()
            .create(projectResource, descriptor)
            .execute();
    logger.info(String.format("Registered new metric descriptor %s", descriptor.getType()));
  } catch (GoogleJsonResponseException jsonException) {
    // Not the error we were expecting, just give up
    if (!"ALREADY_EXISTS".equals(jsonException.getStatusMessage())) {
      throw jsonException;
    }

    descriptor =
        monitoringClient
            .projects()
            .metricDescriptors()
            .get(projectResource + "/metricDescriptors/" + descriptor.getType())
            .execute();

    logger.info(
        String.format("Fetched existing metric descriptor %s", metric.getMetricSchema().name()));
  }

  registeredDescriptors.put(metric, descriptor);

  return descriptor;
}
 
Example #8
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  when(metric.getValueClass()).thenReturn(Long.class);
  when(metric.getCardinality()).thenReturn(1);
  when(metric.getMetricSchema())
      .thenReturn(
          MetricSchema.create(
              "/name",
              "desc",
              "vdn",
              Kind.CUMULATIVE,
              ImmutableSet.of(LabelDescriptor.create("label1", "desc1"))));
  // Store in an intermediate value, because Mockito hates when mocks are evaluated inside of
  // thenReturn() methods.
  MetricPoint<Long> longPoint =
      MetricPoint.create(
          metric,
          ImmutableList.of("value1"),
          Instant.ofEpochMilli(1337),
          Instant.ofEpochMilli(1338),
          5L);
  when(metric.getTimestampedValues()).thenReturn(ImmutableList.of(longPoint));

  when(boolMetric.getValueClass()).thenReturn(Boolean.class);
  when(boolMetric.getMetricSchema())
      .thenReturn(
          MetricSchema.create(
              "/name",
              "desc",
              "vdn",
              Kind.GAUGE,
              ImmutableSet.of(LabelDescriptor.create("label1", "desc1"))));
  // Store in an intermediate value, because Mockito hates when mocks are evaluated inside of
  // thenReturn() methods.
  MetricPoint<Boolean> boolPoint =
      MetricPoint.create(boolMetric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), true);
  when(boolMetric.getTimestampedValues()).thenReturn(ImmutableList.of(boolPoint));

  when(distributionMetric.getMetricSchema())
      .thenReturn(
          MetricSchema.create(
              "/name",
              "desc",
              "vdn",
              Kind.GAUGE,
              ImmutableSet.of(LabelDescriptor.create("label1", "desc1"))));
  when(distributionMetric.getValueClass()).thenReturn(Distribution.class);

  MetricDescriptor descriptor = StackdriverWriter.encodeMetricDescriptor(metric);
  when(client.projects()).thenReturn(projects);
  when(projects.metricDescriptors()).thenReturn(metricDescriptors);
  when(projects.timeSeries()).thenReturn(timeSeries);
  when(metricDescriptors.create(anyString(), any(MetricDescriptor.class)))
      .thenReturn(metricDescriptorCreate);
  when(metricDescriptorCreate.execute()).thenReturn(descriptor);
  when(metricDescriptors.get(anyString())).thenReturn(metricDescriptorGet);
  when(metricDescriptorGet.execute()).thenReturn(descriptor);
  when(timeSeries.create(anyString(), any(CreateTimeSeriesRequest.class)))
      .thenReturn(timeSeriesCreate);
}
 
Example #9
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 4 votes vote down vote up
@Test
public void getEncodedTimeSeries_gaugeMetricPoint_zeroInterval_encodesSameInterval()
    throws Exception {
  when(metric.getMetricSchema())
      .thenReturn(
          MetricSchema.create(
              "/name",
              "desc",
              "vdn",
              Kind.GAUGE,
              ImmutableSet.of(LabelDescriptor.create("label1", "desc1"))));
  // Store in an intermediate value, because Mockito hates when mocks are evaluated inside of
  // thenReturn() methods.
  MetricPoint<Long> testPoint =
      MetricPoint.create(metric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), 10L);
  when(metric.getTimestampedValues()).thenReturn(ImmutableList.of(testPoint));
  // Store in an intermediate value, because Mockito hates when mocks are evaluated inside of
  // thenReturn() methods.
  MetricDescriptor descriptor = StackdriverWriter.encodeMetricDescriptor(metric);
  when(metricDescriptorCreate.execute()).thenReturn(descriptor);
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
  MetricPoint<Long> nativePoint =
      MetricPoint.create(
          metric,
          ImmutableList.of("foo"),
          Instant.ofEpochMilli(1337),
          Instant.ofEpochMilli(1337),
          10L);

  TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);

  assertThat(timeSeries.getValueType()).isEqualTo("INT64");
  assertThat(timeSeries.getMetricKind()).isEqualTo("GAUGE");
  List<Point> points = timeSeries.getPoints();
  assertThat(points).hasSize(1);
  Point point = points.get(0);
  assertThat(point.getValue().getInt64Value()).isEqualTo(10L);
  assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
  assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z");
}
 
Example #10
Source File: StackdriverWriterTest.java    From java-monitoring-client-library with Apache License 2.0 4 votes vote down vote up
@Test
public void getEncodedTimeSeries_distributionMetricExponentialFitter_encodes() throws Exception {
  StackdriverWriter writer =
      new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);

  MetricDescriptor descriptor = StackdriverWriter.encodeMetricDescriptor(distributionMetric);
  when(metricDescriptorCreate.execute()).thenReturn(descriptor);
  MutableDistribution distribution =
      new MutableDistribution(ExponentialFitter.create(2, 3.0, 0.5));
  distribution.add(0.0, 1L);
  distribution.add(3.0, 2L);
  distribution.add(10.0, 5L);
  distribution.add(20.0, 5L);
  MetricPoint<Distribution> nativePoint =
      MetricPoint.create(
          distributionMetric, ImmutableList.of("foo"), Instant.ofEpochMilli(1337), distribution);

  TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);

  assertThat(timeSeries.getValueType()).isEqualTo("DISTRIBUTION");
  assertThat(timeSeries.getMetricKind()).isEqualTo("GAUGE");
  List<Point> points = timeSeries.getPoints();
  assertThat(points).hasSize(1);
  Point point = points.get(0);
  assertThat(point.getValue().getDistributionValue())
      .isEqualTo(
          new com.google.api.services.monitoring.v3.model.Distribution()
              .setMean(12.0)
              .setSumOfSquaredDeviation(646.0)
              .setCount(13L)
              .setBucketCounts(ImmutableList.of(1L, 0L, 2L, 10L))
              .setBucketOptions(
                  new BucketOptions()
                      .setExponentialBuckets(
                          new Exponential()
                              .setNumFiniteBuckets(2)
                              .setGrowthFactor(3.0)
                              .setScale(0.5))));
  assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z");
  assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
}
 
Example #11
Source File: MetricDescriptorCache.java    From kork with Apache License 2.0 4 votes vote down vote up
/** Helper function providing a hook to fix or omit bad labels. */
private void addSanitizedLabel(
    MetricDescriptor descriptor, Tag tag, Map<String, String> labels) {}