mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-26 08:56:54 +00:00
Add support for arbitrary trackers
This is used by cc-prometheus to monitor statistics. It isn't currently exposed as an API, as I'm making no guarantees on the stability of this.
This commit is contained in:
parent
911e404bfa
commit
e2f9ddd534
@ -0,0 +1,10 @@
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
|
||||
public interface Tracker
|
||||
{
|
||||
void addTiming( Computer computer, long time );
|
||||
|
||||
void addValue( Computer computer, TrackingField field, long change );
|
||||
}
|
@ -2,18 +2,23 @@ package dan200.computercraft.core.tracking;
|
||||
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Tracking
|
||||
{
|
||||
static final AtomicInteger tracking = new AtomicInteger( 0 );
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static final HashMap<UUID, TrackingContext> contexts = new HashMap<>();
|
||||
private static final List<Tracker> trackers = new ArrayList<>();
|
||||
|
||||
public static TrackingContext getContext( UUID uuid )
|
||||
{
|
||||
synchronized( contexts )
|
||||
synchronized( lock )
|
||||
{
|
||||
TrackingContext context = contexts.get( uuid );
|
||||
if( context == null ) contexts.put( uuid, context = new TrackingContext() );
|
||||
@ -21,6 +26,15 @@ public class Tracking
|
||||
}
|
||||
}
|
||||
|
||||
public static void add( Tracker tracker )
|
||||
{
|
||||
synchronized( lock )
|
||||
{
|
||||
trackers.add( tracker );
|
||||
tracking.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addTiming( Computer computer, long time )
|
||||
{
|
||||
if( tracking.get() == 0 ) return;
|
||||
@ -28,6 +42,7 @@ public class Tracking
|
||||
synchronized( contexts )
|
||||
{
|
||||
for( TrackingContext context : contexts.values() ) context.addTiming( computer, time );
|
||||
for( Tracker tracker : trackers ) tracker.addTiming( computer, time );
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,17 +50,19 @@ public class Tracking
|
||||
{
|
||||
if( tracking.get() == 0 ) return;
|
||||
|
||||
synchronized( contexts )
|
||||
synchronized( lock )
|
||||
{
|
||||
for( TrackingContext context : contexts.values() ) context.addValue( computer, field, change );
|
||||
for( Tracker tracker : trackers ) tracker.addValue( computer, field, change );
|
||||
}
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
{
|
||||
synchronized( contexts )
|
||||
synchronized( lock )
|
||||
{
|
||||
contexts.clear();
|
||||
trackers.clear();
|
||||
tracking.set( 0 );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user