Skip to content

Commit

Permalink
feat(orca): Persist results of analysis. (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Duftler committed Sep 22, 2017
1 parent b2f1e83 commit c1df72a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.kayenta.canary.orca;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.netflix.kayenta.canary.CanaryClassifierThresholdsConfig;
import com.netflix.kayenta.canary.CanaryConfig;
import com.netflix.kayenta.canary.CanaryJudge;
Expand All @@ -41,6 +42,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
public class CanaryJudgeTask implements RetryableTask {
Expand Down Expand Up @@ -118,7 +120,15 @@ public TaskResult execute(Stage stage) {
}

CanaryJudgeResult result = canaryJudge.judge(canaryConfig, orchestratorScoreThresholds, metricSetPairList);
Map<String, CanaryJudgeResult> outputs = Collections.singletonMap("result", result);
String canaryJudgeResultId = UUID.randomUUID() + "";

storageService.storeObject(resolvedStorageAccountName, ObjectType.CANARY_JUDGE_RESULT, canaryJudgeResultId, result);

Map<String, Object> outputs =
ImmutableMap.<String, Object>builder()
.put("canaryJudgeResultId", canaryJudgeResultId)
.put("result", result)
.build();

return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.netflix.kayenta.canary.CanaryConfig;
import com.netflix.kayenta.canary.results.CanaryJudgeResult;
import com.netflix.kayenta.metrics.MetricSet;
import com.netflix.kayenta.metrics.MetricSetPair;
import lombok.AllArgsConstructor;
Expand All @@ -29,7 +30,8 @@
public enum ObjectType {
CANARY_CONFIG(new TypeReference<CanaryConfig>() {}, "canary_config", "canary_config.json"),
METRIC_SET_LIST(new TypeReference<List<MetricSet>>() {}, "metrics", "metric_sets.json"),
METRIC_SET_PAIR_LIST(new TypeReference<List<MetricSetPair>>() {}, "metric_pairs", "metric_set_pairs.json");
METRIC_SET_PAIR_LIST(new TypeReference<List<MetricSetPair>>() {}, "metric_pairs", "metric_set_pairs.json"),
CANARY_JUDGE_RESULT(new TypeReference<CanaryJudgeResult>() {}, "judge_results", "canary_judge_results.json");

@Getter
final TypeReference typeReference;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.kayenta.controllers;

import com.netflix.kayenta.canary.results.CanaryJudgeResult;
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.netflix.kayenta.security.CredentialsHelper;
import com.netflix.kayenta.storage.ObjectType;
import com.netflix.kayenta.storage.StorageService;
import com.netflix.kayenta.storage.StorageServiceRepository;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/canaryJudgeResult")
@Slf4j
public class CanaryJudgeResultController {

@Autowired
AccountCredentialsRepository accountCredentialsRepository;

@Autowired
StorageServiceRepository storageServiceRepository;

@ApiOperation(value = "Retrieve a canary judge result from object storage")
@RequestMapping(value = "/{canaryJudgeResultId:.+}", method = RequestMethod.GET)
public CanaryJudgeResult loadCanaryJudgeResult(@RequestParam(required = false) final String accountName,
@PathVariable String canaryJudgeResultId) {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to read canary judge result from bucket."));

return storageService.loadObject(resolvedAccountName, ObjectType.CANARY_JUDGE_RESULT, canaryJudgeResultId);
}

@ApiOperation(value = "Write a canary judge result to object storage")
@RequestMapping(consumes = "application/json", method = RequestMethod.POST)
public String storeCanaryJudgeResult(@RequestParam(required = false) final String accountName,
@RequestBody CanaryJudgeResult canaryJudgeResult) throws IOException {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to write canary judge result to bucket."));
String canaryJudgeResultId = UUID.randomUUID() + "";

storageService.storeObject(resolvedAccountName, ObjectType.CANARY_JUDGE_RESULT, canaryJudgeResultId, canaryJudgeResult);

return canaryJudgeResultId;
}

@ApiOperation(value = "Delete a canary judge result")
@RequestMapping(value = "/{canaryJudgeResultId:.+}", method = RequestMethod.DELETE)
public void deleteCanaryJudgeResult(@RequestParam(required = false) final String accountName,
@PathVariable String canaryJudgeResultId,
HttpServletResponse response) {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to delete canary judge result."));

storageService.deleteObject(resolvedAccountName, ObjectType.CANARY_JUDGE_RESULT, canaryJudgeResultId);

response.setStatus(HttpStatus.NO_CONTENT.value());
}

@ApiOperation(value = "Retrieve a list of canary judge result ids and timestamps")
@RequestMapping(method = RequestMethod.GET)
public List<Map<String, Object>> listAllCanaryJudgeResults(@RequestParam(required = false) final String accountName) {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
StorageService storageService =
storageServiceRepository
.getOne(resolvedAccountName)
.orElseThrow(() -> new IllegalArgumentException("No storage service was configured; unable to list all canary judge results."));

return storageService.listObjectKeys(resolvedAccountName, ObjectType.CANARY_JUDGE_RESULT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<MetricSet> loadMetricSetList(@RequestParam(required = false) final S
}

@ApiOperation(value = "Write a metric set list to object storage")
@RequestMapping(consumes = "application/context+json", method = RequestMethod.POST)
@RequestMapping(consumes = "application/json", method = RequestMethod.POST)
public String storeMetricSetList(@RequestParam(required = false) final String accountName,
@RequestBody List<MetricSet> metricSetList) throws IOException {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public List<MetricSetPair> loadMetricSetPairList(@RequestParam(required = false)
}

@ApiOperation(value = "Write a metric set pair list to object storage")
@RequestMapping(consumes = "application/context+json", method = RequestMethod.POST)
@RequestMapping(consumes = "application/json", method = RequestMethod.POST)
public String storeMetricSetPairList(@RequestParam(required = false) final String accountName,
@RequestBody List<MetricSetPair> metricSetPairList) throws IOException {
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
Expand Down
1 change: 1 addition & 0 deletions kayenta-web/src/main/resources/kayenta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ swagger:
patterns:
- /canary.*
- /canaryConfig.*
- /canaryJudgeResult.*
- /credentials.*
- /fetch.*
- /health
Expand Down

0 comments on commit c1df72a

Please sign in to comment.