Skip to content

Instantly share code, notes, and snippets.

@grdryn
Last active February 28, 2023 16:56
Show Gist options
  • Save grdryn/8374c5d32e452ca2ffedd41d22380e74 to your computer and use it in GitHub Desktop.
Save grdryn/8374c5d32e452ca2ffedd41d22380e74 to your computer and use it in GitHub Desktop.
JBang script to get metrics from the OpenShift streams metrics federation endpoint
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.5.0
//DEPS com.redhat.cloud:kafka-management-sdk:0.31.0
// Usage:
// jbang https://gist.github.com/grdryn/8374c5d32e452ca2ffedd41d22380e74 --token $(rhoas authtoken) --instance $(rhoas status -o json | jq -r '.kafka.id')
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import java.util.concurrent.Callable;
import com.openshift.cloud.api.kas.invoker.*;
import com.openshift.cloud.api.kas.invoker.auth.*;
import com.openshift.cloud.api.kas.models.*;
import com.openshift.cloud.api.kas.DefaultApi;
@Command(name = "FetchMetrics", mixinStandardHelpOptions = true, version = "FetchMetrics 0.1",
description = "FetchMetrics made with jbang")
class FetchMetrics implements Callable<Integer> {
@Option(names = { "-i", "--instance" }, description = "The Kafka instance ID")
private String instanceID;
@Option(names = { "-t", "--token" }, description = "The OAuth Bearer token to use with the API")
private String bearerToken;
@Option(names = { "--api-gateway" }, description = "URL of the API gateway (default https://api.openshift.com)")
private String apiGateway = "https://api.openshift.com";
public static void main(String... args) {
int exitCode = new CommandLine(new FetchMetrics()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath(apiGateway);
// Configure HTTP bearer authorization: Bearer
HttpBearerAuth Bearer = (HttpBearerAuth) defaultClient.getAuthentication("Bearer");
Bearer.setBearerToken(bearerToken);
DefaultApi apiInstance = new DefaultApi(defaultClient);
System.out.println(apiInstance.federateMetrics(instanceID));
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment