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

Commit 3e65bcb

Browse files
committed
update example to use log exporter. (#1128)
1 parent 295a4b8 commit 3e65bcb

File tree

6 files changed

+102
-86
lines changed

6 files changed

+102
-86
lines changed

examples/derived_gauges/README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ There are two metrics collected to monitor the queue.
2727
when the queue was consumed. It is represented using derived gauge float64.
2828
This example shows how to use gauge metrics. The program records two gauges.
2929

30-
These metrics are read when exporter scrapes them. In this example prometheus exporter is used to
31-
scrape the data. Metrics can be viewed at [http://localhost:9090/metrics](http://localhost:9090/metrics) once the program is running.
30+
These metrics are read when exporter scrapes them. In this example log exporter is used to
31+
log the data into a file. Metrics can be viewed at [file:///tmp/metrics.log](file:///tmp/metrics.log)
32+
once the program is running. Alternatively you could do `tail -f /tmp/metrics.log` on Linux/OSx.
3233

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

@@ -159,19 +160,22 @@ import (
159160
"fmt"
160161
"log"
161162
"math/rand"
162-
"net/http"
163163
"os"
164164
"strconv"
165165
"strings"
166166
"sync"
167167
"time"
168168

169-
"go.opencensus.io/exporter/prometheus"
169+
"go.opencensus.io/examples/exporter"
170170
"go.opencensus.io/metric"
171171
"go.opencensus.io/metric/metricdata"
172172
"go.opencensus.io/metric/metricproducer"
173173
)
174174

175+
const (
176+
metricsLogFile = "/tmp/metrics.log"
177+
)
178+
175179
type queue struct {
176180
size int
177181
lastConsumed time.Time
@@ -276,7 +280,8 @@ func doWork() {
276280
fmt.Printf("Program monitors queue using two derived gauge metrics.\n")
277281
fmt.Printf(" 1. queue_size = the instantaneous size of the queue.\n")
278282
fmt.Printf(" 2. queue_seconds_since_processed_last = the number of seconds elapsed since last time the queue was processed.\n")
279-
fmt.Printf("Go to http://localhost:9090/metrics to see the metrics.\n\n\n")
283+
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
284+
metricsLogFile, metricsLogFile)
280285

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

290-
func createAndStartExporter() {
291-
// Create Prometheus metrics exporter to verify derived gauge metrics in this example.
292-
exporter, err := prometheus.NewExporter(prometheus.Options{})
295+
func main() {
296+
// Using logexporter but you can choose any supported exporter.
297+
exporter, err := exporter.NewLogExporter(exporter.Options{
298+
ReportingInterval: time.Duration(10 * time.Second),
299+
MetricsLogFile: metricsLogFile,
300+
})
293301
if err != nil {
294-
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
302+
log.Fatalf("Error creating log exporter: %v", err)
295303
}
296-
http.Handle("/metrics", exporter)
297-
go func() {
298-
log.Fatal(http.ListenAndServe(":9090", nil))
299-
300-
}()
301-
}
302-
303-
func main() {
304-
createAndStartExporter()
304+
exporter.Start()
305+
defer exporter.Stop()
306+
defer exporter.Close()
305307

306308
// Create metric registry and register it with global producer manager.
307309
r := metric.NewRegistry()

examples/derived_gauges/derived_gauge.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ import (
3333
"fmt"
3434
"log"
3535
"math/rand"
36-
"net/http"
3736
"os"
3837
"strconv"
3938
"strings"
4039
"sync"
4140
"time"
4241

43-
"go.opencensus.io/exporter/prometheus"
42+
"go.opencensus.io/examples/exporter"
4443
"go.opencensus.io/metric"
4544
"go.opencensus.io/metric/metricdata"
4645
"go.opencensus.io/metric/metricproducer"
4746
)
4847

48+
const (
49+
metricsLogFile = "/tmp/metrics.log"
50+
)
51+
4952
type queue struct {
5053
size int
5154
lastConsumed time.Time
@@ -154,7 +157,8 @@ func doWork() {
154157
fmt.Printf("Program monitors queue using two derived gauge metrics.\n")
155158
fmt.Printf(" 1. queue_size = the instantaneous size of the queue.\n")
156159
fmt.Printf(" 2. queue_seconds_since_processed_last = the number of seconds elapsed since last time the queue was processed.\n")
157-
fmt.Printf("Go to http://localhost:9090/metrics to see the metrics.\n\n\n")
160+
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
161+
metricsLogFile, metricsLogFile)
158162

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

168-
func createAndStartExporter() {
169-
// Create Prometheus metrics exporter to verify derived gauge metrics in this example.
170-
exporter, err := prometheus.NewExporter(prometheus.Options{})
172+
func main() {
173+
// Using logexporter but you can choose any supported exporter.
174+
exporter, err := exporter.NewLogExporter(exporter.Options{
175+
ReportingInterval: time.Duration(10 * time.Second),
176+
MetricsLogFile: metricsLogFile,
177+
})
171178
if err != nil {
172-
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
179+
log.Fatalf("Error creating log exporter: %v", err)
173180
}
174-
http.Handle("/metrics", exporter)
175-
go func() {
176-
log.Fatal(http.ListenAndServe(":9090", nil))
177-
178-
}()
179-
}
180-
181-
func main() {
182-
createAndStartExporter()
181+
exporter.Start()
182+
defer exporter.Stop()
183+
defer exporter.Close()
183184

184185
// Create metric registry and register it with global producer manager.
185186
// START reg

examples/gauges/README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ This example shows how to use gauge metrics. The program records two gauges.
2121
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.
2222
1. **process_heap_idle_to_alloc_ratio (float64)**: It is the ratio of Idle bytes to allocated bytes in the heap.
2323

24-
It periodically runs a function that retrieves the memory stats and updates the above two metrics. These metrics are then exported using prometheus exporter.
25-
Metrics can be viewed at [http://localhost:9090/metrcs](http://localhost:9090/metrcs) once the program is running.
24+
It periodically runs a function that retrieves the memory stats and updates the above two metrics.
25+
These metrics are then exported using log exporter. Metrics can be viewed at
26+
[file:///tmp/metrics.log](file:///tmp/metrics.log)
27+
once the program is running. Alternatively you could do `tail -f /tmp/metrics.log` on Linux/OSx.
28+
2629
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.
2730

2831
## Run the example
@@ -130,7 +133,7 @@ Use `Set` or `Add` function to update the value of gauge entries. You can call t
130133
// bytes in the heap.
131134
//
132135
// It periodically runs a function that retrieves the memory stats and updates the above two
133-
// metrics. These metrics are then exported using prometheus exporter.
136+
// metrics. These metrics are then exported using log exporter.
134137
// The program lets you choose the amount of memory (in MB) to consume. Choose different values
135138
// and query the metrics to see the change in metrics.
136139
package main
@@ -139,19 +142,22 @@ import (
139142
"bufio"
140143
"fmt"
141144
"log"
142-
"net/http"
143145
"os"
144146
"runtime"
145147
"strconv"
146148
"strings"
147149
"time"
148150

149-
"go.opencensus.io/exporter/prometheus"
151+
"go.opencensus.io/examples/exporter"
150152
"go.opencensus.io/metric"
151153
"go.opencensus.io/metric/metricdata"
152154
"go.opencensus.io/metric/metricproducer"
153155
)
154156

157+
const (
158+
metricsLogFile = "/tmp/metrics.log"
159+
)
160+
155161
var (
156162
mem = &runtime.MemStats{}
157163
)
@@ -232,7 +238,8 @@ func work() {
232238
fmt.Printf("Program periodically records following gauge metrics.\n")
233239
fmt.Printf(" 1. process_heap_alloc = the heap allocation (used + freed but not garbage collected)\n")
234240
fmt.Printf(" 2. process_idle_to_alloc_ratio = heap idle (unused) /allocation ratio\n")
235-
fmt.Printf("\nGo to http://localhost:9090/metrics to see the metrics.\n\n\n")
241+
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
242+
metricsLogFile, metricsLogFile)
236243
fmt.Printf("Enter memory you would like to allocate in MB to change the value of above metrics.\n")
237244

238245
// Do some work and record gauge metrics.
@@ -243,21 +250,18 @@ func work() {
243250
}
244251
}
245252

246-
func createAndStartExporter() {
247-
// Create Prometheus metrics exporter to verify gauge metrics in this example.
248-
exporter, err := prometheus.NewExporter(prometheus.Options{})
253+
func main() {
254+
// Using log exporter to export metrics but you can choose any supported exporter.
255+
exporter, err := exporter.NewLogExporter(exporter.Options{
256+
ReportingInterval: time.Duration(10 * time.Second),
257+
MetricsLogFile: metricsLogFile,
258+
})
249259
if err != nil {
250-
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
260+
log.Fatalf("Error creating log exporter: %v", err)
251261
}
252-
http.Handle("/metrics", exporter)
253-
go func() {
254-
log.Fatal(http.ListenAndServe(":9090", nil))
255-
256-
}()
257-
}
258-
259-
func main() {
260-
createAndStartExporter()
262+
exporter.Start()
263+
defer exporter.Stop()
264+
defer exporter.Close()
261265

262266
// Create metric registry and register it with global producer manager.
263267
r := metric.NewRegistry()

examples/gauges/gauge.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// bytes in the heap.
2727
//
2828
// It periodically runs a function that retrieves the memory stats and updates the above two
29-
// metrics. These metrics are then exported using prometheus exporter.
29+
// metrics. These metrics are then exported using log exporter.
3030
// The program lets you choose the amount of memory (in MB) to consume. Choose different values
3131
// and query the metrics to see the change in metrics.
3232
package main
@@ -35,19 +35,22 @@ import (
3535
"bufio"
3636
"fmt"
3737
"log"
38-
"net/http"
3938
"os"
4039
"runtime"
4140
"strconv"
4241
"strings"
4342
"time"
4443

45-
"go.opencensus.io/exporter/prometheus"
44+
"go.opencensus.io/examples/exporter"
4645
"go.opencensus.io/metric"
4746
"go.opencensus.io/metric/metricdata"
4847
"go.opencensus.io/metric/metricproducer"
4948
)
5049

50+
const (
51+
metricsLogFile = "/tmp/metrics.log"
52+
)
53+
5154
var (
5255
mem = &runtime.MemStats{}
5356
)
@@ -130,7 +133,8 @@ func work() {
130133
fmt.Printf("Program periodically records following gauge metrics.\n")
131134
fmt.Printf(" 1. process_heap_alloc = the heap allocation (used + freed but not garbage collected)\n")
132135
fmt.Printf(" 2. process_idle_to_alloc_ratio = heap idle (unused) /allocation ratio\n")
133-
fmt.Printf("\nGo to http://localhost:9090/metrics to see the metrics.\n\n\n")
136+
fmt.Printf("\nGo to file://%s to see the metrics. OR do `tail -f %s` in another terminal\n\n\n",
137+
metricsLogFile, metricsLogFile)
134138
fmt.Printf("Enter memory you would like to allocate in MB to change the value of above metrics.\n")
135139

136140
// Do some work and record gauge metrics.
@@ -141,21 +145,18 @@ func work() {
141145
}
142146
}
143147

144-
func createAndStartExporter() {
145-
// Create Prometheus metrics exporter to verify gauge metrics in this example.
146-
exporter, err := prometheus.NewExporter(prometheus.Options{})
148+
func main() {
149+
// Using log exporter to export metrics but you can choose any supported exporter.
150+
exporter, err := exporter.NewLogExporter(exporter.Options{
151+
ReportingInterval: time.Duration(10 * time.Second),
152+
MetricsLogFile: metricsLogFile,
153+
})
147154
if err != nil {
148-
log.Fatalf("Failed to create the prometheus metrics exporter: %v", err)
155+
log.Fatalf("Error creating log exporter: %v", err)
149156
}
150-
http.Handle("/metrics", exporter)
151-
go func() {
152-
log.Fatal(http.ListenAndServe(":9090", nil))
153-
154-
}()
155-
}
156-
157-
func main() {
158-
createAndStartExporter()
157+
exporter.Start()
158+
defer exporter.Stop()
159+
defer exporter.Close()
159160

160161
// Create metric registry and register it with global producer manager.
161162
// START reg

examples/helloworld/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ func main() {
4444

4545
// Register an exporter to be able to retrieve
4646
// the data from the subscribed views.
47-
e := &exporter.PrintExporter{}
48-
view.RegisterExporter(e)
49-
trace.RegisterExporter(e)
47+
e, err := exporter.NewLogExporter(exporter.Options{ReportingInterval: time.Duration(time.Second)})
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
e.Start()
52+
defer e.Stop()
53+
defer e.Close()
54+
55+
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
5056

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

8186
// process processes the video and instruments the processing

examples/quickstart/stats.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ import (
2828

2929
"net/http"
3030

31-
"go.opencensus.io/exporter/prometheus"
31+
"go.opencensus.io/examples/exporter"
3232
"go.opencensus.io/stats"
3333
"go.opencensus.io/stats/view"
3434
"go.opencensus.io/tag"
3535
"go.opencensus.io/zpages"
3636
)
3737

38+
const (
39+
metricsLogFile = "/tmp/metrics.log"
40+
)
41+
3842
// Measures for the stats quickstart.
3943
var (
4044
// The latency in milliseconds
@@ -94,24 +98,23 @@ func main() {
9498
zpages.Handle(nil, "/debug")
9599
go http.ListenAndServe("localhost:8080", nil)
96100

97-
// Create that Stackdriver stats exporter
98-
exporter, err := prometheus.NewExporter(prometheus.Options{})
101+
// Using log exporter here to export metrics but you can choose any supported exporter.
102+
exporter, err := exporter.NewLogExporter(exporter.Options{
103+
ReportingInterval: time.Duration(10 * time.Second),
104+
MetricsLogFile: metricsLogFile,
105+
})
99106
if err != nil {
100-
log.Fatalf("Failed to create the Stackdriver stats exporter: %v", err)
107+
log.Fatalf("Error creating log exporter: %v", err)
101108
}
102-
http.Handle("/metrics", exporter)
103-
104-
// Register the stats exporter
105-
view.RegisterExporter(exporter)
109+
exporter.Start()
110+
defer exporter.Stop()
111+
defer exporter.Close()
106112

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

112-
// But also we can change the metrics reporting period to 2 seconds
113-
//view.SetReportingPeriod(2 * time.Second)
114-
115118
// In a REPL:
116119
// 1. Read input
117120
// 2. process input

0 commit comments

Comments
 (0)