You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
3.0 KiB

  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package exporter // import "go.opencensus.io/examples/exporter"
  15. import (
  16. "encoding/hex"
  17. "fmt"
  18. "regexp"
  19. "time"
  20. "go.opencensus.io/stats/view"
  21. "go.opencensus.io/trace"
  22. )
  23. // indent these many spaces
  24. const indent = " "
  25. // reZero provides a simple way to detect an empty ID
  26. var reZero = regexp.MustCompile(`^0+$`)
  27. // PrintExporter is a stats and trace exporter that logs
  28. // the exported data to the console.
  29. //
  30. // The intent is help new users familiarize themselves with the
  31. // capabilities of opencensus.
  32. //
  33. // This should NOT be used for production workloads.
  34. type PrintExporter struct{}
  35. // ExportView logs the view data.
  36. func (e *PrintExporter) ExportView(vd *view.Data) {
  37. for _, row := range vd.Rows {
  38. fmt.Printf("%v %-45s", vd.End.Format("15:04:05"), vd.View.Name)
  39. switch v := row.Data.(type) {
  40. case *view.DistributionData:
  41. fmt.Printf("distribution: min=%.1f max=%.1f mean=%.1f", v.Min, v.Max, v.Mean)
  42. case *view.CountData:
  43. fmt.Printf("count: value=%v", v.Value)
  44. case *view.SumData:
  45. fmt.Printf("sum: value=%v", v.Value)
  46. case *view.LastValueData:
  47. fmt.Printf("last: value=%v", v.Value)
  48. }
  49. fmt.Println()
  50. for _, tag := range row.Tags {
  51. fmt.Printf("%v- %v=%v\n", indent, tag.Key.Name(), tag.Value)
  52. }
  53. }
  54. }
  55. // ExportSpan logs the trace span.
  56. func (e *PrintExporter) ExportSpan(vd *trace.SpanData) {
  57. var (
  58. traceID = hex.EncodeToString(vd.SpanContext.TraceID[:])
  59. spanID = hex.EncodeToString(vd.SpanContext.SpanID[:])
  60. parentSpanID = hex.EncodeToString(vd.ParentSpanID[:])
  61. )
  62. fmt.Println()
  63. fmt.Println("#----------------------------------------------")
  64. fmt.Println()
  65. fmt.Println("TraceID: ", traceID)
  66. fmt.Println("SpanID: ", spanID)
  67. if !reZero.MatchString(parentSpanID) {
  68. fmt.Println("ParentSpanID:", parentSpanID)
  69. }
  70. fmt.Println()
  71. fmt.Printf("Span: %v\n", vd.Name)
  72. fmt.Printf("Status: %v [%v]\n", vd.Status.Message, vd.Status.Code)
  73. fmt.Printf("Elapsed: %v\n", vd.EndTime.Sub(vd.StartTime).Round(time.Millisecond))
  74. if len(vd.Annotations) > 0 {
  75. fmt.Println()
  76. fmt.Println("Annotations:")
  77. for _, item := range vd.Annotations {
  78. fmt.Print(indent, item.Message)
  79. for k, v := range item.Attributes {
  80. fmt.Printf(" %v=%v", k, v)
  81. }
  82. fmt.Println()
  83. }
  84. }
  85. if len(vd.Attributes) > 0 {
  86. fmt.Println()
  87. fmt.Println("Attributes:")
  88. for k, v := range vd.Attributes {
  89. fmt.Printf("%v- %v=%v\n", indent, k, v)
  90. }
  91. }
  92. }