Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update each SDK readme with code examples #3172

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion sdks/aperture-csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,92 @@

# C# SDK for FluxNinja Aperture

C# SDK provides an easy way to integrate your .NET applications with
The C# SDK provides an easy way to integrate your .NET applications with
karansohi marked this conversation as resolved.
Show resolved Hide resolved
[FluxNinja Aperture](https://github.com/fluxninja/aperture).

## Usage

### Install

Run the command below to install the SDK:

```bash
dotnet add package ApertureSDK --version 2.23.1
```

### Create Aperture Client

The next step is to create an Aperture Client instance, for which, the address
of the organization created in Aperture Cloud and API key are needed. You can
locate both these details by clicking on the Aperture tab in the sidebar menu of
Aperture Cloud.

```csharp
var sdk = ApertureSdk
.Builder()
.SetAddress("ORGANIZATION.app.fluxninja.com:443")
.SetAgentApiKey("API_KEY")
.Build();
```
karansohi marked this conversation as resolved.
Show resolved Hide resolved

### Flow Functionality

The created instance can then be used to start a flow:

```csharp
// do some business logic to collect labels
var labels = new Dictionary<string, string>();
labels.Add("userId", "some_user_id");
labels.Add("userTier", "gold");
labels.Add("priority", "100");

var rampMode = false;
var flowTimeout = TimeSpan.FromSeconds(5);
var pms = new FeatureFlowParams(
"featureName",
labels,
rampMode,
flowTimeout,
new Grpc.Core.CallOptions(),
"test",
new RepeatedField<string> { "test" });

var flow = sdk.StartFlow(pms);

if (flow.ShouldRun())
{
// do actual work
Thread.Sleep(2000);
SimpleHandlePath((int)HttpStatusCode.OK, "Hello world!", response);
}
else
{
// handle flow rejection by Aperture Agent
flow.SetStatus(FlowStatus.Error);
SimpleHandlePath(flow.GetRejectionHttpStatusCode(), "REJECTED!", response);
}

var endResponse = flow.End();
if (endResponse.Error != null)
{
// handle end failure
log.Error("Failed to end flow: {e}", endResponse.Error);
}
else if (endResponse.FlowEndResponse != null)
{
// handle end success
log.Info("Ended flow with response: " + endResponse.FlowEndResponse.ToString());
}
karansohi marked this conversation as resolved.
Show resolved Hide resolved
```

The above code snippet is making `StartFlow` calls to Aperture. For this call,
it is important to specify the control point (`featureName` in the example) and
business labels that will be aligned with the policy created in Aperture Cloud.
For request prioritization use cases, it's important to set a higher gRPC
deadline. This parameter specifies the maximum duration a request can remain in
the queue. For each flow that is started, a `ShouldRun` decision is made,
determining whether to allow the request into the system or to rate limit it. In
this example, we only see log returns, but in a production environment, actual
business logic can be executed when a request is allowed. It is important to
make the `End` call made after processing each request, to send telemetry data
that would provide granular visibility for each flow.
12 changes: 10 additions & 2 deletions sdks/aperture-go/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
# Aperture-Go SDK

`aperture-go` is an SDK to interact with Aperture Agent. It allows flow control
functionality on fine-grained features inside service code.
The `aperture-go` SDK provides an easy way to integrate your go applications
with [FluxNinja Aperture](https://github.com/fluxninja/aperture). It allows flow
control functionality on fine-grained features inside service code.

## Usage

Run the command below to install the SDK:

```bash
go get github.com/fluxninja/aperture-go/v2
```

### ApertureClient Interface

The next step is to create an Aperture Client instance, for which, the address
of the organization created in Aperture Cloud and API key are needed. You can
locate both these details by clicking on the Aperture tab in the sidebar menu of
Aperture Cloud.

`ApertureClient` maintains a gRPC connection with Aperture Agent.
hdkshingala marked this conversation as resolved.
Show resolved Hide resolved

```go
Expand All @@ -29,7 +37,7 @@

### HTTP Middleware

`aperture-go` provides an HTTP middleware to be used with routers.

Check warning on line 40 in sdks/aperture-go/README.md

View workflow job for this annotation

GitHub Actions / languagetool

[LanguageTool] reported by reviewdog 🐶 This sentence does not start with an uppercase letter. (UPPERCASE_SENTENCE_START) Suggestions: `Provides` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/UPPERCASE_SENTENCE_START?lang=en-US Category: CASING Raw Output: sdks/aperture-go/README.md:40:14: This sentence does not start with an uppercase letter. (UPPERCASE_SENTENCE_START) Suggestions: `Provides` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/UPPERCASE_SENTENCE_START?lang=en-US Category: CASING

```go
// Create a new mux router
Expand All @@ -43,7 +51,7 @@

### gRPC Unary Interceptor

`aperture-go` provides a gRPC unary interceptor to be used with gRPC clients.

Check warning on line 54 in sdks/aperture-go/README.md

View workflow job for this annotation

GitHub Actions / languagetool

[LanguageTool] reported by reviewdog 🐶 This sentence does not start with an uppercase letter. (UPPERCASE_SENTENCE_START) Suggestions: `Provides` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/UPPERCASE_SENTENCE_START?lang=en-US Category: CASING Raw Output: sdks/aperture-go/README.md:54:14: This sentence does not start with an uppercase letter. (UPPERCASE_SENTENCE_START) Suggestions: `Provides` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/UPPERCASE_SENTENCE_START?lang=en-US Category: CASING

```go
// Create a new gRPC interceptor
Expand Down
100 changes: 99 additions & 1 deletion sdks/aperture-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,103 @@

# Java SDK for FluxNinja Aperture

Java SDK provides an easy way to integrate your Java applications with
The Java SDK provides an easy way to integrate your Java applications with
[FluxNinja Aperture](https://github.com/fluxninja/aperture).

## Usage

Follow this
[link](https://central.sonatype.com/artifact/com.fluxninja.aperture/aperture-java-core?smo=true)
to install the Aperture Java SDK.

### Create Aperture Client

The next step is to create an Aperture Client instance, for which, the address
of the organization created in Aperture Cloud and API key are needed. You can
locate both these details by clicking on the Aperture tab in the sidebar menu of
Aperture Cloud.

```java
import com.fluxninja.aperture.sdk.ApertureSDK;
import com.fluxninja.aperture.sdk.EndResponse;
import com.fluxninja.aperture.sdk.FeatureFlowParameters;
import com.fluxninja.aperture.sdk.Flow;
import com.fluxninja.aperture.sdk.FlowStatus;

String agentAddress = "ORGANIZATION.app.fluxninja.com:443";
String apiKey = "API_KEY";

ApertureSDK apertureSDK;
try {
apertureSDK =
ApertureSDK.builder()
.setAddress(agentAddress)
.setAPIKey(apiKey)
.useInsecureGrpc(insecureGrpc)
.setRootCertificateFile(rootCertFile)
.build();
} catch (IOException e) {
e.printStackTrace();
return;
}
```

### Flow Functionality

The created instance can then be used to start a flow:

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

// business logic produces labels
labels.put("userId", "some_user_id");
labels.put("userTier", "gold");
labels.put("priority", "100");

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 {
EndResponse endResponse = flow.end();
if (endResponse.getError() != null) {
logger.error("Error ending flow", endResponse.getError());
}

logger.info("Flow End response: {}", endResponse.getFlowEndResponse());
}
```

The above code snippet is making `startFlow` calls to Aperture. For this call,
it is important to specify the control point (`featureName` in the example) and
business labels that will be aligned with the policy created in Aperture Cloud.
For request prioritization use cases, it's important to set a higher gRPC
deadline. This parameter specifies the maximum duration a request can remain in
the queue. For each flow that is started, a `shouldRun` decision is made,
determining whether to allow the request into the system or to rate limit it. In
this example, we only see response returns, but in a production environment,
actual business logic can be executed when a request is allowed. It is important
to make the `end` call made after processing each request, to send telemetry
data that would provide granular visibility for each flow.
81 changes: 78 additions & 3 deletions sdks/aperture-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

# Aperture JavaScript SDK

`aperture-js` is an SDK to interact with [Aperture](https://docs.fluxninja.com)
Agent. It allows flow control functionality on fine-grained features inside
service code.
The `aperture-js` SDK provides an easy way to integrate your javascript
applications with [FluxNinja Aperture](https://github.com/fluxninja/aperture).
It allows flow control functionality on fine-grained features inside service
code.

Refer [documentation](https://docs.fluxninja.com/sdk/javascript/) for more
karansohi marked this conversation as resolved.
Show resolved Hide resolved
details.
Expand Down Expand Up @@ -44,3 +45,77 @@ details.
- [KeyDeleteResponse](docs/interfaces/KeyDeleteResponse.md)
- [KeyLookupResponse](docs/interfaces/KeyLookupResponse.md)
- [KeyUpsertResponse](docs/interfaces/KeyUpsertResponse.md)

## Usage

### Install SDK

Run the command below to install the SDK:

```bash
npm install @fluxninja/aperture-js
```

### Create Aperture Client

The next step is to create an Aperture Client instance, for which, the address
of the organization created in Aperture Cloud and API key are needed. You can
locate both these details by clicking on the Aperture tab in the sidebar menu of
Aperture Cloud.

```typescript
import { ApertureClient } from "@fluxninja/aperture-js";

// Create aperture client
export const apertureClient = new ApertureClient({
address: "ORGANIZATION.app.fluxninja.com:443",
apiKey: "API_KEY",
});
```

### Flow Functionality

The created instance can then be used to start a flow:

```typescript
async function handleRequestRateLimit(req: Request, res: Response) {
// Start a flow by passing control point and business labels
const flow = await apertureClient.startFlow("awesomeFeature", {
labels: {
limit_key: "some_user_id",
},
grpcCallOptions: {
deadline: Date.now() + 300, // ms
},
});

if (flow.shouldRun()) {
// Add business logic to process incoming request
console.log("Request accepted. Processing...");
const resString = "foo";
res.send({ message: resString });
} else {
console.log("Request rate-limited. Try again later.");
// Handle flow rejection
flow.setStatus(FlowStatus.Error);
res.status(429).send({ message: "Too many requests" });
}

flow.end();
}
```

The above code snippet is making `startFlow` calls to Aperture. For this call,
it is important to specify the control point (`awesomeFeature` in the example)
and business labels that will be aligned with the policy created in Aperture
Cloud. For request prioritization use cases, it's important to set a higher gRPC
deadline. This parameter specifies the maximum duration a request can remain in
the queue. For each flow that is started, a `shouldRun` decision is made,
determining whether to allow the request into the system or to rate limit it. In
this example, we only see log returns, but in a production environment, actual
business logic can be executed when a request is allowed. It is important to
make the `end` call made after processing each request, to send telemetry data
that would provide granular visibility for each flow.

For more context on using the Aperture JavaScript SDK to set feature control
points, refer to the [example app][example] available in the repository.
Loading
Loading