mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-18 03:01:05 +01:00
Metrics for events (#1601)
Updates legman to version 2, which allows the usage of the MicrometerPlugin. The plugin will collect metrics for subscriber invocations and the underlying executor. Furthermore this change will fix the usage of wrong subject context in the asynchronous events.
This commit is contained in:
@@ -21,66 +21,72 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.event;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.github.legman.EventBus;
|
||||
import com.github.legman.Subscribe;
|
||||
import com.github.legman.micrometer.MicrometerPlugin;
|
||||
import com.github.legman.shiro.ShiroPlugin;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.metrics.MeterRegistryProvider;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class LegmanScmEventBus extends ScmEventBus
|
||||
{
|
||||
public class LegmanScmEventBus extends ScmEventBus {
|
||||
|
||||
private static final AtomicLong INSTANCE_COUNTER = new AtomicLong();
|
||||
|
||||
|
||||
/** Field description */
|
||||
private static final String NAME = "ScmEventBus-%s";
|
||||
private static final String FORMAT_NAME = "ScmEventBus-%s";
|
||||
|
||||
/**
|
||||
* the logger for LegmanScmEventBus
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(LegmanScmEventBus.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
private static final Logger logger = LoggerFactory.getLogger(LegmanScmEventBus.class);
|
||||
|
||||
private String name;
|
||||
private EventBus eventBus;
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public LegmanScmEventBus() {
|
||||
eventBus = create();
|
||||
eventBus = create(null);
|
||||
}
|
||||
|
||||
private EventBus create() {
|
||||
name = String.format(NAME, INSTANCE_COUNTER.incrementAndGet());
|
||||
@VisibleForTesting
|
||||
LegmanScmEventBus(Executor executor) {
|
||||
eventBus = create(executor);
|
||||
}
|
||||
|
||||
private EventBus create(Executor executor) {
|
||||
name = String.format(FORMAT_NAME, INSTANCE_COUNTER.incrementAndGet());
|
||||
logger.info("create new event bus {}", name);
|
||||
return new EventBus(name);
|
||||
|
||||
MicrometerPlugin micrometerPlugin = new MicrometerPlugin(MeterRegistryProvider.getStaticRegistry())
|
||||
.withExecutorTags(Tag.of("type", "fixed"));
|
||||
|
||||
ShiroPlugin shiroPlugin = new ShiroPlugin();
|
||||
|
||||
EventBus.Builder builder = EventBus.builder()
|
||||
.withIdentifier(name)
|
||||
.withPlugins(shiroPlugin, micrometerPlugin);
|
||||
|
||||
if (executor != null) {
|
||||
builder.withExecutor(executor);
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
@Override
|
||||
public void post(Object event)
|
||||
{
|
||||
public void post(Object event) {
|
||||
if (eventBus != null) {
|
||||
logger.debug("post {} to event bus {}", event, name);
|
||||
eventBus.post(event);
|
||||
@@ -93,12 +99,9 @@ public class LegmanScmEventBus extends ScmEventBus
|
||||
* Registers a object to the eventbus.
|
||||
*
|
||||
* @param object object to register
|
||||
*
|
||||
* @see {@link #register(java.lang.Object)}
|
||||
*/
|
||||
@Override
|
||||
public void register(Object object)
|
||||
{
|
||||
public void register(Object object) {
|
||||
if (eventBus != null) {
|
||||
logger.trace("register {} to event bus {}", object, name);
|
||||
eventBus.register(object);
|
||||
@@ -107,15 +110,8 @@ public class LegmanScmEventBus extends ScmEventBus
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
*
|
||||
* @param object
|
||||
*/
|
||||
@Override
|
||||
public void unregister(Object object)
|
||||
{
|
||||
public void unregister(Object object) {
|
||||
if (eventBus != null) {
|
||||
logger.trace("unregister {} from event bus {}", object, name);
|
||||
|
||||
@@ -147,11 +143,7 @@ public class LegmanScmEventBus extends ScmEventBus
|
||||
eventBus.shutdown();
|
||||
}
|
||||
logger.info("recreate event bus because of received RecreateEventBusEvent");
|
||||
eventBus = create();
|
||||
eventBus = create(null);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** event bus */
|
||||
private EventBus eventBus;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
|
||||
package sonia.scm.metrics;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import io.micrometer.core.instrument.Meter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
|
||||
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
|
||||
@@ -37,11 +40,18 @@ import org.slf4j.LoggerFactory;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Singleton
|
||||
public class MeterRegistryProvider implements Provider<MeterRegistry> {
|
||||
|
||||
private static final CompositeMeterRegistry staticRegistry = new CompositeMeterRegistry();
|
||||
|
||||
public static MeterRegistry getStaticRegistry() {
|
||||
return staticRegistry;
|
||||
}
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MeterRegistryProvider.class);
|
||||
|
||||
private final Set<MonitoringSystem> providerSet;
|
||||
@@ -49,6 +59,19 @@ public class MeterRegistryProvider implements Provider<MeterRegistry> {
|
||||
@Inject
|
||||
public MeterRegistryProvider(Set<MonitoringSystem> providerSet) {
|
||||
this.providerSet = providerSet;
|
||||
resetStaticRegistry();
|
||||
}
|
||||
|
||||
private void resetStaticRegistry() {
|
||||
Set<MeterRegistry> registries = ImmutableSet.copyOf(staticRegistry.getRegistries());
|
||||
for (MeterRegistry registry : registries) {
|
||||
staticRegistry.remove(registry);
|
||||
}
|
||||
|
||||
List<Meter> meters = ImmutableList.copyOf(staticRegistry.getMeters());
|
||||
for (Meter meter : meters) {
|
||||
staticRegistry.remove(meter);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,6 +87,7 @@ public class MeterRegistryProvider implements Provider<MeterRegistry> {
|
||||
if (providerSet.size() == 1) {
|
||||
MeterRegistry registry = providerSet.iterator().next().getRegistry();
|
||||
LOG.debug("create meter registry from single registration: {}", registry.getClass());
|
||||
staticRegistry.add(registry);
|
||||
return registry;
|
||||
}
|
||||
return createCompositeRegistry();
|
||||
@@ -71,13 +95,12 @@ public class MeterRegistryProvider implements Provider<MeterRegistry> {
|
||||
|
||||
private CompositeMeterRegistry createCompositeRegistry() {
|
||||
LOG.debug("create composite meter registry");
|
||||
CompositeMeterRegistry registry = new CompositeMeterRegistry();
|
||||
for (MonitoringSystem provider : providerSet) {
|
||||
MeterRegistry subRegistry = provider.getRegistry();
|
||||
LOG.debug("register {} as part of composite meter registry", subRegistry.getClass());
|
||||
registry.add(subRegistry);
|
||||
staticRegistry.add(subRegistry);
|
||||
}
|
||||
return registry;
|
||||
return staticRegistry;
|
||||
}
|
||||
|
||||
@SuppressWarnings("java:S2095") // we can't close JvmGcMetrics, but it should be ok
|
||||
|
||||
Reference in New Issue
Block a user