Skip to content

Instantly share code, notes, and snippets.

@stefantalpalaru
Created July 15, 2019 20:03
Show Gist options
  • Save stefantalpalaru/5792229f8faf5df726e27e5df485c742 to your computer and use it in GitHub Desktop.
Save stefantalpalaru/5792229f8faf5df726e27e5df485c742 to your computer and use it in GitHub Desktop.
temp
diff --git a/metrics.nim b/metrics.nim
index b72e498..e474034 100644
--- a/metrics.nim
+++ b/metrics.nim
@@ -34,8 +34,6 @@ type
RegistrationError* = object of Exception
- IgnoredCollector* = object
-
const CONTENT_TYPE* = "text/plain; version=0.0.4; charset=utf-8"
#########
@@ -189,13 +187,13 @@ proc newCounter*(name: string, help: string, labels: Labels = @[], registry = de
result.metrics[labels] = newCounterMetrics(name, labels, labels)
result.register(registry)
-proc validateCounterLabelValues*(counter: Counter, labelValues: Labels): Labels =
+proc validateCounterLabelValues(counter: Counter, labelValues: Labels): Labels =
when defined(metrics):
result = validateLabelValues(counter, labelValues)
if result notin counter.metrics:
counter.metrics[result] = newCounterMetrics(counter.name, counter.labels, result)
-proc incCounter(counter: Counter, amount: int64|float64 = 1, labelValues: Labels = @[]) =
+proc incCounter(counter: Counter | type Counter, amount: int64|float64 = 1, labelValues: Labels = @[]) =
when defined(metrics):
var timestamp = getTime().toMilliseconds()
@@ -207,11 +205,11 @@ proc incCounter(counter: Counter, amount: int64|float64 = 1, labelValues: Labels
atomicAdd(counter.metrics[labelValuesCopy][0].value.addr, amount.float64)
atomicStore(cast[ptr int64](counter.metrics[labelValuesCopy][0].timestamp.addr), timestamp.addr, ATOMIC_SEQ_CST)
-template inc*(counter: Counter, amount: int64|float64 = 1, labelValues: Labels = @[]) =
+template inc*(counter: Counter | type Counter, amount: int64|float64 = 1, labelValues: Labels = @[]) =
when defined(metrics):
{.gcsafe.}: incCounter(counter, amount, labelValues)
-template countExceptions*(counter: Counter, typ: typedesc, labelValues: Labels, body: untyped) =
+template countExceptions*(counter: Counter | type Counter, typ: typedesc, labelValues: Labels, body: untyped) =
when defined(metrics):
try:
body
@@ -221,15 +219,15 @@ template countExceptions*(counter: Counter, typ: typedesc, labelValues: Labels,
else:
body
-template countExceptions*(counter: Counter, typ: typedesc, body: untyped) =
+template countExceptions*(counter: Counter | type Counter, typ: typedesc, body: untyped) =
let labelValues: Labels = @[]
counter.countExceptions(typ, labelValues):
body
-template countExceptions*(counter: Counter, labelValues: Labels, body: untyped) =
+template countExceptions*(counter: Counter | type Counter, labelValues: Labels, body: untyped) =
countExceptions(counter, Exception, labelValues, body)
-template countExceptions*(counter: Counter, body: untyped) =
+template countExceptions*(counter: Counter | type Counter, body: untyped) =
let labelValues: Labels = @[]
counter.countExceptions(labelValues):
body
@@ -241,7 +239,7 @@ template counter*(identifier: untyped,
when defined(metrics):
var identifier = newCounter(astToStr(identifier), help, labels, registry)
else:
- type identifier = IgnoredCollector
+ type identifier = Counter
template publicCounter*(identifier: untyped,
help: static string,
@@ -250,7 +248,7 @@ template publicCounter*(identifier: untyped,
when defined(metrics):
var identifier* = newCounter(astToStr(identifier), help, labels, registry)
else:
- type identifier* = IgnoredCollector
+ type identifier* = Counter
#########
# gauge #
@@ -275,13 +273,13 @@ proc newGauge*(name: string, help: string, labels: Labels = @[], registry = defa
result.metrics[labels] = newGaugeMetrics(name, labels, labels)
result.register(registry)
-proc validateGaugeLabelValues*(gauge: Gauge, labelValues: Labels): Labels =
+proc validateGaugeLabelValues(gauge: Gauge, labelValues: Labels): Labels =
when defined(metrics):
result = validateLabelValues(gauge, labelValues)
if result notin gauge.metrics:
gauge.metrics[result] = newGaugeMetrics(gauge.name, gauge.labels, result)
-proc incGauge(gauge: Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
+proc incGauge(gauge: Gauge | type Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
when defined(metrics):
var timestamp = getTime().toMilliseconds()
@@ -290,11 +288,11 @@ proc incGauge(gauge: Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]
atomicAdd(gauge.metrics[labelValuesCopy][0].value.addr, amount.float64)
atomicStore(cast[ptr int64](gauge.metrics[labelValuesCopy][0].timestamp.addr), timestamp.addr, ATOMIC_SEQ_CST)
-proc decGauge(gauge: Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
+proc decGauge(gauge: Gauge | type Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
when defined(metrics):
gauge.inc((-amount).float64, labelValues)
-proc setGauge(gauge: Gauge, value: int64|float64, labelValues: Labels = @[]) =
+proc setGauge(gauge: Gauge | type Gauge, value: int64|float64, labelValues: Labels = @[]) =
when defined(metrics):
var timestamp = getTime().toMilliseconds()
@@ -303,24 +301,24 @@ proc setGauge(gauge: Gauge, value: int64|float64, labelValues: Labels = @[]) =
atomicStoreN(cast[ptr int64](gauge.metrics[labelValuesCopy][0].value.addr), cast[int64](value.float64), ATOMIC_SEQ_CST)
atomicStore(cast[ptr int64](gauge.metrics[labelValuesCopy][0].timestamp.addr), timestamp.addr, ATOMIC_SEQ_CST)
-template inc*(gauge: Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
+template inc*(gauge: Gauge | type Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
when defined(metrics):
{.gcsafe.}: incGauge(gauge, amount, labelValues)
-template dec*(gauge: Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
+template dec*(gauge: Gauge | type Gauge, amount: int64|float64 = 1, labelValues: Labels = @[]) =
when defined(metrics):
{.gcsafe.}: decGauge(gauge, amount, labelValues)
-template set*(gauge: Gauge, value: int64|float64, labelValues: Labels = @[]) =
+template set*(gauge: Gauge | type Gauge, value: int64|float64, labelValues: Labels = @[]) =
when defined(metrics):
{.gcsafe.}: setGauge(gauge, value, labelValues)
# in seconds
-proc setToCurrentTime*(gauge: Gauge, labelValues: Labels = @[]) =
+proc setToCurrentTime*(gauge: Gauge | type Gauge, labelValues: Labels = @[]) =
when defined(metrics):
gauge.set(getTime().toUnix(), labelValues)
-template trackInProgress*(gauge: Gauge, labelValues: Labels, body: untyped) =
+template trackInProgress*(gauge: Gauge | type Gauge, labelValues: Labels, body: untyped) =
when defined(metrics):
gauge.inc(labelValues = labelValues)
body
@@ -328,13 +326,13 @@ template trackInProgress*(gauge: Gauge, labelValues: Labels, body: untyped) =
else:
body
-template trackInProgress*(gauge: Gauge, body: untyped) =
+template trackInProgress*(gauge: Gauge | type Gauge, body: untyped) =
let labelValues: Labels = @[]
gauge.trackInProgress(labelValues):
body
# in seconds
-template time*(gauge: Gauge, labelValues: Labels, body: untyped) =
+template time*(gauge: Gauge | type Gauge, labelValues: Labels, body: untyped) =
when defined(metrics):
let start = times.toUnix(getTime())
body
@@ -342,7 +340,7 @@ template time*(gauge: Gauge, labelValues: Labels, body: untyped) =
else:
body
-template time*(gauge: Gauge, body: untyped) =
+template time*(gauge: Gauge | type Gauge, body: untyped) =
let labelValues: Labels = @[]
gauge.time(labelValues):
body
@@ -354,7 +352,7 @@ template gauge*(identifier: untyped,
when defined(metrics):
var identifier = newGauge(astToStr(identifier), help, labels, registry)
else:
- type identifier = IgnoredCollector
+ type identifier = Gauge
template publicGauge*(identifier: untyped,
help: static string,
@@ -363,20 +361,7 @@ template publicGauge*(identifier: untyped,
when defined(metrics):
var identifier* = newGauge(astToStr(identifier), help, labels, registry)
else:
- type identifier* = IgnoredCollector
-
-#######################
-# Disabled collectors #
-#######################
-
-template inc*(gauge: type IgnoredCollector, amount: int64|float64 = 1, labelValues: Labels = @[]) =
- discard
-
-template dec*(gauge: type IgnoredCollector, amount: int64|float64 = 1, labelValues: Labels = @[]) =
- discard
-
-template set*(gauge: type IgnoredCollector, value: int64|float64, labelValues: Labels = @[]) =
- discard
+ type identifier* = Gauge
###############
# HTTP server #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment