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

update example to use log exporter. #1128

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions examples/derived_gauges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ There are two metrics collected to monitor the queue.
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 this example prometheus exporter is used to
scrape the data. Metrics can be viewed at [http://localhost:9090/metrics](http://localhost:9090/metrics) once the program is running.
These metrics are read when exporter scrapes them. In this example log exporter is used to
log the data into a file. Metrics can be viewed at [file:///tmp/metrics.log](file:///tmp/metrics.log)
once the program is running. Alternatively you could do `tail -f /tmp/metrics.log` on Linux/OSx.

Enter different value for number of items to queue and fetch the metrics using above url to see the variation in the metrics.

Expand Down Expand Up @@ -159,19 +160,22 @@ import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"

"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/examples/exporter"
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
)

const (
metricsLogFile = "/tmp/metrics.log"
)

type queue struct {
size int
lastConsumed time.Time
Expand Down Expand Up @@ -276,7 +280,8 @@ func doWork() {
fmt.Printf("Program monitors queue using two derived gauge metrics.\n")
fmt.Printf(" 1. queue_size = the instantaneous size of the queue.\n")
fmt.Printf(" 2. queue_seconds_since_processed_last = the number of seconds elapsed since last time the queue was processed.\n")
fmt.Printf("Go to http://localhost:9090/metrics to see the metrics.\n\n\n")
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
metricsLogFile, metricsLogFile)

// Take a number of items to queue as an input from the user
// and enqueue the same number of items on to the consumer queue.
Expand All @@ -287,21 +292,18 @@ func doWork() {
}
}

func createAndStartExporter() {
// Create Prometheus metrics exporter to verify derived gauge metrics in this example.
exporter, err := prometheus.NewExporter(prometheus.Options{})
func main() {
// Using logexporter but you can choose any supported exporter.
exporter, err := exporter.NewLogExporter(exporter.Options{
ReportingInterval: time.Duration(10 * time.Second),
MetricsLogFile: metricsLogFile,
})
if err != nil {
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
log.Fatalf("Error creating log exporter: %v", err)
}
http.Handle("/metrics", exporter)
go func() {
log.Fatal(http.ListenAndServe(":9090", nil))

}()
}

func main() {
createAndStartExporter()
exporter.Start()
defer exporter.Stop()
defer exporter.Close()

// Create metric registry and register it with global producer manager.
r := metric.NewRegistry()
Expand Down
33 changes: 17 additions & 16 deletions examples/derived_gauges/derived_gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@ import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"

"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/examples/exporter"
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
)

const (
metricsLogFile = "/tmp/metrics.log"
)

type queue struct {
size int
lastConsumed time.Time
Expand Down Expand Up @@ -154,7 +157,8 @@ func doWork() {
fmt.Printf("Program monitors queue using two derived gauge metrics.\n")
fmt.Printf(" 1. queue_size = the instantaneous size of the queue.\n")
fmt.Printf(" 2. queue_seconds_since_processed_last = the number of seconds elapsed since last time the queue was processed.\n")
fmt.Printf("Go to http://localhost:9090/metrics to see the metrics.\n\n\n")
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
metricsLogFile, metricsLogFile)

// Take a number of items to queue as an input from the user
// and enqueue the same number of items on to the consumer queue.
Expand All @@ -165,21 +169,18 @@ func doWork() {
}
}

func createAndStartExporter() {
// Create Prometheus metrics exporter to verify derived gauge metrics in this example.
exporter, err := prometheus.NewExporter(prometheus.Options{})
func main() {
// Using logexporter but you can choose any supported exporter.
exporter, err := exporter.NewLogExporter(exporter.Options{
ReportingInterval: time.Duration(10 * time.Second),
MetricsLogFile: metricsLogFile,
})
if err != nil {
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
log.Fatalf("Error creating log exporter: %v", err)
}
http.Handle("/metrics", exporter)
go func() {
log.Fatal(http.ListenAndServe(":9090", nil))

}()
}

func main() {
createAndStartExporter()
exporter.Start()
defer exporter.Stop()
defer exporter.Close()

// Create metric registry and register it with global producer manager.
// START reg
Expand Down
42 changes: 23 additions & 19 deletions examples/gauges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ This example shows how to use gauge metrics. The program records two gauges.
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.
1. **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.
Metrics can be viewed at [http://localhost:9090/metrcs](http://localhost:9090/metrcs) once the program is running.
It periodically runs a function that retrieves the memory stats and updates the above two metrics.
These metrics are then exported using log exporter. Metrics can be viewed at
[file:///tmp/metrics.log](file:///tmp/metrics.log)
once the program is running. Alternatively you could do `tail -f /tmp/metrics.log` on Linux/OSx.

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.

## Run the example
Expand Down Expand Up @@ -130,7 +133,7 @@ Use `Set` or `Add` function to update the value of gauge entries. You can call t
// 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.
// metrics. These metrics are then exported using log 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
Expand All @@ -139,19 +142,22 @@ import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"

"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/examples/exporter"
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
)

const (
metricsLogFile = "/tmp/metrics.log"
)

var (
mem = &runtime.MemStats{}
)
Expand Down Expand Up @@ -232,7 +238,8 @@ func work() {
fmt.Printf("Program periodically records following gauge metrics.\n")
fmt.Printf(" 1. process_heap_alloc = the heap allocation (used + freed but not garbage collected)\n")
fmt.Printf(" 2. process_idle_to_alloc_ratio = heap idle (unused) /allocation ratio\n")
fmt.Printf("\nGo to http://localhost:9090/metrics to see the metrics.\n\n\n")
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
metricsLogFile, metricsLogFile)
fmt.Printf("Enter memory you would like to allocate in MB to change the value of above metrics.\n")

// Do some work and record gauge metrics.
Expand All @@ -243,21 +250,18 @@ func work() {
}
}

func createAndStartExporter() {
// Create Prometheus metrics exporter to verify gauge metrics in this example.
exporter, err := prometheus.NewExporter(prometheus.Options{})
func main() {
// Using log exporter to export metrics but you can choose any supported exporter.
exporter, err := exporter.NewLogExporter(exporter.Options{
ReportingInterval: time.Duration(10 * time.Second),
MetricsLogFile: metricsLogFile,
})
if err != nil {
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
log.Fatalf("Error creating log exporter: %v", err)
}
http.Handle("/metrics", exporter)
go func() {
log.Fatal(http.ListenAndServe(":9090", nil))

}()
}

func main() {
createAndStartExporter()
exporter.Start()
defer exporter.Stop()
defer exporter.Close()

// Create metric registry and register it with global producer manager.
r := metric.NewRegistry()
Expand Down
35 changes: 18 additions & 17 deletions examples/gauges/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// 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.
// metrics. These metrics are then exported using log 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
Expand All @@ -35,19 +35,22 @@ import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"

"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/examples/exporter"
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricproducer"
)

const (
metricsLogFile = "/tmp/metrics.log"
)

var (
mem = &runtime.MemStats{}
)
Expand Down Expand Up @@ -130,7 +133,8 @@ func work() {
fmt.Printf("Program periodically records following gauge metrics.\n")
fmt.Printf(" 1. process_heap_alloc = the heap allocation (used + freed but not garbage collected)\n")
fmt.Printf(" 2. process_idle_to_alloc_ratio = heap idle (unused) /allocation ratio\n")
fmt.Printf("\nGo to http://localhost:9090/metrics to see the metrics.\n\n\n")
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
metricsLogFile, metricsLogFile)
fmt.Printf("Enter memory you would like to allocate in MB to change the value of above metrics.\n")

// Do some work and record gauge metrics.
Expand All @@ -141,21 +145,18 @@ func work() {
}
}

func createAndStartExporter() {
// Create Prometheus metrics exporter to verify gauge metrics in this example.
exporter, err := prometheus.NewExporter(prometheus.Options{})
func main() {
// Using log exporter to export metrics but you can choose any supported exporter.
exporter, err := exporter.NewLogExporter(exporter.Options{
ReportingInterval: time.Duration(10 * time.Second),
MetricsLogFile: metricsLogFile,
})
if err != nil {
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
log.Fatalf("Error creating log exporter: %v", err)
}
http.Handle("/metrics", exporter)
go func() {
log.Fatal(http.ListenAndServe(":9090", nil))

}()
}

func main() {
createAndStartExporter()
exporter.Start()
defer exporter.Stop()
defer exporter.Close()

// Create metric registry and register it with global producer manager.
// START reg
Expand Down
15 changes: 10 additions & 5 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ func main() {

// Register an exporter to be able to retrieve
// the data from the subscribed views.
e := &exporter.PrintExporter{}
view.RegisterExporter(e)
trace.RegisterExporter(e)
e, err := exporter.NewLogExporter(exporter.Options{ReportingInterval: time.Duration(time.Second)})
if err != nil {
log.Fatal(err)
}
e.Start()
defer e.Stop()
defer e.Close()

trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})

var err error
frontendKey, err = tag.NewKey("example.com/keys/frontend")
if err != nil {
log.Fatal(err)
Expand All @@ -75,7 +80,7 @@ func main() {
// Wait for a duration longer than reporting duration to ensure the stats
// library reports the collected data.
fmt.Println("Wait longer than the reporting duration...")
time.Sleep(2 * time.Second)
time.Sleep(4 * time.Second)
}

// process processes the video and instruments the processing
Expand Down
25 changes: 14 additions & 11 deletions examples/quickstart/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ import (

"net/http"

"go.opencensus.io/exporter/prometheus"
"go.opencensus.io/examples/exporter"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.opencensus.io/zpages"
)

const (
metricsLogFile = "/tmp/metrics.log"
)

// Measures for the stats quickstart.
var (
// The latency in milliseconds
Expand Down Expand Up @@ -94,24 +98,23 @@ func main() {
zpages.Handle(nil, "/debug")
go http.ListenAndServe("localhost:8080", nil)

// Create that Stackdriver stats exporter
exporter, err := prometheus.NewExporter(prometheus.Options{})
// Using log exporter here to export metrics but you can choose any supported exporter.
exporter, err := exporter.NewLogExporter(exporter.Options{
ReportingInterval: time.Duration(10 * time.Second),
MetricsLogFile: metricsLogFile,
})
if err != nil {
log.Fatalf("Failed to create the Stackdriver stats exporter: %v", err)
log.Fatalf("Error creating log exporter: %v", err)
}
http.Handle("/metrics", exporter)

// Register the stats exporter
view.RegisterExporter(exporter)
exporter.Start()
defer exporter.Stop()
defer exporter.Close()

// Register the views
if err := view.Register(latencyView, lineCountView, errorCountView, lineLengthView); err != nil {
log.Fatalf("Failed to register views: %v", err)
}

// But also we can change the metrics reporting period to 2 seconds
//view.SetReportingPeriod(2 * time.Second)

// In a REPL:
// 1. Read input
// 2. process input
Expand Down