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

The following examples show how to use com.google.api.services.monitoring.v3.model.Metric. 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: 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 #2
Source File: StackdriverWriter.java    From kork with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a Spectator metric Meter into a Stackdriver TimeSeries entry.
 *
 * @param descriptorType The Stackdriver MetricDescriptorType name for the measurement.
 * @param measurement The Spectator Measurement to encode.
 * @return The Stackdriver TimeSeries equivalent for the measurement.
 */
public TimeSeries measurementToTimeSeries(
    String descriptorType, Registry registry, Meter meter, Measurement measurement) {
  Map<String, String> labels =
      cache.tagsToTimeSeriesLabels(descriptorType, measurement.id().tags());

  long millis = measurement.timestamp();
  double value = measurement.value();

  TimeInterval timeInterval = new TimeInterval();
  Date date = new Date(millis);
  timeInterval.setEndTime(rfc3339.format(date));

  String descriptorKind = cache.descriptorTypeToKind(descriptorType, registry, meter);
  if (descriptorKind == "CUMULATIVE") {
    timeInterval.setStartTime(counterStartTimeRfc3339);
  }

  TypedValue typedValue = new TypedValue();
  typedValue.setDoubleValue(value);

  Point point = new Point();
  point.setValue(typedValue);
  point.setInterval(timeInterval);

  Metric metric = new Metric();
  metric.setType(descriptorType);
  metric.setLabels(labels);

  TimeSeries ts = new TimeSeries();
  ts.setResource(monitoredResource);
  ts.setMetric(metric);
  ts.setMetricKind(descriptorKind);
  ts.setValueType("DOUBLE");
  ts.setPoints(Lists.<Point>newArrayList(point));

  return ts;
}
 
Example #3
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;
}