Skip to content

Commit 9f6440c

Browse files
committed
copilot feedback
1 parent a94b0ef commit 9f6440c

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/PolicyStore.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.ArrayList;
99
import java.util.Collections;
10+
import java.util.LinkedHashSet;
1011
import java.util.List;
1112
import java.util.Objects;
1213

@@ -16,20 +17,27 @@
1617
*/
1718
public final class PolicyStore {
1819

19-
private volatile List<TelemetryPolicy> policies = Collections.emptyList();
20+
private List<TelemetryPolicy> policies = Collections.emptyList();
2021

2122
/**
22-
* Replaces the stored policies when the new list is not equal to the current snapshot.
23+
* Replaces the stored policies when the new snapshot is not equal to the current one.
2324
*
24-
* @return {@code true} if the store was updated, {@code false} if the list was equal
25+
* <p>Input lists are normalized to a set of distinct policies ({@link TelemetryPolicy#equals
26+
* value equality}): duplicates are dropped and only the first occurrence of each policy is
27+
* kept (insertion order). Change detection uses set equality, so list order does not matter.
28+
* That matches telemetry policy semantics where the effective result does not depend on
29+
* processing order (see the telemetry policy OTEP, commutativity / no user-defined ordering
30+
* between policies).
31+
*
32+
* @return {@code true} if the store was updated, {@code false} if the snapshot was unchanged
2533
*/
2634
public synchronized boolean updatePolicies(List<TelemetryPolicy> newPolicies) {
2735
Objects.requireNonNull(newPolicies, "newPolicies cannot be null");
28-
List<TelemetryPolicy> copy = new ArrayList<>(newPolicies);
29-
if (policies.equals(copy)) {
36+
List<TelemetryPolicy> normalized = new ArrayList<>(new LinkedHashSet<>(newPolicies));
37+
if (new LinkedHashSet<>(policies).equals(new LinkedHashSet<>(normalized))) {
3038
return false;
3139
}
32-
policies = copy;
40+
policies = normalized;
3341
return true;
3442
}
3543

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/TelemetryPolicy.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
* <p>Direct instantiation of this base class is intentionally supported for type-only policy
2222
* signals (for example, to indicate policy removal/reset without policy-specific values).
2323
*
24+
* <p><b>Subclasses:</b> {@link #equals(Object)} on this class returns {@code false} when either
25+
* operand is a typed subclass (not {@code TelemetryPolicy} itself), so a type-only instance never
26+
* equals a value-carrying subclass with the same {@link #getType() type}. Each concrete subclass
27+
* <b>must</b> override both {@code equals} and {@code hashCode} consistently with its fields, and
28+
* obey the {@code equals}/{@code hashCode} contract. That is required for consumers such as {@link
29+
* io.opentelemetry.contrib.dynamic.policy.PolicyStore} that deduplicate and detect changes using
30+
* {@link Object#equals(Object)}.
31+
*
2432
* @see io.opentelemetry.contrib.dynamic.policy
2533
*/
2634
public class TelemetryPolicy {
@@ -53,7 +61,8 @@ public String getType() {
5361

5462
/**
5563
* Type-only policies ({@link TelemetryPolicy} instances) do not equal typed subclasses that share
56-
* the same {@link #getType() type} string.
64+
* the same {@link #getType() type} string. Subclasses must override {@code equals} (and {@code
65+
* hashCode}) for value-based equality; see the class Javadoc.
5766
*/
5867
@Override
5968
public boolean equals(Object obj) {
@@ -72,6 +81,6 @@ public boolean equals(Object obj) {
7281

7382
@Override
7483
public int hashCode() {
75-
return Objects.hash(type);
84+
return type.hashCode();
7685
}
7786
}

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/policy/tracesampling/TraceSamplingRatePolicy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.contrib.dynamic.policy.tracesampling;
77

88
import io.opentelemetry.contrib.dynamic.policy.TelemetryPolicy;
9-
import java.util.Objects;
109

1110
public final class TraceSamplingRatePolicy extends TelemetryPolicy {
1211
public static final String POLICY_TYPE = "trace-sampling";
@@ -39,6 +38,6 @@ public boolean equals(Object obj) {
3938

4039
@Override
4140
public int hashCode() {
42-
return Objects.hash(probability);
41+
return Double.hashCode(probability);
4342
}
4443
}

dynamic-control/src/test/java/io/opentelemetry/contrib/dynamic/policy/PolicyStoreTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,25 @@ void updatePoliciesReturnsTrueWhenProbabilityChanges() {
4141
}
4242

4343
@Test
44-
void updatePoliciesDetectsListOrderChange() {
44+
void updatePoliciesReturnsFalseWhenOnlyOrderDiffers() {
4545
PolicyStore store = new PolicyStore();
4646
List<TelemetryPolicy> first =
4747
Arrays.asList(new TraceSamplingRatePolicy(0.1), new TraceSamplingRatePolicy(0.2));
4848
List<TelemetryPolicy> reordered =
4949
Arrays.asList(new TraceSamplingRatePolicy(0.2), new TraceSamplingRatePolicy(0.1));
5050

5151
assertThat(store.updatePolicies(first)).isTrue();
52-
assertThat(store.updatePolicies(reordered)).isTrue();
53-
assertThat(store.getPolicies()).isEqualTo(reordered);
52+
assertThat(store.updatePolicies(reordered)).isFalse();
53+
assertThat(store.getPolicies()).isEqualTo(first);
54+
}
55+
56+
@Test
57+
void updatePoliciesIgnoresDuplicatePoliciesInInput() {
58+
PolicyStore store = new PolicyStore();
59+
TraceSamplingRatePolicy p = new TraceSamplingRatePolicy(0.5);
60+
assertThat(store.updatePolicies(Arrays.asList(p, new TraceSamplingRatePolicy(0.5)))).isTrue();
61+
assertThat(store.getPolicies()).containsExactly(p);
62+
assertThat(store.updatePolicies(singletonList(new TraceSamplingRatePolicy(0.5)))).isFalse();
5463
}
5564

5665
@Test

0 commit comments

Comments
 (0)