github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/internal/oc/trace.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit 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  //     https://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 oc
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"reflect"
    21  	"time"
    22  
    23  	"go.opencensus.io/stats"
    24  	"go.opencensus.io/tag"
    25  	"go.opencensus.io/trace"
    26  	"gocloud.dev/gcerrors"
    27  )
    28  
    29  // A Tracer supports OpenCensus tracing and latency metrics.
    30  type Tracer struct {
    31  	Package        string
    32  	Provider       string
    33  	LatencyMeasure *stats.Float64Measure
    34  }
    35  
    36  // ProviderName returns the name of the provider associated with the driver value.
    37  // It is intended to be used to set Tracer.Provider.
    38  // It actually returns the package path of the driver's type.
    39  func ProviderName(driver interface{}) string {
    40  	// Return the last component of the package path.
    41  	if driver == nil {
    42  		return ""
    43  	}
    44  	t := reflect.TypeOf(driver)
    45  	if t.Kind() == reflect.Ptr {
    46  		t = t.Elem()
    47  	}
    48  	return t.PkgPath()
    49  }
    50  
    51  // Context key for starting time of a method call.
    52  type startTimeKey struct{}
    53  
    54  // Start adds a span to the trace, and prepares for recording a latency measurement.
    55  func (t *Tracer) Start(ctx context.Context, methodName string) context.Context {
    56  	fullName := t.Package + "." + methodName
    57  	ctx, _ = trace.StartSpan(ctx, fullName)
    58  	ctx, err := tag.New(ctx,
    59  		tag.Upsert(MethodKey, fullName),
    60  		tag.Upsert(ProviderKey, t.Provider))
    61  	if err != nil {
    62  		// The only possible errors are from invalid key or value names, and those are programming
    63  		// errors that will be found during testing.
    64  		panic(fmt.Sprintf("fullName=%q, provider=%q: %v", fullName, t.Provider, err))
    65  	}
    66  	return context.WithValue(ctx, startTimeKey{}, time.Now())
    67  }
    68  
    69  // End ends a span with the given error, and records a latency measurement.
    70  func (t *Tracer) End(ctx context.Context, err error) {
    71  	startTime := ctx.Value(startTimeKey{}).(time.Time)
    72  	elapsed := time.Since(startTime)
    73  	code := gcerrors.Code(err)
    74  	span := trace.FromContext(ctx)
    75  	if err != nil {
    76  		span.SetStatus(trace.Status{Code: int32(code), Message: err.Error()})
    77  	}
    78  	span.End()
    79  	stats.RecordWithTags(ctx, []tag.Mutator{tag.Upsert(StatusKey, fmt.Sprint(code))},
    80  		t.LatencyMeasure.M(float64(elapsed.Nanoseconds())/1e6)) // milliseconds
    81  }