Skip to content

Commit

Permalink
fix(MonitoringSubsystem): move meter creation to ComponentSystem.preB…
Browse files Browse the repository at this point in the history
…egin

EngineSubsystem initialization is too early to see all the systems we want to monitor.
  • Loading branch information
keturn committed Dec 19, 2021
1 parent e34cd75 commit 3d7ba24
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import org.terasology.engine.core.subsystem.EngineSubsystem;
import org.terasology.engine.monitoring.gui.AdvancedMonitor;
import reactor.core.publisher.Flux;
import reactor.function.TupleUtils;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.time.Duration;
import java.util.List;
Expand All @@ -45,6 +42,12 @@ public String getName() {
return "Monitoring";
}

@Override
public void preInitialise(Context rootContext) {
// FIXME: `@Share` is not implemented for EngineSubsystems?
rootContext.put(MonitoringSubsystem.class, this);
}

@Override
public void initialise(GameEngine engine, Context rootContext) {
if (rootContext.get(SystemConfig.class).monitoringEnabled.get()) {
Expand All @@ -54,11 +57,6 @@ public void initialise(GameEngine engine, Context rootContext) {
meterRegistry = initMeterRegistries();
}

@Override
public void postInitialise(Context context) {
initMeters(context);
}

/**
* Initialize Micrometer metrics and publishers.
*
Expand Down Expand Up @@ -95,7 +93,7 @@ public Duration step() {
}

/** Initialize meters for all the things in this Context. */
protected void initMeters(Context context) {
public void initMeters(Context context) {
// We can build meters individually like this:
var time = context.get(Time.class);
Gauge.builder("terasology.fps", time::getFps)
Expand All @@ -117,15 +115,17 @@ protected void initMeters(Context context) {
}

protected void registerForContext(Context context, Iterable<GaugeMapEntry> gaugeMap) {
Flux.fromIterable(gaugeMap)
.map(entry -> Tuples.of(context.get(entry.iface), entry.gaugeSpecs))
.filter(TupleUtils.predicate((subject, specs) -> subject != null))
.doOnDiscard(Tuple2.class, TupleUtils.consumer((iface, gaugeSpecs) ->
logger.debug("Not building gauges for {}, none was in {}", iface, context)))
.subscribe(TupleUtils.consumer(this::registerAll));
for (GaugeMapEntry entry : gaugeMap) {
var subject = context.get(entry.iface);
if (subject != null) {
registerAll(subject, entry.gaugeSpecs);
} else {
logger.warn("Not building gauges for {}, none was in {}", entry.iface, context);
}
}
}

protected <T> void registerAll(T subject, Set<GaugeSpec<? extends T>> gaugeSpecs) {
public <T> void registerAll(T subject, Set<GaugeSpec<? extends T>> gaugeSpecs) {
Flux.fromIterable(gaugeSpecs)
.filter(spec -> spec.isInstanceOfType(subject))
// Make sure the gauge is right for the specific type.
Expand All @@ -134,6 +134,7 @@ protected <T> void registerAll(T subject, Set<GaugeSpec<? extends T>> gaugeSpecs
}

public void registerMeter(MeterBinder meterBinder) {
logger.debug("Binding {} to {}", meterBinder, meterRegistry);
meterBinder.bindTo(meterRegistry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package org.terasology.engine.rendering.nui.layers.ingame.metrics;

import com.google.common.base.Preconditions;
import org.terasology.engine.context.Context;
import org.terasology.engine.core.subsystem.common.MonitoringSubsystem;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.gestalt.module.sandbox.API;
import org.terasology.engine.registry.In;
import org.terasology.engine.registry.Share;
import org.terasology.gestalt.module.sandbox.API;

import java.util.ArrayList;

Expand All @@ -25,6 +28,12 @@
@API
public class DebugMetricsSystem extends BaseComponentSystem {

@In
MonitoringSubsystem monitoringSubsystem;

@In
Context context;

private final MetricsMode defaultMode = new NullMetricsMode();
private ArrayList<MetricsMode> modes;
private MetricsMode currentMode;
Expand All @@ -43,8 +52,21 @@ public void initialise() {
register(new HeapAllocationMode());
register(new RenderingExecTimeMeansMode("\n- Rendering - Execution Time: Running Means - Sorted Alphabetically -"));
currentMode = defaultMode;

if (monitoringSubsystem == null) {
monitoringSubsystem = context.get(MonitoringSubsystem.class);
}
}

@Override
public void preBegin() {
monitoringSubsystem.initMeters(context);
}

@Override
public void shutdown() {
// FIXME: Remove all the meters we added to the global registry.
}

/**
* Adds a MetricsMode instance to the set. Use the toggle() and getCurrentMode() methods to iterate over the set and
Expand Down

0 comments on commit 3d7ba24

Please sign in to comment.