From 08ff7f46282f9048b3325b2bfd056f054340302b Mon Sep 17 00:00:00 2001 From: CircleCI Job Date: Fri, 1 Dec 2023 16:21:17 +0000 Subject: [PATCH] Update from fluxninja/aperture --- .../com/fluxninja/example/ArmeriaServer.java | 11 ++++- .../fluxninja/example/ServerInitializer.java | 9 +++- .../main/java/com/fluxninja/example/App.java | 49 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/examples/armeria-example/src/main/java/com/fluxninja/example/ArmeriaServer.java b/examples/armeria-example/src/main/java/com/fluxninja/example/ArmeriaServer.java index 7938020..246e8a3 100644 --- a/examples/armeria-example/src/main/java/com/fluxninja/example/ArmeriaServer.java +++ b/examples/armeria-example/src/main/java/com/fluxninja/example/ArmeriaServer.java @@ -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 @@ -30,6 +32,7 @@ protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) { } }; } + // END: ArmeriaCreateHTTPService public static HttpService createHealthService() { return new AbstractHttpService() { @@ -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( @@ -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 future = server.start(); diff --git a/examples/netty-example/src/main/java/com/fluxninja/example/ServerInitializer.java b/examples/netty-example/src/main/java/com/fluxninja/example/ServerInitializer.java index 5ae5bce..88ec207 100644 --- a/examples/netty-example/src/main/java/com/fluxninja/example/ServerInitializer.java +++ b/examples/netty-example/src/main/java/com/fluxninja/example/ServerInitializer.java @@ -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()); @@ -61,4 +67,5 @@ protected void initChannel(Channel ch) { pipeline.addLast(new ApertureServerHandler(sdk, controlPointName, rampMode, flowTimeout)); pipeline.addLast(new HelloWorldHandler()); } + // END: NettyInitChannel } diff --git a/examples/standalone-example/src/main/java/com/fluxninja/example/App.java b/examples/standalone-example/src/main/java/com/fluxninja/example/App.java index 1869ab6..defb105 100644 --- a/examples/standalone-example/src/main/java/com/fluxninja/example/App.java +++ b/examples/standalone-example/src/main/java/com/fluxninja/example/App.java @@ -56,6 +56,8 @@ public static void main(String[] args) { final ManagedChannel channel = ManagedChannelBuilder.forTarget(agentAddress).build(); + // START: StandaloneExampleSDKInit + ApertureSDK apertureSDK; try { apertureSDK = @@ -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; @@ -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); } @@ -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 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") {