diff --git a/examples/derived_gauges/README.md b/examples/derived_gauges/README.md index 9dda6809c..8d6f0db1a 100644 --- a/examples/derived_gauges/README.md +++ b/examples/derived_gauges/README.md @@ -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. @@ -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 @@ -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. @@ -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() diff --git a/examples/derived_gauges/derived_gauge.go b/examples/derived_gauges/derived_gauge.go index 97f8de474..721742de5 100644 --- a/examples/derived_gauges/derived_gauge.go +++ b/examples/derived_gauges/derived_gauge.go @@ -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 @@ -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. @@ -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 diff --git a/examples/gauges/README.md b/examples/gauges/README.md index 80ed0396b..c1853b173 100644 --- a/examples/gauges/README.md +++ b/examples/gauges/README.md @@ -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 @@ -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 @@ -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{} ) @@ -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. @@ -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() diff --git a/examples/gauges/gauge.go b/examples/gauges/gauge.go index 2b744e79d..896effe95 100644 --- a/examples/gauges/gauge.go +++ b/examples/gauges/gauge.go @@ -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 @@ -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{} ) @@ -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. @@ -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 diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 0f26636dc..5428d6e2a 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -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) @@ -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 diff --git a/examples/quickstart/stats.go b/examples/quickstart/stats.go index 8811ba17d..35208774d 100644 --- a/examples/quickstart/stats.go +++ b/examples/quickstart/stats.go @@ -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 @@ -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