|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.dynamic.policy; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.JsonNode; |
| 9 | +import java.util.Objects; |
| 10 | +import javax.annotation.Nullable; |
| 11 | + |
| 12 | +/** |
| 13 | + * Represents a single Telemetry Policy, comprising a type and a specification. |
| 14 | + * |
| 15 | + * <p>A {@code TelemetryPolicy} instance encapsulates a specific rule or configuration intent |
| 16 | + * identified by its {@link #getType() type}. The behavior of the policy is defined by its {@link |
| 17 | + * #getSpec() specification}, which is a JSON object. |
| 18 | + * |
| 19 | + * <p>Policies are typically immutable data carriers. The {@code spec} can be {@code null}, which |
| 20 | + * conventionally signifies the removal or absence of a policy for the given type in certain |
| 21 | + * contexts (e.g., when calculating diffs or updates). |
| 22 | + * |
| 23 | + * <p>As an example take the JSON structure `{"trace-sampling": {"probability" : 0.5}}` This is of |
| 24 | + * type "trace-sampling", with spec `{"probability" : 0.5}`, indicating the intent that the trace |
| 25 | + * sampling-probability be set to 50% |
| 26 | + * |
| 27 | + * @see io.opentelemetry.contrib.dynamic.policy |
| 28 | + */ |
| 29 | +public class TelemetryPolicy { |
| 30 | + private final String type; // e.g. "trace-sampling" |
| 31 | + // JSON content after schema validation |
| 32 | + // null means removed, which is relevant for merging policies |
| 33 | + @Nullable private final JsonNode spec; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructs a new TelemetryPolicy. |
| 37 | + * |
| 38 | + * @param type the type of the policy (e.g., "trace-sampling"), must not be null. |
| 39 | + * @param spec the JSON specification of the policy, or {@code null} to indicate removal. |
| 40 | + */ |
| 41 | + public TelemetryPolicy(String type, @Nullable JsonNode spec) { |
| 42 | + Objects.requireNonNull(type, "type cannot be null"); |
| 43 | + this.type = type; |
| 44 | + this.spec = spec; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the type of this policy. |
| 49 | + * |
| 50 | + * <p>The type identifies the domain and expected behavior of the policy (e.g., "trace-sampling", |
| 51 | + * "metric-rate"). |
| 52 | + * |
| 53 | + * @return the policy type. |
| 54 | + */ |
| 55 | + public String getType() { |
| 56 | + return type; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the specification of this policy. |
| 61 | + * |
| 62 | + * <p>The specification is a JSON structure defining the parameters of the policy. If {@code |
| 63 | + * null}, it may indicate that the policy is being removed or is empty. |
| 64 | + * |
| 65 | + * @return the policy specification, or {@code null}. |
| 66 | + */ |
| 67 | + @Nullable |
| 68 | + public JsonNode getSpec() { |
| 69 | + return spec; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean equals(Object o) { |
| 74 | + if (this == o) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + if (!(o instanceof TelemetryPolicy)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + TelemetryPolicy that = (TelemetryPolicy) o; |
| 81 | + return Objects.equals(type, that.type) && Objects.equals(spec, that.spec); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int hashCode() { |
| 86 | + return Objects.hash(type, spec); |
| 87 | + } |
| 88 | +} |
0 commit comments