Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ private void record(ProceedingJoinPoint pjp, Timed timed, String metricName, Tim
.tags(EXCEPTION_TAG, exceptionClass)
.tags(tagsBasedOnJoinPoint.apply(pjp))
.publishPercentileHistogram(timed.histogram())
.publishPercentiles(timed.percentiles().length == 0 ? null : timed.percentiles()));
.publishPercentiles(timed.percentiles().length == 0 ? null : timed.percentiles())
.register(registry)
);
} catch (Exception e) {
// ignoring on purpose
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import io.micrometer.api.instrument.noop.NoopMeter;
import io.micrometer.api.instrument.noop.NoopTimeGauge;
import io.micrometer.api.instrument.noop.NoopTimer;
import io.micrometer.api.instrument.observation.Observation;
import io.micrometer.api.instrument.observation.ObservationRegistry;
import io.micrometer.api.instrument.observation.TimerObservationHandler;
import io.micrometer.api.instrument.search.MeterNotFoundException;
import io.micrometer.api.instrument.search.RequiredSearch;
import io.micrometer.api.instrument.search.Search;
Expand Down Expand Up @@ -75,17 +78,40 @@
* @author Jon Schneider
* @author Johnny Lim
*/
public abstract class MeterRegistry {
public abstract class MeterRegistry implements ObservationRegistry {
protected final Clock clock;
private final Object meterMapLock = new Object();
private volatile MeterFilter[] filters = new MeterFilter[0];
private final List<TimerRecordingHandler<?>> timerRecordingHandlers = new CopyOnWriteArrayList<>();
private final List<Consumer<Meter>> meterAddedListeners = new CopyOnWriteArrayList<>();
private final List<Consumer<Meter>> meterRemovedListeners = new CopyOnWriteArrayList<>();
private final List<BiConsumer<Meter.Id, String>> meterRegistrationFailedListeners = new CopyOnWriteArrayList<>();
private final Config config = new Config();
private final More more = new More();
private final ThreadLocal<Timer.Sample> localSample = new ThreadLocal<>();

private final ThreadLocal<Observation> localObservation = new ThreadLocal<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadLocalUsage: ThreadLocals should be stored in static fields (details)
(at-me in a reply with help or ignore)


private final ObservationConfig observationConfig = new ObservationConfig();

@Nullable
@Override
public Observation getCurrentObservation() {
return this.localObservation.get();
}

@Override
public void setCurrentObservation(@Nullable Observation current) {
this.localObservation.set(current);
}

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

//TODO Want this under observationConfig but not sure that's possible
public void withTimerObservationHandler() {
observationConfig().observationHandler(new TimerObservationHandler(this));
}

// Even though writes are guarded by meterMapLock, iterators across value space are supported
// Hence, we use CHM to support that iteration without ConcurrentModificationException risk
Expand Down Expand Up @@ -116,14 +142,6 @@ protected MeterRegistry(Clock clock) {
this.clock = clock;
}

@Nullable public Timer.Sample getCurrentSample() {
return localSample.get();
}

Timer.Scope openNewScope(Timer.Sample currentSample) {
return new Timer.Scope(localSample, currentSample);
}

/**
* Build a new gauge to be added to the registry. This is guaranteed to only be called if the gauge doesn't already exist.
*
Expand Down Expand Up @@ -809,25 +827,6 @@ public Config onMeterRegistrationFailed(BiConsumer<Id, String> meterRegistration
return this;
}

/**
* Register an event handler for {@link Timer} recordings made using {@link Timer#start(MeterRegistry)}
* and {@link Timer.Sample#stop(Timer.Builder)} methods. You can add arbitrary behavior
* in the callbacks provided to get additional behavior out of timing instrumentation.
*
* @param handler handler to add to the current configuration
* @return This configuration instance
* @since 2.0.0
*/
public Config timerRecordingHandler(TimerRecordingHandler<?> handler) {
timerRecordingHandlers.add(handler);
return this;
}

// package-private for minimal visibility
Collection<TimerRecordingHandler<?>> getTimerRecordingHandlers() {
return timerRecordingHandlers;
}

/**
* Use the provided naming convention, overriding the default for your monitoring system.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.api.instrument;

import io.micrometer.api.instrument.observation.Observation;
import io.micrometer.api.lang.Nullable;

public class NoopObservation implements Observation {
public static final NoopObservation INSTANCE = new NoopObservation();

private NoopObservation() {
}

@Override
public Observation contextualName(String contextualName) {
return this;
}

@Override
public Observation lowCardinalityTag(Tag tag) {
return this;
}

@Override
public Observation lowCardinalityTag(String key, String value) {
return this;
}

@Override
public Observation highCardinalityTag(Tag tag) {
return this;
}

@Override
public Observation highCardinalityTag(String key, String value) {
return this;
}

@Override
public Observation error(Throwable error) {
return this;
}

@Override
public Observation start() {
return this;
}

@Override
public void stop() {
}

@Override
public Scope openScope() {
return NoOpScope.INSTANCE;
}

static class NoOpScope implements Scope {
static final NoOpScope INSTANCE = new NoOpScope();

@Nullable
@Override
public Observation getCurrentObservation() {
return NoopObservation.INSTANCE;
}

@Override
public void close() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,48 @@
*/
public interface TagsProvider {

/**
* Default low cardinality tags.
*
* @return tags
*/
default Tags getLowCardinalityTags() {
return Tags.empty();
}

/**
* Default high cardinality tags.
*
* @return tags
*/
default Tags getHighCardinalityTags() {
return Tags.empty();
}

/**
* Default low cardinality tags.
*
* @return tags
*/
default Tags getAllTags() {
return Tags.concat(getLowCardinalityTags(), getHighCardinalityTags());
return Tags.concat(getLowCardinalityTags(), getHighCardinalityTags()).and(getAdditionalLowCardinalityTags()).and(getAdditionalHighCardinalityTags());
}

/**
* Additional to the default low cardinality tags. Can be set at runtime.
*
* @return tags
*/
default Tags getAdditionalLowCardinalityTags() {
return Tags.empty();
}

/**
* Additional to the default high cardinality tags. Can be set at runtime.
*
* @return tags
*/
default Tags getAdditionalHighCardinalityTags() {
return Tags.empty();
}
}
Loading