use of com.google.api.services.monitoring.v3.model.TimeSeries in project kork by spinnaker.
the class StackdriverWriterTest method writeRegistryWithLargeRegistry.
@Test
public void writeRegistryWithLargeRegistry() throws IOException {
TestableStackdriverWriter spy = spy(new TestableStackdriverWriter(writerConfig.build()));
Monitoring.Projects.TimeSeries.Create mockCreateMethod = Mockito.mock(Monitoring.Projects.TimeSeries.Create.class);
DefaultRegistry registry = new DefaultRegistry(clock);
// The contents of this timeseries doesnt matter.
// It is technically invalid to have null entries in the list,
// but since we're mocking out the access the values does not matter.
// What is important is the size of the list, so we can verify chunking.
List<TimeSeries> tsList = new ArrayList<TimeSeries>();
for (int i = 0; i < 200; ++i) {
tsList.add(null);
}
// make last one different to test chunking
tsList.add(new TimeSeries());
doReturn(tsList).when(spy).registryToTimeSeries(registry);
// the calls point to the same instance with the final mutated value.
class MatchN implements ArgumentMatcher<CreateTimeSeriesRequest> {
public int found = 0;
private int n;
public MatchN(int n) {
super();
this.n = n;
}
@Override
public String toString() {
return "Match n=" + n;
}
@Override
public boolean matches(CreateTimeSeriesRequest obj) {
boolean eq = ((CreateTimeSeriesRequest) obj).getTimeSeries().size() == n;
found += eq ? 1 : 0;
return eq;
}
}
;
MatchN match200 = new MatchN(200);
MatchN match1 = new MatchN(1);
when(timeseriesApi.create(eq("projects/test-project"), argThat(match200))).thenReturn(mockCreateMethod);
when(timeseriesApi.create(eq("projects/test-project"), argThat(match1))).thenReturn(mockCreateMethod);
when(mockCreateMethod.execute()).thenReturn(null);
spy.writeRegistry(registry);
verify(mockCreateMethod, times(2)).execute();
Assert.assertEquals(1, match200.found);
Assert.assertEquals(1, match1.found);
}
use of com.google.api.services.monitoring.v3.model.TimeSeries in project kork by spinnaker.
the class StackdriverWriter method handleTimeSeriesResponseException.
/**
* Helper function for logging time series errors in more detail.
*
* @see #findProblematicTimeSeriesElement
*/
private void handleTimeSeriesResponseException(HttpResponseException rex, String msg, List<TimeSeries> nextN) {
Matcher matcher = INVALID_LABEL_REGEX.matcher(rex.getContent());
TimeSeries ts = null;
String label = null;
if (matcher.find()) {
int tsIndex = Integer.parseInt(matcher.group(1));
ts = nextN.get(tsIndex);
label = matcher.group(2);
log.error("{}: time series element: {}", rex.getMessage(), ts.toString());
cache.addLabel(ts.getMetric().getType(), label);
try {
log.info("Retrying individual time series element");
CreateTimeSeriesRequest tsRequest = new CreateTimeSeriesRequest();
tsRequest.setTimeSeries(nextN.subList(tsIndex, tsIndex + 1));
service.projects().timeSeries().create(projectResourceName, tsRequest).execute();
} catch (IOException ioex) {
log.error("Retry failed with " + ioex);
}
} else {
log.error("Caught HttpResponseException {}", msg, rex);
}
}
use of com.google.api.services.monitoring.v3.model.TimeSeries 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.TimeSeries 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;
}
Aggregations