Add derived gauge example.#1110
Conversation
examples/derived_gauges/README.md
Outdated
| Table of Contents | ||
| ================= | ||
| - [Summary](#summary) | ||
| - [Run the example.](#run-the-example) |
examples/derived_gauges/README.md
Outdated
| when the queue was consumed. It is represented using derived gauge float64. | ||
| This example shows how to use gauge metrics. The program records two gauges. | ||
|
|
||
| These metrics are read when exporter scrapes them. In these example prometheus exporter is used to |
examples/derived_gauges/README.md
Outdated
|
|
||
| Enter different value for number of items to queue and fetch the metrics using above url to see the variation in the metrics. | ||
|
|
||
| ## Run the example. |
examples/derived_gauges/README.md
Outdated
| metric.WithDescription("Instantaneous queue size"), | ||
| metric.WithUnit(metricdata.UnitDimensionless)) | ||
| if err != nil { | ||
| log.Fatalf("error creating queue size derived gauge, error%v\n", err) |
There was a problem hiding this comment.
Nit: add a space between "error" and "%v"
examples/derived_gauges/README.md
Outdated
| metric.WithDescription("time elapsed since last time the queue was processed"), | ||
| metric.WithUnit(metricdata.UnitDimensionless)) | ||
| if err != nil { | ||
| log.Fatalf("error creating queue_seconds_since_processed_last derived gauge, error%v\n", err) |
examples/derived_gauges/README.md
Outdated
| ```go | ||
| err = queueSizeGauge.UpsertEntry(q.Size) | ||
| if err != nil { | ||
| log.Fatalf("error getting queue size derived gauge entry, error%v\n", err) |
examples/derived_gauges/README.md
Outdated
| ```go | ||
| err = elapsedSeconds.UpsertEntry(q.Elapsed) | ||
| if err != nil { | ||
| log.Fatalf("error getting queue_seconds_since_processed_last derived gauge entry, error%v\n", err) |
|
|
||
| // Command stats implements the stats Quick Start example from: | ||
| // https://opencensus.io/quickstart/go/metrics/ | ||
| // START entire |
There was a problem hiding this comment.
Add a line, otherwise START entire will be a part of the godoc.
| "go.opencensus.io/metric/metricdata" | ||
| "go.opencensus.io/metric/metricproducer" | ||
| "net/http" | ||
| "os" |
There was a problem hiding this comment.
Standard library imports should be in the first section.
Separate standard library imports from go.opencensus.io imports with a line.
There was a problem hiding this comment.
i relied on gofmt to catch this but it doesn't. fixed it.
| "strings" | ||
| ) | ||
|
|
||
| // This example demonstrates the use of derived gauges. It is a simple interactive program of consumer |
There was a problem hiding this comment.
You can put this explanation as the package doc, so it is visible on godoc.
| size int | ||
| q []int | ||
| lastConsumed time.Time | ||
| mu sync.Mutex |
There was a problem hiding this comment.
In Go, mutex is often declared right above the field the mutex is protecting.
Example:
type queue struct {
size int
lastConsumed time.Time
mu sync.Mutex // guards mutex
q []int
}
| fmt.Printf("queued %d items, queue size is %d\n", count, q.size) | ||
| } | ||
|
|
||
| func (q *queue) runConsumer(interval int, cQuit chan bool) { |
There was a problem hiding this comment.
In Go, don't use int for duration. Use time.Duration instead. So the unit of the value is not ambiguous.
| fmt.Printf("queued %d items, queue size is %d\n", count, q.size) | ||
| } | ||
|
|
||
| func (q *queue) runConsumer(interval int, cQuit chan bool) { |
There was a problem hiding this comment.
s/cQuit/quit
quit is a common variable name for closing channels.
|
|
||
| cQuit := make(chan bool) | ||
| defer func() { | ||
| cQuit <- true |
There was a problem hiding this comment.
You don't need to send true, if you close channel that will be sufficient for the select.
|
@rakyll thanks for the review. I'll open another PR to fix all of them. |
* Add derived gauge example. * fix fmt error and unreachable code error. * fix typos.
* Add derived gauge example. * fix fmt error and unreachable code error. * fix typos.
No description provided.