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

Commit 209434a

Browse files
savakisongy23
authored andcommitted
#762 enhanced output of example PrintExporter with a focus on clarity (#911)
1 parent 53d4387 commit 209434a

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,4 @@ release in which the functionality was marked *Deprecated*.
259259
[exporter-xray]: https://github.com/census-ecosystem/opencensus-go-exporter-aws
260260
[exporter-datadog]: https://github.com/DataDog/opencensus-go-exporter-datadog
261261
[exporter-graphite]: https://github.com/census-ecosystem/opencensus-go-exporter-graphite
262+

examples/exporter/exporter.go

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,91 @@
1515
package exporter // import "go.opencensus.io/examples/exporter"
1616

1717
import (
18-
"log"
18+
"encoding/hex"
19+
"fmt"
20+
"regexp"
21+
"time"
1922

2023
"go.opencensus.io/stats/view"
2124
"go.opencensus.io/trace"
2225
)
2326

27+
// indent these many spaces
28+
const indent = " "
29+
30+
// reZero provides a simple way to detect an empty ID
31+
var reZero = regexp.MustCompile(`^0+$`)
32+
2433
// PrintExporter is a stats and trace exporter that logs
2534
// the exported data to the console.
35+
//
36+
// The intent is help new users familiarize themselves with the
37+
// capabilities of opencensus.
38+
//
39+
// This should NOT be used for production workloads.
2640
type PrintExporter struct{}
2741

2842
// ExportView logs the view data.
2943
func (e *PrintExporter) ExportView(vd *view.Data) {
30-
log.Println(vd)
44+
for _, row := range vd.Rows {
45+
fmt.Printf("%v %-45s", vd.End.Format("15:04:05"), vd.View.Name)
46+
47+
switch v := row.Data.(type) {
48+
case *view.DistributionData:
49+
fmt.Printf("distribution: min=%.1f max=%.1f mean=%.1f", v.Min, v.Max, v.Mean)
50+
case *view.CountData:
51+
fmt.Printf("count: value=%v", v.Value)
52+
case *view.SumData:
53+
fmt.Printf("sum: value=%v", v.Value)
54+
case *view.LastValueData:
55+
fmt.Printf("last: value=%v", v.Value)
56+
}
57+
fmt.Println()
58+
59+
for _, tag := range row.Tags {
60+
fmt.Printf("%v- %v=%v\n", indent, tag.Key.Name(), tag.Value)
61+
}
62+
}
3163
}
3264

3365
// ExportSpan logs the trace span.
3466
func (e *PrintExporter) ExportSpan(vd *trace.SpanData) {
35-
log.Println(vd)
67+
var (
68+
traceID = hex.EncodeToString(vd.SpanContext.TraceID[:])
69+
spanID = hex.EncodeToString(vd.SpanContext.SpanID[:])
70+
parentSpanID = hex.EncodeToString(vd.ParentSpanID[:])
71+
)
72+
fmt.Println()
73+
fmt.Println("#----------------------------------------------")
74+
fmt.Println()
75+
fmt.Println("TraceID: ", traceID)
76+
fmt.Println("SpanID: ", spanID)
77+
if !reZero.MatchString(parentSpanID) {
78+
fmt.Println("ParentSpanID:", parentSpanID)
79+
}
80+
81+
fmt.Println()
82+
fmt.Printf("Span: %v\n", vd.Name)
83+
fmt.Printf("Status: %v [%v]\n", vd.Status.Message, vd.Status.Code)
84+
fmt.Printf("Elapsed: %v\n", vd.EndTime.Sub(vd.StartTime).Round(time.Millisecond))
85+
86+
if len(vd.Annotations) > 0 {
87+
fmt.Println()
88+
fmt.Println("Annotations:")
89+
for _, item := range vd.Annotations {
90+
fmt.Print(indent, item.Message)
91+
for k, v := range item.Attributes {
92+
fmt.Printf(" %v=%v", k, v)
93+
}
94+
fmt.Println()
95+
}
96+
}
97+
98+
if len(vd.Attributes) > 0 {
99+
fmt.Println()
100+
fmt.Println("Attributes:")
101+
for k, v := range vd.Attributes {
102+
fmt.Printf("%v- %v=%v\n", indent, k, v)
103+
}
104+
}
36105
}

examples/http/helloworld_server/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ func main() {
5454
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
5555
fmt.Fprintf(w, "hello world")
5656

57+
// Provide an example of how spans can be annotated with metadata
58+
_, span := trace.StartSpan(req.Context(), "child")
59+
defer span.End()
60+
span.Annotate([]trace.Attribute{trace.StringAttribute("key", "value")}, "something happened")
61+
span.AddAttributes(trace.StringAttribute("hello", "world"))
62+
time.Sleep(time.Millisecond * 125)
63+
5764
r, _ := http.NewRequest("GET", "https://example.com", nil)
5865

5966
// Propagate the trace header info in the outgoing requests.

0 commit comments

Comments
 (0)