Skip to content

Commit

Permalink
Added SplunkLog Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-agrawal committed Dec 6, 2023
1 parent 5d5b886 commit 786064c
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.netflix.spinnaker.gate.controllers;

import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/splunk")
@ConditionalOnProperty("splunk.log.enabled")
public class SplunkLogController {
private static final Logger log = LoggerFactory.getLogger(SplunkLogController.class);

// Sample baseURL = https://hostURL?paramsKey=paramsValue&index=CLUSTER_NAME%20pod=POD_NAME
@Value("${splunk.log.baseURL}")
private String baseURL;

@Value("${splunk.log.clusterName}")
private String clusterName;

@ApiOperation("Retrieve splunk logs")
@GetMapping("/logs/{podName}")
public ResponseEntity<byte[]> getSplunkLogs(@PathVariable("podName") String podName)
throws IOException {
String logUrl = baseURL.replace("CLUSTER_NAME", clusterName).replace("POD_NAME", podName);
log.info("Inside SplunkLogController - getSplunkLogs(), splunk Log URL : " + logUrl);

try (InputStream in = new URL(logUrl).openStream()) {
byte[] fileContents = in.readAllBytes();
return ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=log.zip")
.body(fileContents);
}
}
}

0 comments on commit 786064c

Please sign in to comment.