Search in sources :

Example 1 with Metric

use of com.google.api.services.monitoring.v3.model.Metric in project kork by spinnaker.

the class MetricDescriptorCache method addLabel.

/**
 * Hack another label into an existing Stackdriver Custom Metric Descriptor.
 *
 * <p>This may lose historic time series data. It returns the new descriptor (result from create)
 * only for testing purposes.
 */
public MetricDescriptor addLabel(String descriptorType, String newLabel) {
    String descriptorName = projectResourceName + "/metricDescriptors/" + descriptorType;
    MetricDescriptor descriptor;
    log.info("Adding label '{}' to stackdriver descriptor '{}'. This may lose existing metric data", newLabel, descriptorName);
    try {
        descriptor = service.projects().metricDescriptors().get(descriptorName).execute();
    } catch (IOException ioex) {
        log.error("Could not fetch descriptor " + descriptorType + ": " + ioex);
        return null;
    }
    List<LabelDescriptor> labels = descriptor.getLabels();
    if (labels == null) {
        labels = new ArrayList<LabelDescriptor>();
        descriptor.setLabels(labels);
    }
    for (LabelDescriptor labelDescriptor : descriptor.getLabels()) {
        if (labelDescriptor.getKey().equals(newLabel)) {
            log.info("{} already added to descriptor", newLabel);
            return descriptor;
        }
    }
    LabelDescriptor labelDescriptor = new LabelDescriptor();
    labelDescriptor.setKey(newLabel);
    labelDescriptor.setValueType("STRING");
    descriptor.getLabels().add(labelDescriptor);
    /* Catch but ignore errors. Assume there is another process making mods too.
     * We may overwrite theirs, but they'll just have to make them again later.
     */
    try {
        log.info("Deleting existing stackdriver descriptor {}", descriptorName);
        service.projects().metricDescriptors().delete(descriptorName).execute();
    } catch (IOException ioex) {
        log.info("Ignoring error " + ioex);
    }
    try {
        log.info("Adding new descriptor for {}", descriptorName);
        return service.projects().metricDescriptors().create(projectResourceName, descriptor).execute();
    } catch (IOException ioex) {
        log.error("Failed to update the descriptor definition: " + ioex);
        return null;
    }
}
Also used : MetricDescriptor(com.google.api.services.monitoring.v3.model.MetricDescriptor) IOException(java.io.IOException) LabelDescriptor(com.google.api.services.monitoring.v3.model.LabelDescriptor)

Example 2 with Metric

use of com.google.api.services.monitoring.v3.model.Metric in project kork by spinnaker.

the class StackdriverWriter method writeRegistryHelper.

/**
 * Implementation of writeRegistry wrapped for timing.
 */
private void writeRegistryHelper(Registry registry) {
    MonitoredResource resource = determineMonitoredResource();
    if (resource == null) {
        log.warn("Cannot determine the managed resource - not flushing metrics.");
        return;
    }
    List<TimeSeries> tsList = registryToTimeSeries(registry);
    if (tsList.isEmpty()) {
        log.debug("No metric data points.");
        return;
    }
    CreateTimeSeriesRequest tsRequest = new CreateTimeSeriesRequest();
    int offset = 0;
    int failed = 0;
    List<TimeSeries> nextN;
    log.debug("Writing metrics...");
    while (offset < tsList.size()) {
        if (offset + MAX_TS_PER_REQUEST < tsList.size()) {
            nextN = tsList.subList(offset, offset + MAX_TS_PER_REQUEST);
            offset += MAX_TS_PER_REQUEST;
        } else {
            nextN = tsList.subList(offset, tsList.size());
            offset = tsList.size();
        }
        tsRequest.setTimeSeries(nextN);
        try {
            service.projects().timeSeries().create(projectResourceName, tsRequest).execute();
        } catch (HttpResponseException rex) {
            handleTimeSeriesResponseException(rex, "creating time series", nextN);
            failed += nextN.size();
        } catch (IOException ioex) {
            log.error("Caught Exception creating time series " + ioex);
            failed += nextN.size();
        }
    }
    log.debug("Wrote {} values", tsList.size() - failed);
}
Also used : TimeSeries(com.google.api.services.monitoring.v3.model.TimeSeries) CreateTimeSeriesRequest(com.google.api.services.monitoring.v3.model.CreateTimeSeriesRequest) MonitoredResource(com.google.api.services.monitoring.v3.model.MonitoredResource) HttpResponseException(com.google.api.client.http.HttpResponseException) IOException(java.io.IOException) Point(com.google.api.services.monitoring.v3.model.Point)

Example 3 with Metric

use of com.google.api.services.monitoring.v3.model.Metric in project kork by spinnaker.

the class StackdriverWriter method measurementToTimeSeries.

/**
 * 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;
}
Also used : TimeSeries(com.google.api.services.monitoring.v3.model.TimeSeries) TimeInterval(com.google.api.services.monitoring.v3.model.TimeInterval) Metric(com.google.api.services.monitoring.v3.model.Metric) Point(com.google.api.services.monitoring.v3.model.Point) Date(java.util.Date) TypedValue(com.google.api.services.monitoring.v3.model.TypedValue)

Aggregations

Point (com.google.api.services.monitoring.v3.model.Point)2 TimeSeries (com.google.api.services.monitoring.v3.model.TimeSeries)2 IOException (java.io.IOException)2 HttpResponseException (com.google.api.client.http.HttpResponseException)1 CreateTimeSeriesRequest (com.google.api.services.monitoring.v3.model.CreateTimeSeriesRequest)1 LabelDescriptor (com.google.api.services.monitoring.v3.model.LabelDescriptor)1 Metric (com.google.api.services.monitoring.v3.model.Metric)1 MetricDescriptor (com.google.api.services.monitoring.v3.model.MetricDescriptor)1 MonitoredResource (com.google.api.services.monitoring.v3.model.MonitoredResource)1 TimeInterval (com.google.api.services.monitoring.v3.model.TimeInterval)1 TypedValue (com.google.api.services.monitoring.v3.model.TypedValue)1 Date (java.util.Date)1