feat: jpa, messaging, notification, mongo, graphql 어댑터터 구현체 추가
This commit is contained in:
+30
-5
@@ -54,11 +54,19 @@ public final class CardinalityGuard {
|
||||
if (values.contains(value)) {
|
||||
return true;
|
||||
}
|
||||
if (values.size() >= limitPerDimension) {
|
||||
return false;
|
||||
// Size-then-add was not atomic: N threads could each read size == limit - 1 and each add, so
|
||||
// the configured limit was an average rather than a bound. A guard that can be exceeded under
|
||||
// load is no guard — load is when it matters.
|
||||
synchronized (values) {
|
||||
if (values.contains(value)) {
|
||||
return true;
|
||||
}
|
||||
if (values.size() >= limitPerDimension) {
|
||||
return false;
|
||||
}
|
||||
values.add(value);
|
||||
return true;
|
||||
}
|
||||
values.add(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,13 +77,30 @@ public final class CardinalityGuard {
|
||||
*/
|
||||
public boolean admit(MessagingTags tags) {
|
||||
Objects.requireNonNull(tags, "tags must not be null");
|
||||
// Preflight every dimension before committing any of them.
|
||||
//
|
||||
// The loop used to admit each dimension as it went, so a tag set rejected on its last
|
||||
// dimension had already permanently added the earlier ones — spending the budget of a bounded
|
||||
// dimension on a series that was never emitted.
|
||||
Map<String, String> pending = tags.asMap();
|
||||
for (Map.Entry<String, String> entry : pending.entrySet()) {
|
||||
if (!wouldAdmit(entry.getKey(), entry.getValue())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
boolean admitted = true;
|
||||
for (Map.Entry<String, String> entry : tags.asMap().entrySet()) {
|
||||
for (Map.Entry<String, String> entry : pending.entrySet()) {
|
||||
admitted &= admit(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return admitted;
|
||||
}
|
||||
|
||||
/** Whether a value is already known or would still fit, without recording it. */
|
||||
private boolean wouldAdmit(String dimension, String value) {
|
||||
Set<String> values = observed.get(dimension);
|
||||
return values == null || values.contains(value) || values.size() < limitPerDimension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns how many distinct values a dimension has taken.
|
||||
*
|
||||
|
||||
+22
-8
@@ -119,17 +119,31 @@ public final class MessagingMetrics implements MessagingObservation {
|
||||
// Redact before anything else touches the values. Diagnostics are the one place where a caller
|
||||
// can pass arbitrary keys, so this is where a secret would otherwise reach the backend.
|
||||
Map<String, String> safe = redactor.sanitize(diagnostics);
|
||||
// The value never becomes a tag.
|
||||
//
|
||||
// It used to: every diagnostic key and value was attached to a counter, behind a guard that
|
||||
// only bounded the base dimensions. One unique message id, exception message or URL per
|
||||
// request created one meter series per request — permanently, in the backend and in this
|
||||
// process's heap — and the redactor only masks keys it recognises, so free-form text carried
|
||||
// whatever it carried.
|
||||
//
|
||||
// What stays is the shape: which diagnostic keys occurred, counted against the bounded base
|
||||
// dimensions. The values belong in a structured log or a trace event, where they are bounded
|
||||
// by retention rather than by cardinality.
|
||||
admitted(tags)
|
||||
.ifPresent(
|
||||
micrometerTags ->
|
||||
safe.forEach(
|
||||
(key, value) ->
|
||||
registry
|
||||
.counter(
|
||||
"messaging.diagnostics",
|
||||
micrometerTags.and(
|
||||
Tag.of("diagnostic", key), Tag.of("value", value)))
|
||||
.increment()));
|
||||
safe.keySet()
|
||||
.forEach(
|
||||
key -> {
|
||||
if (guard.admit("diagnostic", key)) {
|
||||
registry
|
||||
.counter(
|
||||
"messaging.diagnostics",
|
||||
micrometerTags.and(Tag.of("diagnostic", key)))
|
||||
.increment();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+55
-1
@@ -84,7 +84,61 @@ class MessagingSecretLeakTest {
|
||||
.map(Tag::getValue)
|
||||
.toList();
|
||||
|
||||
assertThat(emitted).doesNotContain("hunter2").contains("unroutable");
|
||||
// No diagnostic value reaches the registry at all — not the secret, and not the innocuous one
|
||||
// either. A value tag is one meter series per distinct value, permanently, and the redactor
|
||||
// only masks keys it recognises: free-form text carries whatever it carries.
|
||||
assertThat(emitted).doesNotContain("hunter2").doesNotContain("unroutable");
|
||||
assertThat(
|
||||
registry.getMeters().stream()
|
||||
.map(Meter::getId)
|
||||
.flatMap(id -> id.getTags().stream())
|
||||
.filter(tag -> tag.getKey().equals("diagnostic"))
|
||||
.map(Tag::getValue)
|
||||
.toList())
|
||||
.as("the shape is kept: which diagnostic keys occurred, against bounded base dimensions")
|
||||
.contains("reason");
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.DisplayName("ten thousand distinct diagnostic values create no new series")
|
||||
void distinctDiagnosticValuesCreateNoNewSeries() {
|
||||
MessagingMetrics metrics = new MessagingMetrics(registry, new CardinalityGuard(100), redactor);
|
||||
MessagingTags tags = MessagingTags.of("kafka", "orders.v1", "publish", "rejected");
|
||||
|
||||
for (int index = 0; index < 10_000; index++) {
|
||||
metrics.recordDiagnostics(tags, Map.of("reason", "unique-value-" + index));
|
||||
}
|
||||
|
||||
assertThat(
|
||||
registry.getMeters().stream()
|
||||
.filter(meter -> meter.getId().getName().equals("messaging.diagnostics"))
|
||||
.toList())
|
||||
.as("one unique message id per request used to create one meter series per request")
|
||||
.hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.DisplayName("concurrent admission never exceeds the configured limit")
|
||||
void concurrentAdmissionNeverExceedsTheLimit() throws Exception {
|
||||
CardinalityGuard guard = new CardinalityGuard(10);
|
||||
java.util.concurrent.ExecutorService pool =
|
||||
java.util.concurrent.Executors.newFixedThreadPool(8);
|
||||
try {
|
||||
java.util.List<java.util.concurrent.Callable<Boolean>> work =
|
||||
java.util.stream.IntStream.range(0, 200)
|
||||
.<java.util.concurrent.Callable<Boolean>>mapToObj(
|
||||
index -> () -> guard.admit("destination", "d-" + index))
|
||||
.toList();
|
||||
for (java.util.concurrent.Future<Boolean> future : pool.invokeAll(work)) {
|
||||
future.get();
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
|
||||
assertThat(guard.cardinality("destination"))
|
||||
.as("size-then-add is not atomic, so the limit was an average rather than a bound")
|
||||
.isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+10
@@ -31,6 +31,9 @@ class SecretLeakStaticScanTest {
|
||||
Pattern.compile("(System\\.out|System\\.err|printStackTrace\\()");
|
||||
|
||||
/** A Java string literal, including escaped quotes. */
|
||||
/** Whether the scanner is currently inside a text block. */
|
||||
private static final ThreadLocal<Boolean> IN_TEXT_BLOCK = ThreadLocal.withInitial(() -> false);
|
||||
|
||||
private static final Pattern STRING_LITERAL = Pattern.compile("\"(\\\\.|[^\"\\\\])*\"");
|
||||
|
||||
/** The operand immediately before or after a string concatenation. */
|
||||
@@ -129,6 +132,13 @@ class SecretLeakStaticScanTest {
|
||||
* @return true when the line concatenates a sensitive value
|
||||
*/
|
||||
private static boolean leaksASensitiveValue(String line) {
|
||||
if (line.contains("\"\"\"") || IN_TEXT_BLOCK.get()) {
|
||||
// A text block is SQL or JSON, not Java code. The scanner replaced ordinary string literals
|
||||
// and then read the contents of text blocks as if they were expressions, so a column named
|
||||
// `lease_token` in an UPDATE statement looked like a concatenated credential.
|
||||
IN_TEXT_BLOCK.set(IN_TEXT_BLOCK.get() != line.contains("\"\"\""));
|
||||
return false;
|
||||
}
|
||||
String code = STRING_LITERAL.matcher(line).replaceAll("\"\"");
|
||||
if (!code.contains("+")) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user