Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stackdriver): read INT64 metrics correctly #933

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kayenta-stackdriver/kayenta-stackdriver.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ dependencies {
implementation project(":kayenta-core")
implementation project(":kayenta-google")
api "com.google.apis:google-api-services-monitoring"

testImplementation 'org.mockito:mockito-inline:2.13.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,27 @@ public List<MetricSet> queryMetrics(
: stackdriverCanaryScope.getEnd();

// TODO(duftler): What if there are no data points?
List<Double> pointValues =
points.stream()
.map(point -> point.getValue().getDoubleValue())
.collect(Collectors.toList());
List<Double> pointValues;

if (timeSeries.getValueType().equals("INT64")) {
pointValues =
points.stream()
.map(point -> (double) point.getValue().getInt64Value())
.collect(Collectors.toList());
} else if (timeSeries.getValueType().equals("DOUBLE")) {
pointValues =
points.stream()
.map(point -> point.getValue().getDoubleValue())
.collect(Collectors.toList());
} else {
log.warn(
"expected timeSeries value type to be either DOUBLE or INT64. Got {}.",
timeSeries.getValueType());
pointValues =
points.stream()
.map(point -> point.getValue().getDoubleValue())
.collect(Collectors.toList());
}

MetricSet.MetricSetBuilder metricSetBuilder =
MetricSet.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.netflix.kayenta.stackdriver.metrics;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import com.google.api.services.monitoring.v3.Monitoring;
import com.google.api.services.monitoring.v3.model.ListMetricDescriptorsResponse;
import com.google.api.services.monitoring.v3.model.ListTimeSeriesResponse;
import com.google.api.services.monitoring.v3.model.MetricDescriptor;
import com.google.api.services.monitoring.v3.model.Point;
import com.google.api.services.monitoring.v3.model.TimeInterval;
import com.google.api.services.monitoring.v3.model.TimeSeries;
import com.google.api.services.monitoring.v3.model.TypedValue;
import com.netflix.kayenta.canary.CanaryConfig;
import com.netflix.kayenta.canary.CanaryMetricConfig;
import com.netflix.kayenta.canary.CanaryScope;
import com.netflix.kayenta.canary.providers.metrics.StackdriverCanaryMetricSetQueryConfig;
import com.netflix.kayenta.google.security.GoogleNamedAccountCredentials;
import com.netflix.kayenta.metrics.MetricSet;
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.netflix.kayenta.stackdriver.canary.StackdriverCanaryScope;
import com.netflix.spectator.api.Registry;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class StackdriverMetricsServiceTest {

private static final String ACCOUNT = "test-account";

@Mock AccountCredentialsRepository accountCredentialsRepoMock;

@Mock(answer = RETURNS_DEEP_STUBS)
GoogleNamedAccountCredentials googleAccountCredentialsMock;

@Mock GoogleNamedAccountCredentials stackdriverCredentialsMock;
@Mock ListMetricDescriptorsResponse listMetricDescriptorsResponseMock;
@Mock StackdriverCanaryScope canaryScopeMock;

@Mock(answer = RETURNS_DEEP_STUBS)
Monitoring monitoringMock;

@Mock Monitoring.Projects.TimeSeries.List timeSeriesListMock;
@Mock ListTimeSeriesResponse responseMock;

@Mock(answer = RETURNS_DEEP_STUBS)
Registry registry;

@InjectMocks StackdriverMetricsService stackdriverMetricsService;

@Test
public void readsInt64Metrics() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails without the code change above, which is great!

when(accountCredentialsRepoMock.getRequiredOne(ACCOUNT)).thenReturn(stackdriverCredentialsMock);

when(stackdriverCredentialsMock.getMonitoring()).thenReturn(monitoringMock);

when(monitoringMock
.projects()
.timeSeries()
.list(anyString())
.setAggregationAlignmentPeriod(anyString())
.setAggregationCrossSeriesReducer(anyString())
.setAggregationPerSeriesAligner(anyString())
.setFilter(anyString())
.setIntervalStartTime(anyString())
.setIntervalEndTime(anyString()))
.thenReturn(timeSeriesListMock);

when(timeSeriesListMock.execute()).thenReturn(responseMock);

List<TimeSeries> timeSeriesListWithInt64Points = new ArrayList<TimeSeries>();

// Create a time series with INT64 points
List<Point> int64Points = new ArrayList<Point>();
int64Points.add(
new Point()
.setValue(new TypedValue().setInt64Value((Long) 64l))
.setInterval(
new TimeInterval()
.setStartTime("1970-01-01T00:00:00.00Z")
.setEndTime("1970-01-01T00:00:01.00Z")));
TimeSeries timeSeriesWithInt64Points =
new TimeSeries().setValueType("INT64").setPoints(int64Points);
timeSeriesListWithInt64Points.add(timeSeriesWithInt64Points);

when(responseMock.getTimeSeries()).thenReturn(timeSeriesListWithInt64Points);

CanaryConfig canaryConfig = new CanaryConfig();
CanaryMetricConfig canaryMetricConfig =
CanaryMetricConfig.builder()
.name("metricConfig")
.query(
StackdriverCanaryMetricSetQueryConfig.builder()
.resourceType("global")
.metricType("instance")
.build())
.build();

CanaryScope canaryScope =
new StackdriverCanaryScope()
.setStart(Instant.EPOCH)
.setEnd(Instant.EPOCH.plusSeconds(1))
.setStep(1l);
List<MetricSet> queriedMetrics =
stackdriverMetricsService.queryMetrics(
ACCOUNT, canaryConfig, canaryMetricConfig, canaryScope);

assertThat(queriedMetrics.get(0).getValues()).contains(64d);
}

@Test
public void returnsSingleMetricDescriptorInCache() throws IOException {
Set<AccountCredentials> accountCredentialsSetMock = new HashSet<>();
accountCredentialsSetMock.add(googleAccountCredentialsMock);

when(accountCredentialsRepoMock.getAllOf(AccountCredentials.Type.METRICS_STORE))
.thenReturn(accountCredentialsSetMock);

when(googleAccountCredentialsMock
.getMonitoring()
.projects()
.metricDescriptors()
.list(anyString())
.execute())
.thenReturn(listMetricDescriptorsResponseMock);

List<MetricDescriptor> metricDesciprtorMockList = new ArrayList<MetricDescriptor>();

MetricDescriptor exampleMetricDescriptor = new MetricDescriptor();
metricDesciprtorMockList.add(exampleMetricDescriptor);
when(listMetricDescriptorsResponseMock.getMetricDescriptors())
.thenReturn(metricDesciprtorMockList);

stackdriverMetricsService.updateMetricDescriptorsCache();

List<Map> metadata = stackdriverMetricsService.getMetadata(ACCOUNT, "");

assertThat(metadata).containsOnly(exampleMetricDescriptor);
}
}