Skip to content

Commit dc81b54

Browse files
authored
initial TelemetryPolicy (#2592)
1 parent 6770c04 commit dc81b54

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

dynamic-control/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ dependencies {
1616
annotationProcessor("com.google.auto.service:auto-service")
1717
compileOnly("com.google.auto.service:auto-service-annotations")
1818

19+
implementation("com.fasterxml.jackson.core:jackson-databind")
20+
1921
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
2022
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
2123

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* Defines the Telemetry Policy API, a mechanism for intent-based specification of telemetry
8+
* behavior.
9+
*
10+
* <p>A Telemetry Policy is a distinct rule that governs how telemetry data is collected, processed,
11+
* or exported. Unlike traditional configuration, which often describes a pipeline of specific
12+
* components, a policy describes the <em>desired outcome</em> (the intent) and is
13+
* implementation-agnostic.
14+
*
15+
* <h2>Key Characteristics</h2>
16+
*
17+
* <ul>
18+
* <li><strong>Typed:</strong> Each policy has a type (e.g., {@code trace-sampling}, {@code
19+
* log-filter}) that defines its domain and behavior.
20+
* <li><strong>Implementation Agnostic:</strong> The same policy definition can be enforced by an
21+
* SDK, a Collector, or any other component that understands the policy type.
22+
* <li><strong>Standalone:</strong> Policies are atomic and self-contained. They do not depend on
23+
* pipeline configuration or other policies.
24+
* <li><strong>Dynamic:</strong> Policies are designed to be updated at runtime, allowing behavior
25+
* to change without restarting the application.
26+
* <li><strong>Idempotent:</strong> Policies can be applied multiple times safely. The system
27+
* converges to the desired state.
28+
* </ul>
29+
*
30+
* <h2>Architecture</h2>
31+
*
32+
* <p>The telemetry policy ecosystem typically consists of:
33+
*
34+
* <ul>
35+
* <li><strong>Policy Providers:</strong> Sources of policies (e.g., a file, an HTTP endpoint, an
36+
* OpAMP server).
37+
* <li><strong>Policy Aggregator:</strong> Merges policies from multiple providers. Policies of
38+
* the same type are merged (e.g., using JSON Merge Patch), while policies of different types
39+
* coexist.
40+
* <li><strong>Policy Implementations:</strong> Components that react to policy updates and
41+
* enforce the specified behavior (e.g., updating a trace sampler).
42+
* </ul>
43+
*
44+
* @see io.opentelemetry.contrib.dynamic.policy.TelemetryPolicy
45+
*/
46+
@ParametersAreNonnullByDefault
47+
package io.opentelemetry.contrib.dynamic.policy;
48+
49+
import javax.annotation.ParametersAreNonnullByDefault;

0 commit comments

Comments
 (0)