use of com.google.api.services.monitoring.v3.model.CreateTimeSeriesRequest 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.CreateTimeSeriesRequest 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.CreateTimeSeriesRequest 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.CreateTimeSeriesRequest in project kork by spinnaker.
the class StackdriverWriterTest method writeRegistryWithSmallRegistry.
@Test
public void writeRegistryWithSmallRegistry() 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);
Counter counterA = registry.counter(idAXY);
Counter counterB = registry.counter(idBXY);
counterA.increment(4);
counterB.increment(10);
when(timeseriesApi.create(eq("projects/test-project"), any(CreateTimeSeriesRequest.class))).thenReturn(mockCreateMethod);
when(mockCreateMethod.execute()).thenReturn(null);
spy.writeRegistry(registry);
verify(mockCreateMethod, times(1)).execute();
ArgumentCaptor<CreateTimeSeriesRequest> captor = ArgumentCaptor.forClass(CreateTimeSeriesRequest.class);
verify(timeseriesApi, times(1)).create(eq("projects/test-project"), captor.capture());
// A, B, timer count and totalTime.
Assert.assertEquals(4, captor.getValue().getTimeSeries().size());
}
Aggregations