...

Source file src/go.opencensus.io/examples/exporter/exporter.go

Documentation: go.opencensus.io/examples/exporter

     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  
    15  package exporter // import "go.opencensus.io/examples/exporter"
    16  
    17  import (
    18  	"encoding/hex"
    19  	"fmt"
    20  	"regexp"
    21  	"time"
    22  
    23  	"go.opencensus.io/stats/view"
    24  	"go.opencensus.io/trace"
    25  )
    26  
    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  
    33  // PrintExporter is a stats and trace exporter that logs
    34  // 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.
    40  type PrintExporter struct{}
    41  
    42  // ExportView logs the view data.
    43  func (e *PrintExporter) ExportView(vd *view.Data) {
    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  	}
    63  }
    64  
    65  // ExportSpan logs the trace span.
    66  func (e *PrintExporter) ExportSpan(vd *trace.SpanData) {
    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  	}
   105  }
   106  

View as plain text