All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.sdk.internal.ScopeConfiguratorBuilder Maven / Gradle / Ivy

There is a newer version: 1.44.1
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.sdk.internal;

import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import javax.annotation.Nullable;

/**
 * Builder for {@link ScopeConfigurator}.
 *
 * 

This class is internal and is hence not for public use. Its APIs are unstable and can change * at any time. * * @param The scope configuration object, e.g. {@code TracerConfig}, {@code LoggerConfig}, * {@code MeterConfig}. */ public final class ScopeConfiguratorBuilder { private final ScopeConfigurator baseScopeConfigurator; @Nullable private T defaultScopeConfig; private final List> conditions = new ArrayList<>(); ScopeConfiguratorBuilder(ScopeConfigurator baseScopeConfigurator) { this.baseScopeConfigurator = baseScopeConfigurator; } /** * Set the default scope config, which is returned by {@link ScopeConfigurator#apply(Object)} if a * {@link InstrumentationScopeInfo} does not match any {@link #addCondition(Predicate, Object) * conditions}. If a default is not set, an SDK defined default is used. */ public ScopeConfiguratorBuilder setDefault(T defaultScopeConfig) { this.defaultScopeConfig = defaultScopeConfig; return this; } /** * Add a condition. Conditions are evaluated in order. The {@code scopeConfig} for the first match * is returned by {@link ScopeConfigurator#apply(Object)}. * * @param scopePredicate predicate that {@link InstrumentationScopeInfo}s are evaluated against * @param scopeConfig the scope config to use when this condition is the first matching {@code * scopePredicate} * @see #nameMatchesGlob(String) * @see #nameEquals(String) */ public ScopeConfiguratorBuilder addCondition( Predicate scopePredicate, T scopeConfig) { conditions.add(new Condition<>(scopePredicate, scopeConfig)); return this; } /** * Helper function for pattern matching {@link InstrumentationScopeInfo#getName()} against the * {@code globPattern}. * *

{@code globPattern} may contain the wildcard characters {@code *} and {@code ?} with the * following matching criteria: * *

    *
  • {@code *} matches 0 or more instances of any character *
  • {@code ?} matches exactly one instance of any character *
* * @see #addCondition(Predicate, Object) */ public static Predicate nameMatchesGlob(String globPattern) { Predicate globPredicate = GlobUtil.toGlobPatternPredicate(globPattern); return scopeInfo -> globPredicate.test(scopeInfo.getName()); } /** * Helper function for exact matching {@link InstrumentationScopeInfo#getName()} against the * {@code scopeName}. * * @see #addCondition(Predicate, Object) */ public static Predicate nameEquals(String scopeName) { return scopeInfo -> scopeInfo.getName().equals(scopeName); } /** Build a {@link ScopeConfigurator} with the configuration of this builder. */ public ScopeConfigurator build() { // TODO: return an instance with toString implementation which self describes rules return scopeInfo -> { T scopeConfig = baseScopeConfigurator.apply(scopeInfo); if (scopeConfig != null) { return scopeConfig; } for (Condition condition : conditions) { if (condition.scopeMatcher.test(scopeInfo)) { return condition.scopeConfig; } } return defaultScopeConfig; }; } private static final class Condition { private final Predicate scopeMatcher; private final T scopeConfig; private Condition(Predicate scopeMatcher, T scopeConfig) { this.scopeMatcher = scopeMatcher; this.scopeConfig = scopeConfig; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy