-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MonitoringSubsystem): provide Micrometer gauges for rendering.world
- Loading branch information
Showing
6 changed files
with
235 additions
and
27 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
engine/src/main/java/org/terasology/engine/core/subsystem/common/GaugeMapEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.core.subsystem.common; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* Describes the set of gauges that may apply to a particular interface. | ||
*/ | ||
public class GaugeMapEntry { | ||
public final Class<?> iface; | ||
public final Set<GaugeSpec<?>> gaugeSpecs; | ||
|
||
@SafeVarargs | ||
public <T> GaugeMapEntry(Class<T> iface, GaugeSpec<? extends T>... gaugeSpecs) { | ||
this.iface = iface; | ||
this.gaugeSpecs = Set.of(gaugeSpecs); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
engine/src/main/java/org/terasology/engine/core/subsystem/common/GaugeSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.core.subsystem.common; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.binder.MeterBinder; | ||
|
||
import java.util.function.ToDoubleFunction; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
/** | ||
* The information that defines a Gauge. | ||
* <p> | ||
* The Micrometer API doesn't let you define a Gauge without connecting it to | ||
* some MeterRegistry. This class provides an immutable record of all<sup>*</sup> | ||
* the properties of a Gauge, facilitating a more data-driven approach. | ||
* <p> | ||
* * <i>All the ones we use so far, anyway.</i> | ||
* | ||
* @param <T> the type this gauge reads from | ||
*/ | ||
public class GaugeSpec<T> { | ||
public final String name; | ||
public final String description; | ||
public final ToDoubleFunction<T> valueFunction; | ||
public final String baseUnit; | ||
|
||
protected final Class<T> subjectType; | ||
|
||
public GaugeSpec(String name, String description, ToDoubleFunction<T> valueFunction) { | ||
this(name, description, valueFunction, null); | ||
} | ||
|
||
/** @see Gauge.Builder */ | ||
public GaugeSpec(String name, String description, ToDoubleFunction<T> valueFunction, String baseUnit) { | ||
this.name = name; | ||
this.description = description; | ||
this.valueFunction = valueFunction; | ||
this.baseUnit = baseUnit; | ||
this.subjectType = getSubjectClass(); | ||
} | ||
|
||
public Gauge register(MeterRegistry registry, T subject) { | ||
return Gauge.builder(name, subject, valueFunction) | ||
.description(description) | ||
.baseUnit(baseUnit) | ||
.register(registry); | ||
} | ||
|
||
/** | ||
* Creates a MeterBinder for this gauge. | ||
* <p> | ||
* This allows us to make things with the same interface as the meters | ||
* provided by {@link io.micrometer.core.instrument.binder}. | ||
* | ||
* @param subject passed to this gauge's {@link #valueFunction} | ||
* @return call to bind this gauge to a MeterRegistry | ||
*/ | ||
public MeterBinder binder(T subject) { | ||
return registry -> register(registry, subject); | ||
} | ||
|
||
public <U> MeterBinder binderAfterCasting(U subject) { | ||
checkArgument(isInstanceOfType(subject)); | ||
T safeSubject = subjectType.cast(subject); | ||
return binder(safeSubject); | ||
} | ||
|
||
public boolean isInstanceOfType(Object object) { | ||
return subjectType.isInstance(object); | ||
} | ||
|
||
@SafeVarargs | ||
private Class<T> getSubjectClass(T...t) { | ||
// Thank you https://stackoverflow.com/a/40917725 for this amazing kludge | ||
//noinspection unchecked | ||
return (Class<T>) t.getClass().getComponentType(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
engine/src/main/java/org/terasology/engine/rendering/world/Meters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.rendering.world; | ||
|
||
import io.micrometer.core.instrument.binder.BaseUnits; | ||
import org.terasology.engine.core.subsystem.common.GaugeMapEntry; | ||
import org.terasology.engine.core.subsystem.common.GaugeSpec; | ||
|
||
import java.util.List; | ||
|
||
public final class Meters { | ||
public static final String PREFIX = Meters.class.getPackageName(); | ||
|
||
public static final List<GaugeMapEntry> GAUGE_MAP = List.of( | ||
new GaugeMapEntry(WorldRenderer.class, | ||
new GaugeSpec<WorldRendererImpl>( | ||
PREFIX + ".emptyMeshChunks", | ||
"Empty Mesh Chunks", | ||
wri -> wri.statChunkMeshEmpty, | ||
BaseUnits.OBJECTS), | ||
new GaugeSpec<WorldRendererImpl>( | ||
PREFIX + ".unreadyChunks", | ||
"Unready Chunks", | ||
wri -> wri.statChunkNotReady, | ||
BaseUnits.OBJECTS), | ||
new GaugeSpec<WorldRendererImpl>( | ||
PREFIX + ".triangles", | ||
"Rendered Triangles", | ||
wri -> wri.statRenderedTriangles, | ||
BaseUnits.OBJECTS) | ||
), | ||
new GaugeMapEntry(RenderableWorld.class, | ||
new GaugeSpec<RenderableWorldImpl>( | ||
PREFIX + ".visibleChunks", | ||
"Visible Chunks", | ||
rwi -> rwi.statVisibleChunks, | ||
BaseUnits.OBJECTS), | ||
new GaugeSpec<RenderableWorldImpl>( | ||
PREFIX + ".dirtyChunks", | ||
"Dirty Chunks", | ||
rwi -> rwi.statDirtyChunks, | ||
BaseUnits.OBJECTS), | ||
new GaugeSpec<RenderableWorldImpl>( | ||
PREFIX + ".dirtyChunks", | ||
"Ignored Phases", | ||
rwi -> rwi.statIgnoredPhases) | ||
) | ||
); | ||
|
||
private Meters() { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters