# Counters

Counters are integer values you can increment and decrement.

They can be useful for tracking things that you don't have a particular list of, but you do control when they're added/removed/opened/closed.

For example: it's not necessarily very easy to get a list of all the open HTTP connections for a web server, but it's often easy to wrap some "middleware" that increments the "open requests" counter when a request comes in and decrements it when the response goes out.

They can also be useful for tracking simple "total" values like "total requests served in this app's entire lifetime".

Examples of metrics you might want to track with a counter:

• Number of http requests currently being processed.
• Total number of requests/responses received/sent.

TODO: More examples.

## Creating

(use '[metrics.counters :only (counter)])

(def users-connected (counter "users-connected"))


You can also use the defcounter macro to create a counter and bind it to a var in one concise, easy step:

(use '[metrics.counters :only (defcounter)])

(defcounter users-connected)


All the def[metric] macros do some :ref:magic <desugaring> to the metric title to make it easier to define.

## Writing

Once you have a counter you can increment it and decrement it.

### inc!

Increment counters with inc!. You can pass a number to increment it by, or omit it to increment by 1:

(use '[metrics.counters :only (inc!)])

(inc! users-connected)
(inc! users-connected 2)


### dec!

Decrement counters with dec!. You can pass a number to decrement it by, or omit it to decrement by 1:

(use '[metrics.counters :only (dec!)])

(dec! users-connected)
(dec! users-connected 2)


There's only one way to get data from a counter.

### value

You can get the current value of a counter with value:

(use '[metrics.counters :only (value)])

(value users-connected)

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.