Skip to content

Commit

Permalink
Global observation registry
Browse files Browse the repository at this point in the history
This change adds a static ObservationRegistry.global similar to the Metrics.globalRegistry. For the same reasons, for use in places where dependency injection is not desired.
  • Loading branch information
cfredri4 committed Nov 22, 2024
1 parent 956ced4 commit 0320809
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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;

/**
* Delegating {@link ObservationRegistry}.
*
* @author Christian Fredriksson
* @since 1.14.2
*/
class DelegatingObservationRegistry implements ObservationRegistry {

private ObservationRegistry delegate = ObservationRegistry.NOOP;

@Override
public Observation getCurrentObservation() {
return delegate.getCurrentObservation();
}

@Override
public Observation.Scope getCurrentObservationScope() {
return delegate.getCurrentObservationScope();
}

@Override
public void setCurrentObservationScope(Observation.Scope current) {
delegate.setCurrentObservationScope(current);
}

@Override
public ObservationConfig observationConfig() {
return delegate.observationConfig();
}

@Override
public boolean isNoop() {
return delegate.isNoop();
}

void set(ObservationRegistry delegate) {
this.delegate = delegate != null ? delegate : ObservationRegistry.NOOP;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ static ObservationRegistry create() {
return new SimpleObservationRegistry();
}

/**
* Creates an instance of {@link ObservationRegistry}.
* @return {@link ObservationRegistry} instance
*/
static void setGlobal(ObservationRegistry registry) {
((DelegatingObservationRegistry) global).set(registry);
}

/**
* Global {@link ObservationRegistry}.
*/
ObservationRegistry global = new DelegatingObservationRegistry();

/**
* No-op implementation of {@link ObservationRegistry}.
*/
Expand Down

0 comments on commit 0320809

Please sign in to comment.