📈 A counter instrument that tracks cumulative values.

Counters are perfect for tracking things that only increase over time, like total requests processed, blocks mined, or items crafted.

The counter maintains its current value and sends updates to the collector whenever you add to it. It also remembers all the metric metadata so you don't have to repeat it with each measurement.

const counter = new RednetCounter(client, "items_processed", "Items processed", "items");

// Add values as work is done
counter.add(1); // Simple increment
counter.add(5, { "type": "ore" }); // Batch with attributes

console.log(`Total processed: ${counter.getValue()}`);

Constructors

Methods

Constructors

  • Create a new counter instrument.

    Parameters

    • client: RednetClient

      RednetClient for sending data

    • name: string

      Counter name

    • description: string

      Human-readable description

    • unit: string

      Unit of measurement

    Returns RednetCounter

Methods

  • 📈 Add a value to the counter.

    The value is added to the current total and immediately sent to the collector. Counter values must be non-negative (you can't subtract from a counter).

    Parameters

    • value: number

      Value to add (must be >= 0)

    • attributes: Record<string, unknown> = {}

      Additional attributes for this measurement

    Returns void

    Error if value is negative

    // Simple increment
    counter.add(1);

    // Batch processing
    counter.add(batchSize, {
    "batch.id": "batch_001",
    "processing.time": processingTime
    });

    // Conditional counting
    if (operation.success) {
    counter.add(1, { "result": "success" });
    }
  • 📊 Get the current counter value.

    Returns number

    The current cumulative value

    const currentTotal = counter.getValue();
    console.log(`Processed ${currentTotal} items so far`);