Repository: open-telemetry/opentelemetry-java
File: sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/trace/samplers/ComposableRuleBasedSampler.java (Java)

Code before:
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.sdk.extension.incubator.trace.samplers;

import static io.opentelemetry.sdk.extension.incubator.trace.samplers.ImmutableSamplingIntent.NON_SAMPLING_INTENT;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.data.LinkData;
import java.util.List;

final class ComposableRuleBasedSampler implements ComposableSampler {

  private final SamplingRule[] rules;
  private final String description;

  ComposableRuleBasedSampler(List<SamplingRule> rules) {
    this.rules = rules.toArray(new SamplingRule[0]);

    StringBuilder description = new StringBuilder("ComposableRuleBasedSampler{[");
    if (this.rules.length > 0) {
      for (SamplingRule rule : this.rules) {
        description.append('(');
        description.append(rule.predicate().getDescription());
        description.append(':');
        description.append(rule.sampler().getDescription());
        description.append(')');
        description.append(',');
      }
      // Remove trailing comma
      description.setLength(description.length() - 1);
    }
    description.append("]}");
    this.description = description.toString();
  }

  @Override
  public SamplingIntent getSamplingIntent(
      Context parentContext,
      String traceId,
      String name,
      SpanKind spanKind,
      Attributes attributes,
      List<LinkData> parentLinks) {
    for (SamplingRule rule : rules) {
      if (rule.predicate()

Review comment (suggestion):
I considered whether empty should be an error, but I could conceive of cases that update the rules dynamically in a sort of "default-to-deny-all, sometimes accept something" type of situation. If making empty an error, that's still implementable though with an explicit catch-all and drop rule, so open to thoughts.

Code after:
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.sdk.extension.incubator.trace.samplers;

import static io.opentelemetry.sdk.extension.incubator.trace.samplers.ImmutableSamplingIntent.NON_SAMPLING_INTENT;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.data.LinkData;
import java.util.List;

final class ComposableRuleBasedSampler implements ComposableSampler {

  private final SamplingRule[] rules;
  private final String description;

  ComposableRuleBasedSampler(List<SamplingRule> rules) {
    this.rules = rules.toArray(new SamplingRule[0]);

    StringBuilder description = new StringBuilder("ComposableRuleBasedSampler{[");
    if (this.rules.length > 0) {
      for (SamplingRule rule : this.rules) {
        description.append('(');
        description.append(rule.predicate().toString());
        description.append(':');
        description.append(rule.sampler().getDescription());
        description.append(')');
        description.append(',');
      }
      // Remove trailing comma
      description.setLength(description.length() - 1);
    }
    description.append("]}");
    this.description = description.toString();
  }

  @Override
  public SamplingIntent getSamplingIntent(
      Context parentContext,
      String traceId,
      String name,
      SpanKind spanKind,
      Attributes attributes,
      List<LinkData> parentLinks) {
    for (SamplingRule rule : rules) {
      if (rule.predicate()