diff --git a/docs/services/managed-prometheus.md b/docs/services/managed-prometheus.md
index db2697f8a1..dd272f1f6e 100644
--- a/docs/services/managed-prometheus.md
+++ b/docs/services/managed-prometheus.md
@@ -20,7 +20,13 @@ definitions, scrapers and logging configurations are not implemented.
ingestion/query endpoint.
- The `alias` parameter of `ListWorkspaces` is a **prefix** filter, matching the provider's
`alias_prefix` data-source argument.
-- `clientToken` on `CreateWorkspace` is accepted and ignored: creates are not deduplicated.
+- `CreateWorkspace` deduplicates requests with the same case-sensitive `clientToken` in
+ the calling account and region. Tokens accept 1–64 printable ASCII characters excluding
+ spaces. A retry returns the original response; changing the alias, tags or KMS key with
+ the same token returns `ConflictException` (409). Receipts use the configured storage
+ mode and are committed atomically with the workspace in the same store. They remain
+ until that state is cleared, including after workspace deletion; token
+ expiry is not emulated. Requests without a token create independently.
- `kmsKeyArn` is stored and echoed back but no encryption is performed.
- A rule groups namespace is **ACTIVE from birth** for the same reason, so the provider's
create (`CREATING` → `ACTIVE`) and update (`UPDATING` → `ACTIVE`) waiters complete on their
@@ -38,8 +44,8 @@ definitions, scrapers and logging configurations are not implemented.
- Namespace names are validated against AMP's documented constraints (1 to 128 characters
matching `.*[0-9A-Za-z][-.0-9A-Z_a-z]*.*`). Floci additionally rejects `/`, because the
namespace is addressed as a single path segment.
-- `clientToken` on `CreateRuleGroupsNamespace` is accepted and ignored, the same as on
- `CreateWorkspace`: creates are not deduplicated.
+- `clientToken` on `CreateRuleGroupsNamespace` is accepted and ignored: namespace creates
+ are not deduplicated.
- The shared tags dispatcher rejects an ARN whose service, region or account does not match the
request with `ValidationException` (400), rather than resolving it against the request's own
region.
diff --git a/pom.xml b/pom.xml
index 564145197a..ac51810f64 100644
--- a/pom.xml
+++ b/pom.xml
@@ -366,6 +366,12 @@
${aws.sdk.version}
test
+
+ software.amazon.awssdk
+ amp
+ ${aws.sdk.version}
+ test
+
software.amazon.awssdk
bedrock
diff --git a/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java b/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java
index 1638a74979..82896a4313 100644
--- a/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/aps/ApsController.java
@@ -60,10 +60,10 @@ public class ApsController {
String alias = asString(body.get("alias"), "alias");
String kmsKeyArn = asString(body.get("kmsKeyArn"), "kmsKeyArn");
Map tags = asStringMap(body.get("tags"), "tags");
- // clientToken is accepted and ignored: creates are not deduplicated.
+ String clientToken = asString(body.get("clientToken"), "clientToken");
PrometheusWorkspace workspace =
- service.createWorkspace(regionResolver.resolveRegion(headers), alias, tags, kmsKeyArn);
+ service.createWorkspace(regionResolver.resolveRegion(headers), alias, tags, kmsKeyArn, clientToken);
ObjectNode response = objectMapper.createObjectNode();
response.put("workspaceId", workspace.getWorkspaceId());
diff --git a/src/main/java/io/github/hectorvent/floci/services/aps/ApsService.java b/src/main/java/io/github/hectorvent/floci/services/aps/ApsService.java
index 0581c9ca2d..23fd927891 100644
--- a/src/main/java/io/github/hectorvent/floci/services/aps/ApsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/aps/ApsService.java
@@ -19,6 +19,7 @@ import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.UUID;
import java.util.regex.Pattern;
@@ -31,6 +32,7 @@ public class ApsService implements TagHandler {
private static final int MAX_PAGE = 1000;
private static final int MAX_NAMESPACE_NAME_LENGTH = 128;
private static final Pattern NAMESPACE_NAME = Pattern.compile(".*[0-9A-Za-z][-.0-9A-Z_a-z]*.*");
+ private static final Pattern CLIENT_TOKEN = Pattern.compile("[!-~]{1,64}");
private final StorageBackend storage;
private final StorageBackend namespaceStorage;
@@ -47,6 +49,29 @@ public class ApsService implements TagHandler {
public PrometheusWorkspace createWorkspace(String region, String alias, Map tags,
String kmsKeyArn) {
+ return createWorkspace(region, alias, tags, kmsKeyArn, null);
+ }
+
+ // ponytail: serialize token claims; use per-token locks if workspace-create throughput matters.
+ public synchronized PrometheusWorkspace createWorkspace(String region, String alias, Map tags,
+ String kmsKeyArn, String clientToken) {
+ if (clientToken != null && !CLIENT_TOKEN.matcher(clientToken).matches()) {
+ throw new AwsException("ValidationException",
+ "clientToken must contain 1 to 64 printable ASCII characters without spaces.", 400);
+ }
+ Map requestedTags = tags != null ? tags : Map.of();
+ if (clientToken != null) {
+ PrometheusWorkspace receipt = storage.get(workspaceRequestKey(region, clientToken)).orElse(null);
+ if (receipt != null) {
+ if (!Objects.equals(stripAlias(alias), receipt.getAlias())
+ || !Objects.equals(kmsKeyArn, receipt.getKmsKeyArn())
+ || !requestedTags.equals(receipt.getTags())) {
+ throw new AwsException("ConflictException",
+ "clientToken has already been used with different workspace parameters.", 409);
+ }
+ return copyWorkspace(receipt);
+ }
+ }
String workspaceId = "ws-" + UUID.randomUUID();
String arn = regionResolver.buildArn("aps", region, "workspace/" + workspaceId);
@@ -66,9 +91,29 @@ public class ApsService implements TagHandler {
workspace.getTags().putAll(tags);
}
- storage.put(key(region, workspaceId), workspace);
+ PrometheusWorkspace response = clientToken != null ? copyWorkspace(workspace) : workspace;
+ if (clientToken != null) {
+ // One commit keeps the receipt and resource together in snapshots and WAL replay.
+ storage.putAllAtomically(Map.of(key(region, workspaceId), workspace,
+ workspaceRequestKey(region, clientToken), response));
+ } else {
+ storage.put(key(region, workspaceId), workspace);
+ }
LOG.infov("Created AMP workspace: {0} in {1}", workspaceId, region);
- return workspace;
+ return clientToken != null ? copyWorkspace(response) : workspace;
+ }
+
+ private static PrometheusWorkspace copyWorkspace(PrometheusWorkspace source) {
+ PrometheusWorkspace copy = new PrometheusWorkspace();
+ copy.setWorkspaceId(source.getWorkspaceId());
+ copy.setAlias(source.getAlias());
+ copy.setArn(source.getArn());
+ copy.setStatus(source.getStatus());
+ copy.setPrometheusEndpoint(source.getPrometheusEndpoint());
+ copy.setCreatedAt(source.getCreatedAt());
+ copy.setKmsKeyArn(source.getKmsKeyArn());
+ copy.setTags(source.getTags());
+ return copy;
}
public PrometheusWorkspace describeWorkspace(String region, String workspaceId) {
@@ -263,6 +308,11 @@ public class ApsService implements TagHandler {
return region + "::";
}
+ private static String workspaceRequestKey(String region, String clientToken) {
+ // Request regions cannot contain ':', keeping receipts outside every workspace prefix.
+ return ":CreateWorkspace::" + region + "::" + clientToken;
+ }
+
private static String namespaceKey(String region, String workspaceId, String name) {
return namespaceKeyPrefix(region, workspaceId) + name;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/aps/ApsControllerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/aps/ApsControllerIntegrationTest.java
index 232397d1a0..e17d514f13 100644
--- a/src/test/java/io/github/hectorvent/floci/services/aps/ApsControllerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/aps/ApsControllerIntegrationTest.java
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
+import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
@@ -29,8 +30,8 @@ class ApsControllerIntegrationTest {
return given()
.contentType("application/json")
.body("""
- {"alias": "%s", "tags": {"team": "devops"}, "clientToken": "token-123"}
- """.formatted(alias))
+ {"alias": "%s", "tags": {"team": "devops"}, "clientToken": "%s"}
+ """.formatted(alias, UUID.randomUUID()))
.when()
.post("/workspaces")
.then()
diff --git a/src/test/java/io/github/hectorvent/floci/services/aps/ApsCreateWorkspaceIdempotencyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/aps/ApsCreateWorkspaceIdempotencyIntegrationTest.java
new file mode 100644
index 0000000000..4c116f736e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/aps/ApsCreateWorkspaceIdempotencyIntegrationTest.java
@@ -0,0 +1,195 @@
+package io.github.hectorvent.floci.services.aps;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.aps.model.PrometheusWorkspace;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.response.Response;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.amp.AmpClient;
+import software.amazon.awssdk.services.amp.model.ConflictException;
+import software.amazon.awssdk.services.amp.model.CreateWorkspaceResponse;
+import software.amazon.awssdk.services.amp.model.ValidationException;
+
+import java.net.URI;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class ApsCreateWorkspaceIdempotencyIntegrationTest {
+
+ private static final String ACCOUNT = "741648800001";
+ private static final String OTHER_ACCOUNT = "741648800002";
+ private static final Region REGION = Region.EU_CENTRAL_1;
+ private final ConcurrentLinkedQueue created = new ConcurrentLinkedQueue<>();
+
+ @Inject
+ StorageFactory storageFactory;
+
+ @AfterEach
+ void cleanUp() {
+ AccountAwareStorageBackend workspaces = storageFactory.create("aps", "aps-workspaces.json",
+ new TypeReference