Fix comment received for #1110 after it was merged. (#1113)
diff --git a/examples/derived_gauges/README.md b/examples/derived_gauges/README.md index 3315d38..9dda680 100644 --- a/examples/derived_gauges/README.md +++ b/examples/derived_gauges/README.md
@@ -140,40 +140,44 @@ [embedmd]:# (derived_gauge.go entire) ```go + +// This example demonstrates the use of derived gauges. It is a simple interactive program of consumer +// and producer. User can input number of items to produce. Producer produces specified number of +// items. Consumer randomly consumes 1-5 items in each attempt. It then sleeps randomly +// between 1-10 seconds before the next attempt. Two metrics collected to monitor the queue. +// +// Metrics +// +// * queue_size: It is an instantaneous queue size represented using derived gauge int64. +// +// * queue_seconds_since_processed_last: It is the time elaspsed in seconds since the last time +// when the queue was consumed. It is represented using derived gauge float64. package main import ( + "bufio" "fmt" "log" "math/rand" - "sync" - "time" - - "bufio" - "go.opencensus.io/exporter/prometheus" - "go.opencensus.io/metric" - "go.opencensus.io/metric/metricdata" - "go.opencensus.io/metric/metricproducer" "net/http" "os" "strconv" "strings" + "sync" + "time" + + "go.opencensus.io/exporter/prometheus" + "go.opencensus.io/metric" + "go.opencensus.io/metric/metricdata" + "go.opencensus.io/metric/metricproducer" ) -// This example demonstrates the use of derived gauges. It is a simple interactive program of consumer -// and producer. User can input number of items to produce. Producer produces specified number of -// items. Consumer consumes randomly consumes 1-5 items in each attempt. It then sleeps randomly -// between 1-10 seconds before the next attempt. -// -// There are two metrics collected to monitor the queue. -// 1. queue_size: It is an instantaneous queue size represented using derived gauge int64. -// 2. queue_seconds_since_processed_last: It is the time elaspsed in seconds since the last time -// when the queue was consumed. It is represented using derived gauge float64. type queue struct { size int - q []int lastConsumed time.Time - mu sync.Mutex + + mu sync.Mutex + q []int } var q = &queue{} @@ -219,8 +223,8 @@ fmt.Printf("queued %d items, queue size is %d\n", count, q.size) } -func (q *queue) runConsumer(interval int, cQuit chan bool) { - t := time.NewTicker(time.Duration(interval) * time.Second) +func (q *queue) runConsumer(interval time.Duration, cQuit chan bool) { + t := time.NewTicker(interval) for { select { case <-t.C: @@ -331,14 +335,13 @@ log.Fatalf("error getting queue_seconds_since_processed_last derived gauge entry, error %v\n", err) } - cQuit := make(chan bool) + quit := make(chan bool) defer func() { - cQuit <- true - close(cQuit) + close(quit) }() // Run consumer and producer - go q.runConsumer(5, cQuit) + go q.runConsumer(5*time.Second, quit) for { doWork()
diff --git a/examples/derived_gauges/derived_gauge.go b/examples/derived_gauges/derived_gauge.go index ea3b81e..97f8de4 100644 --- a/examples/derived_gauges/derived_gauge.go +++ b/examples/derived_gauges/derived_gauge.go
@@ -11,44 +11,47 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// -// Command stats implements the stats Quick Start example from: -// https://opencensus.io/quickstart/go/metrics/ // START entire + +// This example demonstrates the use of derived gauges. It is a simple interactive program of consumer +// and producer. User can input number of items to produce. Producer produces specified number of +// items. Consumer randomly consumes 1-5 items in each attempt. It then sleeps randomly +// between 1-10 seconds before the next attempt. Two metrics collected to monitor the queue. +// +// Metrics +// +// * queue_size: It is an instantaneous queue size represented using derived gauge int64. +// +// * queue_seconds_since_processed_last: It is the time elaspsed in seconds since the last time +// when the queue was consumed. It is represented using derived gauge float64. package main import ( + "bufio" "fmt" "log" "math/rand" - "sync" - "time" - - "bufio" - "go.opencensus.io/exporter/prometheus" - "go.opencensus.io/metric" - "go.opencensus.io/metric/metricdata" - "go.opencensus.io/metric/metricproducer" "net/http" "os" "strconv" "strings" + "sync" + "time" + + "go.opencensus.io/exporter/prometheus" + "go.opencensus.io/metric" + "go.opencensus.io/metric/metricdata" + "go.opencensus.io/metric/metricproducer" ) -// This example demonstrates the use of derived gauges. It is a simple interactive program of consumer -// and producer. User can input number of items to produce. Producer produces specified number of -// items. Consumer consumes randomly consumes 1-5 items in each attempt. It then sleeps randomly -// between 1-10 seconds before the next attempt. -// -// There are two metrics collected to monitor the queue. -// 1. queue_size: It is an instantaneous queue size represented using derived gauge int64. -// 2. queue_seconds_since_processed_last: It is the time elaspsed in seconds since the last time -// when the queue was consumed. It is represented using derived gauge float64. type queue struct { size int - q []int lastConsumed time.Time - mu sync.Mutex + + mu sync.Mutex + q []int } var q = &queue{} @@ -94,8 +97,8 @@ fmt.Printf("queued %d items, queue size is %d\n", count, q.size) } -func (q *queue) runConsumer(interval int, cQuit chan bool) { - t := time.NewTicker(time.Duration(interval) * time.Second) +func (q *queue) runConsumer(interval time.Duration, cQuit chan bool) { + t := time.NewTicker(interval) for { select { case <-t.C: @@ -220,14 +223,13 @@ } // END entryElapsed - cQuit := make(chan bool) + quit := make(chan bool) defer func() { - cQuit <- true - close(cQuit) + close(quit) }() // Run consumer and producer - go q.runConsumer(5, cQuit) + go q.runConsumer(5*time.Second, quit) for { doWork()
diff --git a/examples/gauges/README.md b/examples/gauges/README.md index 03650f8..5ae3d7f 100644 --- a/examples/gauges/README.md +++ b/examples/gauges/README.md
@@ -117,12 +117,29 @@ [embedmd]:# (gauge.go entire) ```go + +// This example shows how to use gauge metrics. The program records two gauges, one to demonstrate +// a gauge with int64 value and the other to demonstrate a gauge with float64 value. +// +// Metrics +// +// 1. process_heap_alloc (int64): Total bytes used by objects allocated in the heap. +// It includes objects currently used and objects that are freed but not garbage collected. +// +// 2. process_heap_idle_to_alloc_ratio (float64): It is the ratio of Idle bytes to allocated +// bytes in the heap. +// +// It periodically runs a function that retrieves the memory stats and updates the above two +// metrics. These metrics are then exported using prometheus exporter. +// The program lets you choose the amount of memory (in MB) to consume. Choose different values +// and query the metrics to see the change in metrics. package main import ( "bufio" "fmt" "log" + "net/http" "os" "runtime" "strconv" @@ -133,7 +150,6 @@ "go.opencensus.io/metric" "go.opencensus.io/metric/metricdata" "go.opencensus.io/metric/metricproducer" - "net/http" ) var (
diff --git a/examples/gauges/gauge.go b/examples/gauges/gauge.go index 99ae909..287d0b2 100644 --- a/examples/gauges/gauge.go +++ b/examples/gauges/gauge.go
@@ -12,15 +12,30 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Command stats implements the stats Quick Start example from: -// https://opencensus.io/quickstart/go/metrics/ // START entire + +// This example shows how to use gauge metrics. The program records two gauges, one to demonstrate +// a gauge with int64 value and the other to demonstrate a gauge with float64 value. +// +// Metrics +// +// 1. process_heap_alloc (int64): Total bytes used by objects allocated in the heap. +// It includes objects currently used and objects that are freed but not garbage collected. +// +// 2. process_heap_idle_to_alloc_ratio (float64): It is the ratio of Idle bytes to allocated +// bytes in the heap. +// +// It periodically runs a function that retrieves the memory stats and updates the above two +// metrics. These metrics are then exported using prometheus exporter. +// The program lets you choose the amount of memory (in MB) to consume. Choose different values +// and query the metrics to see the change in metrics. package main import ( "bufio" "fmt" "log" + "net/http" "os" "runtime" "strconv" @@ -31,7 +46,6 @@ "go.opencensus.io/metric" "go.opencensus.io/metric/metricdata" "go.opencensus.io/metric/metricproducer" - "net/http" ) var (