Skip to content

Commit

Permalink
Update from fluxninja/aperture
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxninjaops committed Dec 1, 2023
1 parent 65a9e11 commit 08ff7f4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class ArmeriaServer {
public static final String DEFAULT_INSECURE_GRPC = "true";
public static final String DEFAULT_ROOT_CERT = "";

// START: ArmeriaCreateHTTPService

public static HttpService createHelloHTTPService() {
return new AbstractHttpService() {
@Override
Expand All @@ -30,6 +32,7 @@ protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) {
}
};
}
// END: ArmeriaCreateHTTPService

public static HttpService createHealthService() {
return new AbstractHttpService() {
Expand Down Expand Up @@ -71,25 +74,30 @@ public static void main(String[] args) {

String rootCertFile = getEnv("APERTURE_ROOT_CERTIFICATE_FILE", DEFAULT_ROOT_CERT);

// START: ArmeriaCreateApertureSDK
ApertureSDK apertureSDK;
try {
apertureSDK =
ApertureSDK.builder()
.setAddress(agentHost)
.setAPIKey(apiKey)
.useInsecureGrpc(insecureGrpc)
.addIgnoredPaths("/health,/connected")
.useInsecureGrpc(insecureGrpc) // Optional: Defaults to true
.setRootCertificateFile(rootCertFile)
.build();
} catch (IOException e) {
e.printStackTrace();
return;
}
// END: ArmeriaCreateApertureSDK

ServerBuilder serverBuilder = Server.builder();
serverBuilder.http(Integer.parseInt(appPort));
serverBuilder.service("/notsuper", createHelloHTTPService());
serverBuilder.service("/health", createHealthService());
serverBuilder.service("/connected", createConnectedHTTPService());

// START: ArmeriadecorateService
ApertureHTTPService decoratedService =
createHelloHTTPService()
.decorate(
Expand All @@ -99,6 +107,7 @@ public static void main(String[] args) {
rampMode,
Duration.ofMillis(1000)));
serverBuilder.service("/super", decoratedService);
// END: ArmeriadecorateService

Server server = serverBuilder.build();
CompletableFuture<Void> future = server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,25 @@ public ServerInitializer(
this.rootCertFile = rootCertFile;
}

// START: NettyInitChannel

@Override
protected void initChannel(Channel ch) {

// START: NettyCreateSDK
try {
sdk =
ApertureSDK.builder()
.setAddress(this.agentAddress)
.setAPIKey(this.agentAPIKey)
.useInsecureGrpc(insecureGrpc)
.useInsecureGrpc(insecureGrpc) // Optional: Defaults to true
.setRootCertificateFile(rootCertFile)
.addIgnoredPaths("/health,/connected")
.build();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
// END: NettyCreateSDK

ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
Expand All @@ -61,4 +67,5 @@ protected void initChannel(Channel ch) {
pipeline.addLast(new ApertureServerHandler(sdk, controlPointName, rampMode, flowTimeout));
pipeline.addLast(new HelloWorldHandler());
}
// END: NettyInitChannel
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public static void main(String[] args) {

final ManagedChannel channel = ManagedChannelBuilder.forTarget(agentAddress).build();

// START: StandaloneExampleSDKInit

ApertureSDK apertureSDK;
try {
apertureSDK =
Expand All @@ -70,6 +72,8 @@ public static void main(String[] args) {
return;
}

// END: StandaloneExampleSDKInit

String featureName = System.getenv("APERTURE_FEATURE_NAME");
if (featureName == null) {
featureName = DEFAULT_FEATURE_NAME;
Expand All @@ -82,6 +86,7 @@ public static void main(String[] args) {
}
Spark.port(Integer.parseInt(appPort));
Spark.get("/super", app::handleSuperAPI);
Spark.get("/super2", app::handleSuper2API);
Spark.get("/connected", app::handleConnectedAPI);
Spark.get("/health", app::handleHealthAPI);
}
Expand Down Expand Up @@ -124,6 +129,50 @@ private String handleSuperAPI(spark.Request req, spark.Response res) {
return "";
}

private String handleSuper2API(spark.Request req, spark.Response res) {

// START: StandaloneExampleFlow

Map<String, String> labels = new HashMap<>();

// business logic produces labels
labels.put("key", "value");

Boolean rampMode = false;

FeatureFlowParameters params =
FeatureFlowParameters.newBuilder("featureName")
.setExplicitLabels(labels)
.setRampMode(rampMode)
.setFlowTimeout(Duration.ofMillis(1000))
.build();
// StartFlow performs a flowcontrolv1.Check call to Aperture. It returns a Flow.
Flow flow = this.apertureSDK.startFlow(params);

// See whether flow was accepted by Aperture.
try {
if (flow.shouldRun()) {
// do actual work
res.status(202);
} else {
// handle flow rejection by Aperture
res.status(flow.getRejectionHttpStatusCode());
}
} catch (Exception e) {
// Flow Status captures whether the feature captured by the Flow was
// successful or resulted in an error. When not explicitly set,
// the default value is FlowStatus.OK .
flow.setStatus(FlowStatus.Error);
logger.error("Error in flow execution", e);
} finally {
flow.end();
}

// END: StandaloneExampleFlow

return "";
}

private String handleConnectedAPI(spark.Request req, spark.Response res) {
ConnectivityState state = this.channel.getState(true);
// if (state.toString() != "READY") {
Expand Down

0 comments on commit 08ff7f4

Please sign in to comment.