4545import opamp .proto .RemoteConfigStatuses ;
4646import opamp .proto .ServerErrorResponse ;
4747
48- /** Policy provider backed by OpAMP remote config updates. */
48+ /**
49+ * {@link PolicyProvider} implementation backed by OpAMP remote configuration updates.
50+ *
51+ * <p>The provider subscribes to an OpAMP endpoint, extracts the configured payload for one source
52+ * location key, maps incoming source keys to internal policy types, validates them with the
53+ * supplied validators, and publishes the resulting policies to callers.
54+ */
4955public final class OpampPolicyProvider implements PolicyProvider {
5056 private static final Logger logger = Logger .getLogger (OpampPolicyProvider .class .getName ());
5157 private static final ObjectMapper MAPPER = new ObjectMapper ();
@@ -76,6 +82,16 @@ public final class OpampPolicyProvider implements PolicyProvider {
7682 private final AtomicReference <OpampClient > clientRef = new AtomicReference <>();
7783 private final AtomicReference <Thread > shutdownHookRef = new AtomicReference <>();
7884
85+ /**
86+ * Creates a provider for one OpAMP-backed policy source.
87+ *
88+ * @param properties auto-configuration properties used to resolve endpoint/service identity
89+ * @param configuredLocation source location key used to select one OpAMP config entry
90+ * @param format payload format parser for the selected source
91+ * @param mappings source-key-to-policy-type mappings for this source
92+ * @param validators validators used to materialize typed {@link TelemetryPolicy} instances
93+ * @throws IllegalArgumentException if required configuration such as OpAMP endpoint is missing
94+ */
7995 public OpampPolicyProvider (
8096 ConfigProperties properties ,
8197 String configuredLocation ,
@@ -105,11 +121,25 @@ public OpampPolicyProvider(
105121 GLOBAL_POLLING_INTERVAL .get (), "polling interval cannot be null" ));
106122 }
107123
124+ /**
125+ * Returns the latest validated policies received from OpAMP.
126+ *
127+ * <p>The returned list is the current immutable snapshot held by this provider.
128+ */
108129 @ Override
109130 public List <TelemetryPolicy > fetchPolicies () {
110131 return Objects .requireNonNull (currentPolicies .get (), "currentPolicies cannot be null" );
111132 }
112133
134+ /**
135+ * Starts the OpAMP watch loop and registers a callback for policy updates.
136+ *
137+ * <p>If already started, this method is idempotent and returns a handle that still stops the
138+ * active watcher.
139+ *
140+ * @param onUpdate callback invoked with an immutable snapshot whenever policies change
141+ * @return a {@link Closeable} that stops watching
142+ */
113143 @ Override
114144 public Closeable startWatching (Consumer <List <TelemetryPolicy >> onUpdate ) {
115145 Objects .requireNonNull (onUpdate , "onUpdate cannot be null" );
@@ -283,6 +313,12 @@ private static void safeClose(OpampClient client) {
283313 }
284314 }
285315
316+ /**
317+ * Sets the global polling interval used by all active and future providers.
318+ *
319+ * @param interval new polling interval, must be greater than zero
320+ * @throws IllegalArgumentException if interval is zero or negative
321+ */
286322 public static void setGlobalPollingInterval (Duration interval ) {
287323 Objects .requireNonNull (interval , "interval cannot be null" );
288324 if (interval .isZero () || interval .isNegative ()) {
@@ -294,11 +330,13 @@ public static void setGlobalPollingInterval(Duration interval) {
294330 }
295331 }
296332
333+ /** Returns the current global polling interval for unit tests. */
297334 static Duration getGlobalPollingIntervalForTest () {
298335 return Objects .requireNonNull (
299336 GLOBAL_POLLING_INTERVAL .get (), "global polling interval cannot be null" );
300337 }
301338
339+ /** Resets shared provider test state, including polling interval and active provider tracking. */
302340 public static void resetForTest () {
303341 setGlobalPollingInterval (DEFAULT_POLLING_INTERVAL );
304342 ACTIVE_PROVIDERS .clear ();
@@ -309,7 +347,11 @@ void setPollingInterval(Duration interval) {
309347 logger .info ("Updated OpAMP polling interval to " + interval );
310348 }
311349
312- // package private for testing
350+ /**
351+ * Resolves and normalizes the OpAMP endpoint URL from configuration.
352+ *
353+ * <p>Returns {@code null} when unset.
354+ */
313355 @ Nullable
314356 static String getEndpoint (ConfigProperties properties ) {
315357 String endpoint = properties .getString (OPAMP_ENDPOINT );
@@ -319,7 +361,12 @@ static String getEndpoint(ConfigProperties properties) {
319361 return normalizeEndpoint (endpoint );
320362 }
321363
322- // package private for testing
364+ /**
365+ * Resolves service name from configuration.
366+ *
367+ * <p>Resolution order: {@code otel.service.name}, then {@code service.name} from {@code
368+ * otel.resource.attributes}, then {@code unknown_service:java}.
369+ */
323370 static String getServiceName (ConfigProperties properties ) {
324371 String configuredServiceName = properties .getString (SERVICE_NAME );
325372 if (configuredServiceName != null ) {
@@ -333,7 +380,11 @@ static String getServiceName(ConfigProperties properties) {
333380 return "unknown_service:java" ;
334381 }
335382
336- // package private for testing
383+ /**
384+ * Resolves deployment environment from resource attributes.
385+ *
386+ * <p>Resolution order: {@code deployment.environment.name}, then {@code deployment.environment}.
387+ */
337388 @ Nullable
338389 static String getServiceEnvironment (ConfigProperties properties ) {
339390 Map <String , String > resourceMap = properties .getMap (RESOURCE_ATTRIBUTES );
0 commit comments