Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Add derived gauge example.#1110

Merged
rghetia merged 3 commits intocensus-instrumentation:devfrom
rghetia:derived_example
Apr 16, 2019
Merged

Add derived gauge example.#1110
rghetia merged 3 commits intocensus-instrumentation:devfrom
rghetia:derived_example

Conversation

@rghetia
Copy link
Copy Markdown
Contributor

@rghetia rghetia commented Apr 16, 2019

No description provided.

@rghetia rghetia requested review from a team and rakyll as code owners April 16, 2019 05:50
@rghetia rghetia requested a review from songy23 April 16, 2019 17:03
Table of Contents
=================
- [Summary](#summary)
- [Run the example.](#run-the-example)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the period?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/these/this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the period?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

metric.WithDescription("Instantaneous queue size"),
metric.WithUnit(metricdata.UnitDimensionless))
if err != nil {
log.Fatalf("error creating queue size derived gauge, error%v\n", err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add a space between "error" and "%v"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

```go
err = queueSizeGauge.UpsertEntry(q.Size)
if err != nil {
log.Fatalf("error getting queue size derived gauge entry, error%v\n", err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

```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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@rghetia rghetia merged commit ad39e72 into census-instrumentation:dev Apr 16, 2019
@rghetia rghetia deleted the derived_example branch April 16, 2019 20:35

// Command stats implements the stats Quick Start example from:
// https://opencensus.io/quickstart/go/metrics/
// START entire
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a line, otherwise START entire will be a part of the godoc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
"net/http"
"os"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard library imports should be in the first section.

Separate standard library imports from go.opencensus.io imports with a line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put this explanation as the package doc, so it is visible on godoc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

size int
q []int
lastConsumed time.Time
mu sync.Mutex
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

fmt.Printf("queued %d items, queue size is %d\n", count, q.size)
}

func (q *queue) runConsumer(interval int, cQuit chan bool) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Go, don't use int for duration. Use time.Duration instead. So the unit of the value is not ambiguous.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

fmt.Printf("queued %d items, queue size is %d\n", count, q.size)
}

func (q *queue) runConsumer(interval int, cQuit chan bool) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/cQuit/quit

quit is a common variable name for closing channels.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


cQuit := make(chan bool)
defer func() {
cQuit <- true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to send true, if you close channel that will be sufficient for the select.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@rghetia
Copy link
Copy Markdown
Contributor Author

rghetia commented Apr 17, 2019

@rakyll thanks for the review. I'll open another PR to fix all of them.

rghetia added a commit to rghetia/opencensus-go that referenced this pull request Apr 17, 2019
@rghetia rghetia mentioned this pull request Apr 24, 2019
2 tasks
rghetia added a commit to rghetia/opencensus-go that referenced this pull request Apr 25, 2019
* Add derived gauge example.

* fix fmt error and unreachable code error.

* fix typos.
rghetia added a commit to rghetia/opencensus-go that referenced this pull request Apr 25, 2019
rghetia added a commit that referenced this pull request Apr 25, 2019
* Add derived gauge example.

* fix fmt error and unreachable code error.

* fix typos.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants