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());
}
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());
}
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);
}
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;
}
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;
}
Aggregations