Chapter 3
The profiler turns source declarations into profile YAML.
Declare what an app needs in the language it is written in, run the profiler, and get a profile. Pick your language.
1. Add the packages
Add the generated binding modules like any Go module. The Go profiler is built into rc.
These two are the packages for Common Integrations and Env Configuration. Every extension has packages in all four languages: see the Binding packages table.
go get runtimeconditions.io/x/rc/common runtimeconditions.io/x/rc/env
2. Declare what the app needs
Each declaration is a call built from generated types, so the compiler checks every field. The calls do nothing when the app runs. The app below needs an HTTP API, with the environment variable that holds its URL, and a Redis cache, with two ways to find it.
package main
import (
"runtimeconditions.io/x/rc/common"
"runtimeconditions.io/x/rc/env"
)
func declaration() {
_ = common.Api(
common.Http{
Spec: &common.Spec{Format: common.FormatOpenapi, Uri: "catalog://api/default/todos-api"},
Operations: common.HttpOperations{
{Method: common.MethodGet, Path: "/todos/{id}"},
},
},
env.HttpConfiguration{
Env: env.HttpConfigurationEnv{
{Name: "TODOS_API_URL", Property: env.HttpConfigurationEnvPropertyBaseUrl},
},
},
)
_ = common.Cache(
common.KeyValue{Engine: &[]common.KeyValueEngine{common.KeyValueEngineRedis}[0]},
env.KeyValueConfiguration{
Alternatives: env.KeyValueConfigurationAlternatives{
{Env: env.KeyValueConfigurationAlternativesEnv{
{Name: "REDIS_URL", Property: env.KeyValueConfigurationAlternativesEnvPropertyUrl},
}},
{Env: env.KeyValueConfigurationAlternativesEnv{
{Name: "REDIS_HOST", Property: env.KeyValueConfigurationAlternativesEnvPropertyHostname},
{Name: "REDIS_PORT", Property: env.KeyValueConfigurationAlternativesEnvPropertyPort},
}},
},
},
)
}
3. Use the variables in normal code
The profile records the names TODOS_API_URL and REDIS_URL, not their values. The app reads them as it always did, and the platform supplies them later.
func checkTodosAPI(ctx context.Context) error {
baseURL := strings.TrimRight(os.Getenv("TODOS_API_URL"), "/")
if baseURL == "" {
return errors.New("TODOS_API_URL is not set")
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/todos/1", nil)
if err != nil {
return err
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return fmt.Errorf("todos-api request failed: %w", err)
}
defer response.Body.Close()
return nil
}
4. Run the profiler
These commands use the rc CLI. The rc CLI chapter shows how to install it and lists every command.
rc profile generate \
--dir . \
--name request-logger-http \
--workload-uri github.com/runtimeconditions/rc-demos/apps/request-logger-http \
--workload-version v0.1.0 \
--out profile.yaml
The profiler parses and type-checks your module, so it needs a Go toolchain and your dependencies downloaded. It reads literals, named constants, and typed composite literals. A value it cannot determine fails with the file and line, never with part of a Condition.
1. Add the packages
Add the binding packages from Maven Central, and install the profiler. It needs a JDK 17 or newer, not a JRE.
These two are the packages for Common Integrations and Env Configuration. Every extension has packages in all four languages: see the Binding packages table.
# pom.xml
<dependency>
<groupId>io.runtimeconditions.bindings</groupId>
<artifactId>runtimeconditions-common-integrations</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>io.runtimeconditions.bindings</groupId>
<artifactId>runtimeconditions-env-configuration</artifactId>
<version>0.1.0</version>
</dependency>
# the profiler
brew install runtimeconditions/tools/rc-profiler-java
2. Declare what the app needs
Each declaration is a call built from generated types, so the compiler checks every field. The calls do nothing when the app runs. The app below needs an HTTP API, with the environment variable that holds its URL, and a Redis cache, with two ways to find it.
package demo;
import io.runtimeconditions.bindings.commonintegrations.Bindings;
import io.runtimeconditions.bindings.commonintegrations.Format;
import io.runtimeconditions.bindings.commonintegrations.Http;
import io.runtimeconditions.bindings.commonintegrations.HttpOperations;
import io.runtimeconditions.bindings.commonintegrations.HttpOperationsItem;
import io.runtimeconditions.bindings.commonintegrations.KeyValue;
import io.runtimeconditions.bindings.commonintegrations.KeyValueEngine;
import io.runtimeconditions.bindings.commonintegrations.Method;
import io.runtimeconditions.bindings.commonintegrations.Spec;
import io.runtimeconditions.bindings.envconfiguration.HttpConfiguration;
import io.runtimeconditions.bindings.envconfiguration.HttpConfigurationEnv;
import io.runtimeconditions.bindings.envconfiguration.HttpConfigurationEnvItem;
import io.runtimeconditions.bindings.envconfiguration.HttpConfigurationEnvItemProperty;
import io.runtimeconditions.bindings.envconfiguration.KeyValueConfiguration;
import io.runtimeconditions.bindings.envconfiguration.KeyValueConfigurationAlternatives;
import io.runtimeconditions.bindings.envconfiguration.KeyValueConfigurationAlternativesItem;
import io.runtimeconditions.bindings.envconfiguration.KeyValueConfigurationAlternativesItemEnv;
import io.runtimeconditions.bindings.envconfiguration.KeyValueConfigurationAlternativesItemEnvItem;
import io.runtimeconditions.bindings.envconfiguration.KeyValueConfigurationAlternativesItemEnvItemProperty;
import java.util.List;
final class Conditions {
static final Object TODOS_API = Bindings.api(
new Http(
new HttpOperations(List.of(new HttpOperationsItem(Method.GET, "/todos/{id}", null, null))),
new Spec(Format.OPENAPI, "catalog://api/default/todos-api", null)),
new HttpConfiguration(
null,
new HttpConfigurationEnv(List.of(
new HttpConfigurationEnvItem(
"TODOS_API_URL", HttpConfigurationEnvItemProperty.BASE_URL, null, null)))));
static final Object REQUEST_CACHE = Bindings.cache(
new KeyValue(KeyValueEngine.REDIS),
new KeyValueConfiguration(
new KeyValueConfigurationAlternatives(List.of(
new KeyValueConfigurationAlternativesItem(new KeyValueConfigurationAlternativesItemEnv(List.of(
new KeyValueConfigurationAlternativesItemEnvItem(
"REDIS_URL", KeyValueConfigurationAlternativesItemEnvItemProperty.URL, null, null)))),
new KeyValueConfigurationAlternativesItem(new KeyValueConfigurationAlternativesItemEnv(List.of(
new KeyValueConfigurationAlternativesItemEnvItem(
"REDIS_HOST", KeyValueConfigurationAlternativesItemEnvItemProperty.HOSTNAME, null, null),
new KeyValueConfigurationAlternativesItemEnvItem(
"REDIS_PORT", KeyValueConfigurationAlternativesItemEnvItemProperty.PORT, null, null)))))),
null));
}
3. Use the variables in normal code
The profile records the names TODOS_API_URL and REDIS_URL, not their values. The app reads them as it always did, and the platform supplies them later.
String baseUrl = System.getenv("TODOS_API_URL");
if (baseUrl == null || baseUrl.isBlank()) {
throw new IllegalStateException("TODOS_API_URL is not set");
}
HttpRequest request = HttpRequest.newBuilder(URI.create(baseUrl + "/todos/1")).build();
4. Run the profiler
These commands use the rc CLI. The rc CLI chapter shows how to install it and lists every command.
rc profile java generate \
--project . \
--resolve-build-classpath \
--name request-logger-http \
--workload-uri github.com/runtimeconditions/rc-demos/apps/request-logger-http \
--workload-version v0.1.0 \
--out profile.yaml
The profiler needs to know where the binding jars are. --resolve-build-classpath asks your Maven or Gradle build, which runs your build, so it only happens when you ask. Without it, pass the jars with --classpath a.jar:b.jar. The profiler reads your source with the JDK's parser and never compiles or runs it.
1. Add the packages
Install the profiler and the bindings into the same virtual environment, because the profiler finds the bindings through that environment. Do not use pipx, which would give it an environment of its own.
These two are the packages for Common Integrations and Env Configuration. Every extension has packages in all four languages: see the Binding packages table.
python3 -m venv .venv
.venv/bin/pip install runtimeconditions-profiler \
runtimeconditions-common-integrations \
runtimeconditions-env-configuration
2. Declare what the app needs
Each declaration is a call built from generated types, so the compiler checks every field. The calls do nothing when the app runs. The app below needs an HTTP API, with the environment variable that holds its URL, and a Redis cache, with two ways to find it.
from runtimeconditions_common_integrations import (
Format,
Http,
HttpOperationsItem,
KeyValue,
KeyValueEngine,
Method,
Spec,
api,
cache,
)
from runtimeconditions_env_configuration import (
HttpConfiguration,
HttpConfigurationEnvItem,
HttpConfigurationEnvItemProperty,
KeyValueConfiguration,
KeyValueConfigurationAlternativesItem,
KeyValueConfigurationAlternativesItemEnvItem,
KeyValueConfigurationAlternativesItemEnvItemProperty,
)
api(
Http(
spec=Spec(format=Format.OPENAPI, uri="catalog://api/default/todos-api"),
operations=[HttpOperationsItem(method=Method.GET, path="/todos/{id}")],
),
HttpConfiguration(
env=[
HttpConfigurationEnvItem(
name="TODOS_API_URL",
property=HttpConfigurationEnvItemProperty.BASE_URL,
)
]
),
)
cache(
KeyValue(engine=KeyValueEngine.REDIS),
KeyValueConfiguration(
alternatives=[
KeyValueConfigurationAlternativesItem(
env=[
KeyValueConfigurationAlternativesItemEnvItem(
name="REDIS_URL",
property=KeyValueConfigurationAlternativesItemEnvItemProperty.URL,
)
]
),
KeyValueConfigurationAlternativesItem(
env=[
KeyValueConfigurationAlternativesItemEnvItem(
name="REDIS_HOST",
property=KeyValueConfigurationAlternativesItemEnvItemProperty.HOSTNAME,
),
KeyValueConfigurationAlternativesItemEnvItem(
name="REDIS_PORT",
property=KeyValueConfigurationAlternativesItemEnvItemProperty.PORT,
),
]
),
]
),
)
3. Use the variables in normal code
The profile records the names TODOS_API_URL and REDIS_URL, not their values. The app reads them as it always did, and the platform supplies them later.
import os
import urllib.request
base_url = os.environ["TODOS_API_URL"].rstrip("/")
urllib.request.urlopen(f"{base_url}/todos/1")
4. Run the profiler
These commands use the rc CLI. The rc CLI chapter shows how to install it and lists every command.
export PATH="$PWD/.venv/bin:$PATH"
rc profile python generate \
--project . \
--name request-logger-http \
--workload-uri github.com/runtimeconditions/rc-demos/apps/request-logger-http \
--workload-version v0.1.0 \
--out profile.yaml
Python 3.11 or newer. The profiler reads your source and never imports the bindings or runs the app.
1. Add the packages
Add the binding packages to your project, and install the profiler. It needs Node.js.
These two are the packages for Common Integrations and Env Configuration. Every extension has packages in all four languages: see the Binding packages table.
npm install @runtimeconditions/common-integrations @runtimeconditions/env-configuration
npm install --global @runtimeconditions/typescript-profiler
2. Declare what the app needs
Each declaration is a call built from generated types, so the compiler checks every field. The calls do nothing when the app runs. The app below needs an HTTP API, with the environment variable that holds its URL, and a Redis cache, with two ways to find it.
import {
api,
cache,
Format,
Http,
HttpOperationsItem,
KeyValue,
KeyValueEngine,
Method,
Spec,
} from "@runtimeconditions/common-integrations";
import {
HttpConfiguration,
HttpConfigurationEnvItem,
HttpConfigurationEnvItemProperty,
KeyValueConfiguration,
KeyValueConfigurationAlternativesItem,
KeyValueConfigurationAlternativesItemEnvItem,
KeyValueConfigurationAlternativesItemEnvItemProperty,
} from "@runtimeconditions/env-configuration";
api(
new Http({
spec: new Spec({ format: Format.Openapi, uri: "catalog://api/default/todos-api" }),
operations: [new HttpOperationsItem({ method: Method.Get, path: "/todos/{id}" })],
}),
new HttpConfiguration({
env: [
new HttpConfigurationEnvItem({
name: "TODOS_API_URL",
property: HttpConfigurationEnvItemProperty.BaseUrl,
}),
],
}),
);
cache(
new KeyValue({ engine: KeyValueEngine.Redis }),
new KeyValueConfiguration({
alternatives: [
new KeyValueConfigurationAlternativesItem({
env: [
new KeyValueConfigurationAlternativesItemEnvItem({
name: "REDIS_URL",
property: KeyValueConfigurationAlternativesItemEnvItemProperty.Url,
}),
],
}),
new KeyValueConfigurationAlternativesItem({
env: [
new KeyValueConfigurationAlternativesItemEnvItem({
name: "REDIS_HOST",
property: KeyValueConfigurationAlternativesItemEnvItemProperty.Hostname,
}),
new KeyValueConfigurationAlternativesItemEnvItem({
name: "REDIS_PORT",
property: KeyValueConfigurationAlternativesItemEnvItemProperty.Port,
}),
],
}),
],
}),
);
3. Use the variables in normal code
The profile records the names TODOS_API_URL and REDIS_URL, not their values. The app reads them as it always did, and the platform supplies them later.
const baseUrl = (process.env.TODOS_API_URL ?? "").replace(/\/$/, "");
if (!baseUrl) {
throw new Error("TODOS_API_URL is not set");
}
await fetch(`${baseUrl}/todos/1`);
4. Run the profiler
These commands use the rc CLI. The rc CLI chapter shows how to install it and lists every command.
rc profile typescript generate \
--project . \
--name request-logger-http \
--workload-uri github.com/runtimeconditions/rc-demos/apps/request-logger-http \
--workload-version v0.1.0 \
--out profile.yaml
The profiler reads your TypeScript source and never runs it.
The profile every language writes
The four profilers write the same profile for these declarations. A profiler writes the extensions in the short form, the extension id with its version, and it does not name the Conditions: name is optional, and a hand-written profile can add one. Without --out the profile is printed, and a failed run leaves an existing output file alone.
apiVersion: runtimeconditions.io/v1alpha1
kind: RuntimeConditionsProfile
metadata:
name: request-logger-http
workload:
uri: github.com/runtimeconditions/rc-demos/apps/request-logger-http
version: v0.1.0
extensions:
- https://runtimeconditions.io/extensions/rc/common-integrations:v1alpha2
- https://runtimeconditions.io/extensions/rc/env-configuration:v1alpha2
conditions:
- kind: api
interface:
type: http
operations:
- method: GET
path: /todos/{id}
spec:
format: openapi
uri: catalog://api/default/todos-api
configuration:
env:
- name: TODOS_API_URL
property: baseUrl
- kind: cache
interface:
type: key_value
engine: redis
configuration:
alternatives:
- env:
- name: REDIS_URL
property: url
- env:
- name: REDIS_HOST
property: hostname
- name: REDIS_PORT
property: port