Search in sources :

Example 1 with MonitoredResource

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

the class MonitoredResourceBuilderTest method testEc2Instance.

@Test
public void testEc2Instance() throws IOException {
    String region = "us-east-1";
    String instanceId = "i-abcdef";
    String accountId = "12345";
    String project = "StackdriverProject";
    builder.setStackdriverProject(project);
    String awsIdentityDoc = "{\n" + "\"privateIp\" : \"123.45.67.89\",\n" + "\"devpayProductCodes\" : null,\n" + "\"availabilityZone\" : \"us-east-1d\",\n" + "\"accountId\" : \"" + accountId + "\",\n" + "\"version\" : \"2010-08-31\",\n" + "\"instanceId\" : \"" + instanceId + "\",\n" + "\"billingProducts\" : null,\n" + "\"region\" : \"" + region + "\"\n" + "}";
    doThrow(new IOException()).when(builder).getGoogleMetadataValue(any(String.class));
    doReturn(awsIdentityDoc).when(builder).getAwsIdentityDocument();
    Map<String, String> labels = new HashMap<String, String>();
    labels.put("instance_id", instanceId);
    labels.put("aws_account", accountId);
    labels.put("region", region);
    labels.put("project_id", project);
    MonitoredResource resource = builder.build();
    Assert.assertEquals("aws_ec2_instance", resource.getType());
    Assert.assertEquals(labels, resource.getLabels());
}
Also used : HashMap(java.util.HashMap) MonitoredResource(com.google.api.services.monitoring.v3.model.MonitoredResource) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with MonitoredResource

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

the class MonitoredResourceBuilderTest method testGlobal.

@Test
public void testGlobal() throws IOException {
    String project = "StackdriverProject";
    builder.setStackdriverProject(project);
    doThrow(new IOException()).when(builder).getGoogleMetadataValue(any(String.class));
    doThrow(new IOException()).when(builder).getAwsIdentityDocument();
    Map<String, String> labels = new HashMap<String, String>();
    labels.put("project_id", project);
    MonitoredResource resource = builder.build();
    Assert.assertEquals("global", resource.getType());
    Assert.assertEquals(labels, resource.getLabels());
}
Also used : HashMap(java.util.HashMap) MonitoredResource(com.google.api.services.monitoring.v3.model.MonitoredResource) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with MonitoredResource

use of com.google.api.services.monitoring.v3.model.MonitoredResource 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 4 with MonitoredResource

use of com.google.api.services.monitoring.v3.model.MonitoredResource 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)

Example 5 with MonitoredResource

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

the class MonitoredResourceBuilder method build.

/**
 * Create a MonitoredResource that describes this deployment.
 *
 * <p>This will throw an IOException if the resource could not be created. In practice this
 * exception is not currently thrown.
 *
 * <p>However the expectation is that custom types will be added in the future and there may be
 * transient IO errors interacting with Stackdriver to create the type.
 */
public MonitoredResource build() throws IOException {
    HashMap<String, String> labels = new HashMap<String, String>();
    MonitoredResource resource = new MonitoredResource();
    if (maybeCollectGceInstanceLabels(labels)) {
        resource.setType("gce_instance");
    } else if (maybeCollectGkeInstanceLabels(labels)) {
        resource.setType("gke_container");
    } else if (maybeCollectEc2InstanceLabels(labels)) {
        resource.setType("aws_ec2_instance");
    } else if (stackdriverProjectId.isEmpty()) {
        throw new IllegalStateException("stackdriverProjectId was not set.");
    } else {
        labels.put("project_id", stackdriverProjectId);
        resource.setType("global");
    }
    resource.setLabels(labels);
    return resource;
}
Also used : HashMap(java.util.HashMap) MonitoredResource(com.google.api.services.monitoring.v3.model.MonitoredResource)

Aggregations

MonitoredResource (com.google.api.services.monitoring.v3.model.MonitoredResource)5 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Test (org.junit.Test)3 Point (com.google.api.services.monitoring.v3.model.Point)2 TimeSeries (com.google.api.services.monitoring.v3.model.TimeSeries)2 HttpResponseException (com.google.api.client.http.HttpResponseException)1 CreateTimeSeriesRequest (com.google.api.services.monitoring.v3.model.CreateTimeSeriesRequest)1 Metric (com.google.api.services.monitoring.v3.model.Metric)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