-
Notifications
You must be signed in to change notification settings - Fork 178
[dynamic control] Add policy store #2721
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/PolicyStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Holds the latest validated policy snapshot and reports whether an update changed effective | ||
| * configuration. | ||
| */ | ||
| public final class PolicyStore { | ||
|
|
||
| private List<TelemetryPolicy> policies = Collections.emptyList(); | ||
|
|
||
| /** | ||
| * Replaces the stored policies when the new snapshot is not equal to the current one. | ||
| * | ||
| * <p>Input lists are normalized to a set of distinct policies ({@link TelemetryPolicy#equals | ||
| * value equality}): duplicates are dropped and only the first occurrence of each policy is kept | ||
| * (insertion order). Change detection uses set equality, so list order does not matter. That | ||
| * matches telemetry policy semantics where the effective result does not depend on processing | ||
| * order (see the telemetry policy OTEP, commutativity / no user-defined ordering between | ||
| * policies). | ||
| * | ||
| * @return {@code true} if the store was updated, {@code false} if the snapshot was unchanged | ||
| */ | ||
| public synchronized boolean updatePolicies(List<TelemetryPolicy> newPolicies) { | ||
| Objects.requireNonNull(newPolicies, "newPolicies cannot be null"); | ||
| LinkedHashSet<TelemetryPolicy> newPolicySet = new LinkedHashSet<>(newPolicies); | ||
| if (new LinkedHashSet<>(policies).equals(newPolicySet)) { | ||
| return false; | ||
| } | ||
| policies = new ArrayList<>(newPolicySet); | ||
| return true; | ||
| } | ||
|
|
||
| public synchronized List<TelemetryPolicy> getPolicies() { | ||
| return Collections.unmodifiableList(new ArrayList<>(policies)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
dynamic-control/src/test/java/io/opentelemetry/contrib/dynamic/policy/PolicyStoreTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.contrib.dynamic.policy; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.contrib.dynamic.policy.tracesampling.TraceSamplingRatePolicy; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PolicyStoreTest { | ||
|
|
||
| @Test | ||
| void updatePoliciesReturnsTrueOnFirstSet() { | ||
| PolicyStore store = new PolicyStore(); | ||
| List<TelemetryPolicy> policies = singletonList(new TraceSamplingRatePolicy(0.5)); | ||
|
|
||
| assertThat(store.updatePolicies(policies)).isTrue(); | ||
| assertThat(store.getPolicies()).isEqualTo(policies); | ||
| } | ||
|
|
||
| @Test | ||
| void updatePoliciesReturnsFalseWhenEqualContent() { | ||
| PolicyStore store = new PolicyStore(); | ||
| assertThat(store.updatePolicies(singletonList(new TraceSamplingRatePolicy(0.5)))).isTrue(); | ||
| assertThat(store.updatePolicies(singletonList(new TraceSamplingRatePolicy(0.5)))).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void updatePoliciesReturnsTrueWhenProbabilityChanges() { | ||
| PolicyStore store = new PolicyStore(); | ||
| assertThat(store.updatePolicies(singletonList(new TraceSamplingRatePolicy(0.25)))).isTrue(); | ||
| assertThat(store.updatePolicies(singletonList(new TraceSamplingRatePolicy(0.75)))).isTrue(); | ||
| assertThat(store.getPolicies()).containsExactly(new TraceSamplingRatePolicy(0.75)); | ||
| } | ||
|
|
||
| @Test | ||
| void updatePoliciesReturnsFalseWhenOnlyOrderDiffers() { | ||
| PolicyStore store = new PolicyStore(); | ||
| List<TelemetryPolicy> first = | ||
| Arrays.asList(new TraceSamplingRatePolicy(0.1), new TraceSamplingRatePolicy(0.2)); | ||
| List<TelemetryPolicy> reordered = | ||
| Arrays.asList(new TraceSamplingRatePolicy(0.2), new TraceSamplingRatePolicy(0.1)); | ||
|
|
||
| assertThat(store.updatePolicies(first)).isTrue(); | ||
| assertThat(store.updatePolicies(reordered)).isFalse(); | ||
| assertThat(store.getPolicies()).isEqualTo(first); | ||
| } | ||
|
|
||
| @Test | ||
| void updatePoliciesIgnoresDuplicatePoliciesInInput() { | ||
| PolicyStore store = new PolicyStore(); | ||
| TraceSamplingRatePolicy p = new TraceSamplingRatePolicy(0.5); | ||
| assertThat(store.updatePolicies(Arrays.asList(p, new TraceSamplingRatePolicy(0.5)))).isTrue(); | ||
| assertThat(store.getPolicies()).containsExactly(p); | ||
| assertThat(store.updatePolicies(singletonList(new TraceSamplingRatePolicy(0.5)))).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void getPoliciesReturnsEmptyWhenNeverUpdated() { | ||
| assertThat(new PolicyStore().getPolicies()).isEqualTo(Collections.emptyList()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.