-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add openmetrics and exemplars support #482
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
const { register, Registry, Counter, Histogram } = require('..'); | ||
|
||
async function makeCounters() { | ||
const c = new Counter({ | ||
name: 'test_counter_exemplar', | ||
help: 'Example of a counter with exemplar', | ||
labelNames: ['code'], | ||
enableExemplars: true, | ||
}); | ||
|
||
const exemplarLabels = { traceId: '888', spanId: 'jjj' }; | ||
|
||
c.inc({ | ||
labels: { code: 300 }, | ||
value: 1, | ||
exemplarLabels, | ||
}); | ||
c.inc({ | ||
labels: { code: 200 }, | ||
exemplarLabels, | ||
}); | ||
|
||
c.inc({ exemplarLabels }); | ||
c.inc(); | ||
} | ||
|
||
async function makeHistograms() { | ||
const h = new Histogram({ | ||
name: 'test_histogram_exemplar', | ||
help: 'Example of a histogram with exemplar', | ||
labelNames: ['code'], | ||
enableExemplars: true, | ||
}); | ||
|
||
const exemplarLabels = { traceId: '111', spanId: 'zzz' }; | ||
|
||
h.observe({ | ||
labels: { code: '200' }, | ||
value: 1, | ||
exemplarLabels, | ||
}); | ||
|
||
h.observe({ | ||
labels: { code: '200' }, | ||
value: 3, | ||
exemplarLabels, | ||
}); | ||
|
||
h.observe({ | ||
labels: { code: '200' }, | ||
value: 0.3, | ||
exemplarLabels, | ||
}); | ||
|
||
h.observe({ | ||
labels: { code: '200' }, | ||
value: 300, | ||
exemplarLabels, | ||
}); | ||
} | ||
|
||
async function main() { | ||
// exemplars will be shown only by OpenMetrics registry types | ||
register.setContentType(Registry.OPENMETRICS_CONTENT_TYPE); | ||
|
||
makeCounters(); | ||
makeHistograms(); | ||
|
||
console.log(await register.metrics()); | ||
console.log('---'); | ||
|
||
// if you dont want to set the default registry to OpenMetrics type then you need to create a new registry and assign it to the metric | ||
|
||
register.setContentType(Registry.PROMETHEUS_CONTENT_TYPE); | ||
const omReg = new Registry(Registry.OPENMETRICS_CONTENT_TYPE); | ||
const c = new Counter({ | ||
name: 'counter_with_exemplar', | ||
help: 'Example of a counter', | ||
labelNames: ['code'], | ||
registers: [omReg], | ||
enableExemplars: true, | ||
}); | ||
c.inc({ labels: { code: '200' }, exemplarLabels: { traceId: 'traceA' } }); | ||
console.log(await omReg.metrics()); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as prom from '../index'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need both a JS and a TS example - could you remove this one? |
||
|
||
async function prometheusRegistry() { | ||
let reg = new prom.Registry(); | ||
|
||
let counter = new prom.Counter({ | ||
name: 'test_counter', | ||
help: 'counter help message', | ||
registers: [reg], | ||
labelNames: ['code'], | ||
}); | ||
|
||
let hist = new prom.Histogram({ | ||
name: 'test_histogram', | ||
help: 'histogram help message', | ||
registers: [reg], | ||
labelNames: ['code'], | ||
}); | ||
|
||
counter.inc({ code: '300' }, 2); | ||
hist.observe({ code: '200' }, 1); | ||
|
||
console.log(await reg.metrics()); | ||
} | ||
|
||
async function openMetricsRegistry() { | ||
let reg = new prom.Registry<prom.OpenMetricsContentType>(); | ||
reg.setContentType(prom.openMetricsContentType); | ||
|
||
let counter = new prom.Counter({ | ||
name: 'test_counter', | ||
help: 'counter help message', | ||
registers: [reg], | ||
labelNames: ['code'], | ||
enableExemplars: true, | ||
}); | ||
|
||
let hist = new prom.Histogram({ | ||
name: 'test_histogram', | ||
help: 'histogram help message', | ||
registers: [reg], | ||
labelNames: ['code'], | ||
enableExemplars: true, | ||
}); | ||
|
||
counter.inc(<prom.IncreaseDataWithExemplar<string>>{ | ||
value: 2, | ||
labels: { code: '300' }, | ||
exemplarLabels: { traceID: 'traceA' }, | ||
}); | ||
|
||
hist.observe(<prom.ObserveDataWithExemplar<string>>{ | ||
value: 1, | ||
labels: { code: '200' }, | ||
exemplarLabels: { traceID: 'traceA' }, | ||
}); | ||
|
||
console.log(await reg.metrics()); | ||
} | ||
|
||
async function main() { | ||
prometheusRegistry(); | ||
openMetricsRegistry(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,27 @@ | ||||||
// Type definitions for prom-client | ||||||
// Definitions by: Simon Nyberg http://twitter.com/siimon_nyberg | ||||||
|
||||||
export type Charset = 'utf-8'; | ||||||
|
||||||
export type PrometheusMIME = 'text/plain'; | ||||||
export type PrometheusMetricsVersion = '0.0.4'; | ||||||
|
||||||
export type OpenMetricsMIME = 'application/openmetrics-text'; | ||||||
export type OpenMetricsVersion = '1.0.0'; | ||||||
|
||||||
export type PrometheusContentType = | ||||||
`${OpenMetricsMIME}; version=${OpenMetricsVersion}; charset=${Charset}`; | ||||||
export type OpenMetricsContentType = | ||||||
`${PrometheusMIME}; version=${PrometheusMetricsVersion}; charset=${Charset}`; | ||||||
|
||||||
export type RegistryContentType = | ||||||
| PrometheusContentType | ||||||
| OpenMetricsContentType; | ||||||
|
||||||
/** | ||||||
* Container for all registered metrics | ||||||
*/ | ||||||
export class Registry { | ||||||
export class Registry<RegistryContentType = PrometheusContentType> { | ||||||
/** | ||||||
* Get string representation for all metrics | ||||||
*/ | ||||||
|
@@ -64,7 +81,14 @@ export class Registry { | |||||
/** | ||||||
* Gets the Content-Type of the metrics for use in the response headers. | ||||||
*/ | ||||||
contentType: string; | ||||||
contentType: RegistryContentType; | ||||||
|
||||||
/** | ||||||
* Set the content type of a registry. Used to change between Prometheus and | ||||||
* OpenMetrics versions. | ||||||
* @param contentType The type of the registry | ||||||
*/ | ||||||
setContentType(contentType: RegistryContentType): void; | ||||||
|
||||||
/** | ||||||
* Merge registers | ||||||
|
@@ -80,9 +104,20 @@ export type Collector = () => void; | |||||
export const register: Registry; | ||||||
|
||||||
/** | ||||||
* The Content-Type of the metrics for use in the response headers. | ||||||
* HTTP Content-Type for metrics response headers, defaults to Prometheus text | ||||||
* format. | ||||||
*/ | ||||||
export const contentType: RegistryContentType; | ||||||
|
||||||
/** | ||||||
* HTTP Prometheus Content-Type for metrics response headers. | ||||||
*/ | ||||||
export const prometheusContentType: PrometheusContentType; | ||||||
|
||||||
/** | ||||||
* HTTP OpenMetrics Content-Type for metrics response headers. | ||||||
*/ | ||||||
export const contentType: string; | ||||||
export const openMetricsContentType: OpenMetricsContentType; | ||||||
|
||||||
export class AggregatorRegistry extends Registry { | ||||||
/** | ||||||
|
@@ -150,16 +185,29 @@ interface MetricConfiguration<T extends string> { | |||||
name: string; | ||||||
help: string; | ||||||
labelNames?: T[] | readonly T[]; | ||||||
registers?: Registry[]; | ||||||
registers?: Registry<PrometheusContentType | OpenMetricsContentType>[]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
right? A registry itself cannot contain either |
||||||
aggregator?: Aggregator; | ||||||
collect?: CollectFunction<any>; | ||||||
enableExemplars?: boolean; | ||||||
} | ||||||
|
||||||
export interface CounterConfiguration<T extends string> | ||||||
extends MetricConfiguration<T> { | ||||||
collect?: CollectFunction<Counter<T>>; | ||||||
} | ||||||
|
||||||
export interface IncreaseDataWithExemplar<T extends string> { | ||||||
value?: number; | ||||||
labels?: LabelValues<T>; | ||||||
exemplarLabels?: LabelValues<T>; | ||||||
} | ||||||
|
||||||
export interface ObserveDataWithExemplar<T extends string> { | ||||||
value: number; | ||||||
labels?: LabelValues<T>; | ||||||
exemplarLabels?: LabelValues<T>; | ||||||
} | ||||||
|
||||||
/** | ||||||
* A counter is a cumulative metric that represents a single numerical value that only ever goes up | ||||||
*/ | ||||||
|
@@ -182,6 +230,12 @@ export class Counter<T extends string = string> { | |||||
*/ | ||||||
inc(value?: number): void; | ||||||
|
||||||
/** | ||||||
* Increment with exemplars | ||||||
* @param incData Object with labels, value and exemplars for an increase | ||||||
*/ | ||||||
inc(incData: IncreaseDataWithExemplar<T>): void; | ||||||
|
||||||
/** | ||||||
* Return the child for given labels | ||||||
* @param values Label values | ||||||
|
@@ -386,6 +440,12 @@ export class Histogram<T extends string = string> { | |||||
*/ | ||||||
observe(labels: LabelValues<T>, value: number): void; | ||||||
|
||||||
/** | ||||||
* Observe with exemplars | ||||||
* @param observeData Object with labels, value and exemplars for an observation | ||||||
*/ | ||||||
observe(observeData: ObserveDataWithExemplar<T>): void; | ||||||
|
||||||
/** | ||||||
* Start a timer. Calling the returned function will observe the duration in | ||||||
* seconds in the histogram. | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, can we throw a usweful error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/siimon/prom-client/pull/482/files#diff-c76cf141522a047fee59ae738cbc8a0dc2c48ed15666d958d407d867c3a4b69fR237
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs should be updated to say it throws, no? Not undefined behavior