prometheus.Counter.add()

Adds a float value to the counter for the specified label values.

Syntax

Note:

In this syntax diagram, the square brackets ([ ]) are part of the syntax.

add( value FLOAT, [ labels ] )
  1. value is a value of type FLOAT to add to the counter.
  2. labels is a comma-separated list of label values inside square brackets, one for each label name declared when the metric was created, in the same order. Where the metric declares no label names, use empty brackets [].

Usage

Use the prometheus.Counter.add() method to add a specific float value to the counter metric. Counters can only increase. The label values change from one call to the next. For example, with the label name ["action"] declared, add(engagementTime, ["cancel"]) adds to the series for the cancel action.

Example

IMPORT prometheus

DEFINE counter prometheus.Counter
DEFINE startTime FLOAT
DEFINE endTime FLOAT
DEFINE engagementTime FLOAT
DEFINE product DYNAMIC ARRAY OF RECORD
    name STRING,
    price MONEY(5, 2)
END RECORD

MAIN
    -- Create a counter metric with one label for the display actions
    LET counter = prometheus.Counter.create("counter_dialog_use", "Count dialog use",
                                        ["action"])
    # ... program code 
    -- Record the start time when the dialog is opened
    LET startTime = CURRENT

    DISPLAY ARRAY product TO sr_p.* ATTRIBUTES(UNBUFFERED)
        ON ACTION edit
            OPEN FORM e FROM "edit"
            DISPLAY FORM e
               -- code to handle form actions
            CALL counter.inc(["edit"])
            CLOSE FORM e
        ON ACTION sql
           -- Execute SQL query
           CALL counter.inc(["sql"])
        ON ACTION cancel
            -- Record the end time when the user cancels the dialog
            LET endTime = CURRENT
            LET engagementTime = endTime - startTime
            
            -- Use counter.add to increment by engagement time for the "cancel" action
            CALL counter.add(engagementTime, ["cancel"])
            EXIT DISPLAY
    END DISPLAY
END MAIN