Skip to content

Commit

Permalink
Json execution request (#103)
Browse files Browse the repository at this point in the history
Clean up POST /canary/:configName API and judge result
  • Loading branch information
Michael Graff committed Oct 17, 2017
1 parent bedfcfb commit c354c5e
Show file tree
Hide file tree
Showing 33 changed files with 292 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.netflix.kayenta.canary.CanaryScopeFactory;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Collections;
import java.util.Map;

@Component
Expand All @@ -32,20 +32,19 @@ public boolean handles(String serviceType) {
}

@Override
public CanaryScope buildCanaryScope(String scope,
Instant startTimeInstant,
Instant endTimeInstant,
String step,
Map<String, String> extendedScopeParams) {
public CanaryScope buildCanaryScope(CanaryScope canaryScope){
AtlasCanaryScope atlasCanaryScope = new AtlasCanaryScope();
atlasCanaryScope.setScope(scope);
atlasCanaryScope.setStart(startTimeInstant.toEpochMilli() + "");
atlasCanaryScope.setEnd(endTimeInstant.toEpochMilli() + "");
atlasCanaryScope.setStep(step);

if (extendedScopeParams != null && extendedScopeParams.containsKey("type")) {
atlasCanaryScope.setType(extendedScopeParams.get("type"));
atlasCanaryScope.setScope(canaryScope.getScope());
atlasCanaryScope.setStart(canaryScope.getStart());
atlasCanaryScope.setEnd(canaryScope.getEnd());
atlasCanaryScope.setStep(canaryScope.getStep());
atlasCanaryScope.setExtendedScopeParams(canaryScope.getExtendedScopeParams());

Map<String, String> extendedScopeParams = atlasCanaryScope.getExtendedScopeParams();
if (extendedScopeParams == null) {
extendedScopeParams = Collections.emptyMap();
}
atlasCanaryScope.setType(extendedScopeParams.getOrDefault("type", "cluster"));

return atlasCanaryScope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.kayenta.atlas.model.AtlasResults;
import com.netflix.kayenta.util.ObjectMapperFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
Expand All @@ -44,9 +45,7 @@
// We are not implementing full, proper SSE handling here.
public class AtlasSSEConverter implements Converter {

private static final ObjectMapper objectMapper = new ObjectMapper()
.setSerializationInclusion(NON_NULL)
.disable(FAIL_ON_UNKNOWN_PROPERTIES);
private static final ObjectMapper objectMapper = ObjectMapperFactory.getMapper();
private static final List<String> expectedResultsTypeList = Arrays.asList("timeseries", "close");

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.Instant;
import java.util.Collections;

@RestController
Expand All @@ -52,9 +53,9 @@ public String queryMetrics(@RequestParam(required = false) final String metricsA
@ApiParam(defaultValue = "cpu") @RequestParam String metricSetName,
@ApiParam(defaultValue = "cluster") @RequestParam String type,
@RequestParam String scope,
@ApiParam(defaultValue = "0") @RequestParam String start,
@ApiParam(defaultValue = "6000000") @RequestParam String end,
@ApiParam(defaultValue = "PT1M") @RequestParam String step) throws IOException {
@ApiParam(defaultValue = "2000-01-01T00:00:00Z") @RequestParam String start,
@ApiParam(defaultValue = "2000-01-01T04:00:00Z") @RequestParam String end,
@ApiParam(defaultValue = "300") @RequestParam Long step) throws IOException {
String resolvedMetricsAccountName = CredentialsHelper.resolveAccountByNameOrType(metricsAccountName,
AccountCredentials.Type.METRICS_STORE,
accountCredentialsRepository);
Expand All @@ -77,8 +78,8 @@ public String queryMetrics(@RequestParam(required = false) final String metricsA
AtlasCanaryScope atlasCanaryScope = new AtlasCanaryScope();
atlasCanaryScope.setType(type);
atlasCanaryScope.setScope(scope);
atlasCanaryScope.setStart(start);
atlasCanaryScope.setEnd(end);
atlasCanaryScope.setStart(Instant.parse(start));
atlasCanaryScope.setEnd(Instant.parse(end));
atlasCanaryScope.setStep(step);

return synchronousQueryProcessor.processQuery(resolvedMetricsAccountName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.time.temporal.ChronoUnit.SECONDS;

@Builder
@Slf4j
public class AtlasMetricsService implements MetricsService {
Expand Down Expand Up @@ -76,13 +79,13 @@ public List<MetricSet> queryMetrics(String accountName,
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
AtlasRemoteService atlasRemoteService = credentials.getAtlasRemoteService();
// TODO(mgraff): Is this how we decorate the base query?
AtlasCanaryMetricSetQueryConfig atlasMetricSetQuery = (AtlasCanaryMetricSetQueryConfig)canaryMetricConfig.getQuery();
String decoratedQuery = atlasMetricSetQuery.getQ() + "," + atlasCanaryScope.cq();
String isoStep = Duration.of(atlasCanaryScope.getStep(), SECONDS) + "";
List<AtlasResults> atlasResultsList = atlasRemoteService.fetch(decoratedQuery,
atlasCanaryScope.getStart(),
atlasCanaryScope.getEnd(),
atlasCanaryScope.getStep());
isoStep);
Map<String, AtlasResults> idToAtlasResultsMap = AtlasResultsHelper.merge(atlasResultsList);
List<MetricSet> metricSetList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import com.netflix.kayenta.storage.ObjectType;
import com.netflix.kayenta.storage.StorageService;
import com.netflix.kayenta.storage.StorageServiceRepository;
import com.netflix.kayenta.util.ObjectMapperFactory;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.RetryableTask;
import com.netflix.spinnaker.orca.TaskResult;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -41,10 +43,10 @@
import java.util.Map;

@Component
@Slf4j
public class AtlasFetchTask implements RetryableTask {

@Autowired
ObjectMapper objectMapper;
private final ObjectMapper objectMapper = ObjectMapperFactory.getMapper();

@Autowired
AccountCredentialsRepository accountCredentialsRepository;
Expand Down Expand Up @@ -86,8 +88,14 @@ public TaskResult execute(Stage stage) {
String storageAccountName = (String)context.get("storageAccountName");
String configurationAccountName = (String)context.get("configurationAccountName");
String canaryConfigId = (String)context.get("canaryConfigId");
AtlasCanaryScope atlasCanaryScope =
objectMapper.convertValue(stage.getContext().get("atlasCanaryScope"), AtlasCanaryScope.class);
String scopeJson = (String)stage.getContext().get("atlasCanaryScope");
AtlasCanaryScope atlasCanaryScope;
try {
atlasCanaryScope = objectMapper.readValue(scopeJson, AtlasCanaryScope.class);
} catch (IOException e) {
log.error("Unable to parse JSON scope: " + scopeJson, e);
throw new RuntimeException(e);
}
String resolvedMetricsAccountName = CredentialsHelper.resolveAccountByNameOrType(metricsAccountName,
AccountCredentials.Type.METRICS_STORE,
accountCredentialsRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import retrofit.http.GET;
import retrofit.http.Query;

import java.time.Instant;
import java.util.List;

public interface AtlasRemoteService {

// TODO(mgraff): I know this isn't quite right. Just adding all of these in as a starting point.
@GET("/api/v2/fetch")
List<AtlasResults> fetch(@Query("q") String q,
@Query("s") String start,
@Query("e") String end,
@Query("s") Instant start,
@Query("e") Instant end,
@Query("step") String step);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.netflix.kayenta.canary.CanaryConfig;
import com.netflix.kayenta.canary.CanaryMetricConfig;
import com.netflix.kayenta.canary.providers.AtlasCanaryMetricSetQueryConfig;
import com.netflix.kayenta.util.ObjectMapperFactory;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -27,6 +28,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -59,9 +61,7 @@ public class IntegrationTest {
@Autowired
private ResourceLoader resourceLoader;

private ObjectMapper objectMapper = new ObjectMapper()
.setSerializationInclusion(NON_NULL)
.disable(FAIL_ON_UNKNOWN_PROPERTIES);
private static final ObjectMapper objectMapper = ObjectMapperFactory.getMapper();

private String getFileContent(String filename) throws IOException {
try (InputStream inputStream = resourceLoader.getResource("classpath:" + filename).getInputStream()) {
Expand All @@ -75,9 +75,9 @@ private CanaryConfig getConfig(String filename) throws IOException {
}

private CanaryMetricConfigWithResults queryMetric(CanaryMetricConfig metric, AtlasCanaryScope scope) {
long step = Duration.parse(scope.getStep()).toMillis();
long start = Long.parseLong(scope.getStart()) / step * step;
long end = Long.parseLong(scope.getEnd()) / step * step;
long step = Duration.ofSeconds(scope.getStep()).toMillis();
long start = scope.getStart().toEpochMilli() / step * step;
long end = scope.getEnd().toEpochMilli() / step * step;
long count = (end - start) / step;

AtlasCanaryMetricSetQueryConfig atlasMetricSetQuery = (AtlasCanaryMetricSetQueryConfig)metric.getQuery();
Expand Down Expand Up @@ -106,15 +106,15 @@ public void loadConfig() throws Exception {
AtlasCanaryScope experiment = new AtlasCanaryScope();
experiment.setType("asg");
experiment.setScope("kayenta-iep-v400");
experiment.setStart("0");
experiment.setEnd("600000");
experiment.setStep("PT1M");
experiment.setStart(Instant.parse("2000-01-01T00:11:22Z"));
experiment.setEnd(Instant.parse("2000-01-01T04:11:22Z"));
experiment.setStep(300L);
AtlasCanaryScope control = new AtlasCanaryScope();
control.setType("asg");
control.setScope("kayenta-iep-v401");
control.setStart("0");
control.setEnd("600000");
control.setStep("PT1M");
control.setStart(Instant.parse("2000-01-01T00:11:22Z"));
control.setEnd(Instant.parse("2000-01-01T04:11:22Z"));
control.setStep(300L);

// 3. for each metric in the config:
// a. issue an Atlas query for this metric, scoped to the canary
Expand Down
9 changes: 5 additions & 4 deletions kayenta-core/kayenta-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ dependencies {
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-data-rest:$springBootVersion"

// spinnaker.group('jackson')
compile "com.fasterxml.jackson.core:jackson-core:2.8.7"
compile "com.fasterxml.jackson.core:jackson-databind:2.8.7"
compile "com.fasterxml.jackson.core:jackson-databind:2.8.7"
compile "com.fasterxml.jackson.core:jackson-core:2.8.8"
compile "com.fasterxml.jackson.core:jackson-databind:2.8.8"
compile "com.fasterxml.jackson.module:jackson-module-parameter-names:2.8.8"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.8.8"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.8"

// spinnaker.group('retrofitDefault')
compile "com.squareup.retrofit:retrofit:1.9.0"
Expand Down
Loading

0 comments on commit c354c5e

Please sign in to comment.