Skip to content

Commit

Permalink
Fix comment received for census-instrumentation#1110 after it was mer…
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia committed Apr 25, 2019
1 parent df16657 commit fd315ad
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 50 deletions.
47 changes: 25 additions & 22 deletions examples/derived_gauges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,40 +140,44 @@ func (q *queue) Elapsed() float64 {

[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"
"net/http"
"os"
"strconv"
"strings"
"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"
)

// 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{}
Expand Down Expand Up @@ -219,8 +223,8 @@ func (q *queue) produce(count int) {
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:
Expand Down Expand Up @@ -331,14 +335,13 @@ func main() {
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()
Expand Down
50 changes: 26 additions & 24 deletions examples/derived_gauges/derived_gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"net/http"
"os"
"strconv"
"strings"
"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"
)

// 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{}
Expand Down Expand Up @@ -94,8 +97,8 @@ func (q *queue) produce(count int) {
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:
Expand Down Expand Up @@ -220,14 +223,13 @@ func main() {
}
// 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()
Expand Down
18 changes: 17 additions & 1 deletion examples/gauges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,29 @@ Use `Set` or `Add` function to update the value of gauge entries. You can call t

[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"
Expand All @@ -133,7 +150,6 @@ import (
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
"net/http"
)

var (
Expand Down
20 changes: 17 additions & 3 deletions examples/gauges/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,7 +46,6 @@ import (
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
"net/http"
)

var (
Expand Down

0 comments on commit fd315ad

Please sign in to comment.