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

keycloakjar.io.micrometer.observation.ObservationRegistry Maven / Gradle / Ivy

There is a newer version: 7.21.1
Show newest version
/*
 * Copyright 2022 VMware, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micrometer.observation;

import io.micrometer.common.lang.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;

/**
 * Implementations of this interface are responsible for managing state of an
 * {@link Observation}.
 *
 * @author Jonatan Ivanov
 * @author Tommy Ludwig
 * @author Marcin Grzejszczak
 * @since 1.10.0
 */
public interface ObservationRegistry {

    /**
     * Creates an instance of {@link ObservationRegistry}.
     * @return {@link ObservationRegistry} instance
     */
    static ObservationRegistry create() {
        return new SimpleObservationRegistry();
    }

    /**
     * No-op implementation of {@link ObservationRegistry}.
     */
    ObservationRegistry NOOP = NoopObservationRegistry.INSTANCE;

    /**
     * When previously set will allow to retrieve the {@link Observation} at any point in
     * time.
     *
     * Example: if an {@link Observation} was put in {@link Observation.Scope} then this
     * method will return the current present {@link Observation} within the scope.
     * @return current observation or {@code null} if it's not present
     */
    @Nullable
    Observation getCurrentObservation();

    /**
     * When previously set will allow to retrieve the {@link Observation.Scope} at any
     * point in time.
     *
     * Example: if an {@link Observation} was put in {@link Observation.Scope} then this
     * method will return the current present {@link Observation.Scope}.
     * @return current observation scope or {@code null} if it's not present
     */
    @Nullable
    Observation.Scope getCurrentObservationScope();

    /**
     * Sets the observation scope as current.
     * @param current observation scope
     */
    void setCurrentObservationScope(@Nullable Observation.Scope current);

    /**
     * Configuration options for this registry.
     * @return observation configuration
     */
    ObservationConfig observationConfig();

    /**
     * Checks whether this {@link ObservationRegistry} is no-op.
     * @return {@code true} when this is a no-op observation registry
     */
    default boolean isNoop() {
        return this == NOOP;
    }

    /**
     * Access to configuration options for this registry.
     */
    class ObservationConfig {

        private final List> observationHandlers = new CopyOnWriteArrayList<>();

        private final List observationPredicates = new CopyOnWriteArrayList<>();

        private final List> observationConventions = new CopyOnWriteArrayList<>();

        private final List observationFilters = new CopyOnWriteArrayList<>();

        /**
         * Register a handler for the {@link Observation observations}.
         * @param handler handler to add to the current configuration
         * @return This configuration instance
         */
        public ObservationConfig observationHandler(ObservationHandler handler) {
            this.observationHandlers.add(handler);
            return this;
        }

        /**
         * Register a predicate to define whether {@link Observation observation} should
         * be created or a {@link NoopObservation} instead.
         * @param predicate predicate
         * @return This configuration instance
         */
        public ObservationConfig observationPredicate(ObservationPredicate predicate) {
            this.observationPredicates.add(predicate);
            return this;
        }

        /**
         * Register an observation filter for the {@link Observation observations}.
         * @param observationFilter an observation filter to add to the current
         * configuration
         * @return This configuration instance
         */
        public ObservationConfig observationFilter(ObservationFilter observationFilter) {
            this.observationFilters.add(observationFilter);
            return this;
        }

        /**
         * Register an {@link ObservationConvention}.
         * 

* Please check the javadoc of * {@link Observation#createNotStarted(ObservationConvention, ObservationConvention, Supplier, ObservationRegistry)} * method for the logic of choosing the convention. *

* @param observationConvention observation convention * @return This configuration instance */ public ObservationConfig observationConvention(GlobalObservationConvention observationConvention) { this.observationConventions.add(observationConvention); return this; } /** * Finds an {@link ObservationConvention} for the given * {@link Observation.Context}. * @param context context * @param defaultConvention default convention if none found * @return matching {@link ObservationConvention} or default when no matching * found */ @SuppressWarnings("unchecked") ObservationConvention getObservationConvention(T context, ObservationConvention defaultConvention) { for (ObservationConvention convention : this.observationConventions) { if (convention.supportsContext(context)) { return (ObservationConvention) convention; } } return Objects.requireNonNull(defaultConvention, "Default ObservationConvention must not be null"); } /** * Check to assert whether {@link Observation} should be created or * {@link NoopObservation} instead. * @param name observation technical name * @param context context * @return {@code true} when observation is enabled */ boolean isObservationEnabled(String name, @Nullable Observation.Context context) { for (ObservationPredicate predicate : this.observationPredicates) { if (!predicate.test(name, context)) { return false; } } return true; } // package-private for minimal visibility Collection> getObservationHandlers() { return observationHandlers; } Collection getObservationFilters() { return observationFilters; } Collection> getObservationConventions() { return observationConventions; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy