go.dedis.ch/onet/v3@v3.2.11-0.20210930124529-e36530bca7ef/tracing/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"go.dedis.ch/onet/v3"
     8  	"go.dedis.ch/onet/v3/log"
     9  	"go.dedis.ch/onet/v3/tracing"
    10  )
    11  
    12  // Import this service for most automatic use of the tracing system.
    13  // All you need to do is to set the following environment variable:
    14  //   HONEYCOMB_API_KEY="hex_key:dataset"
    15  // And then this service will send all traces to your honeycomb account.
    16  // It will track automatically what is happening in your code and send it to
    17  //your honeycomb account.
    18  // There are additional environmental variables described in
    19  //   onet/tracing/logger.go
    20  
    21  // Name of the service.
    22  var Name = "TracingService"
    23  var loggerCounter = 0
    24  
    25  func init() {
    26  	_, err := onet.RegisterNewService(Name, newTracer)
    27  	log.ErrFatal(err)
    28  }
    29  
    30  type tracer struct {
    31  	*onet.ServiceProcessor
    32  	tl *tracing.TraceLogger
    33  }
    34  
    35  func newTracer(c *onet.Context) (onet.Service, error) {
    36  	if loggerCounter > 0 {
    37  		log.Warn("can only start one service for tracing")
    38  		return nil, nil
    39  	}
    40  	loggerCounter++
    41  	tl, err := tracing.NewHoneycombLoggerFromEnv()
    42  	if err != nil {
    43  		return nil, fmt.Errorf("couldn't init honeycomb: %v", err)
    44  	}
    45  	if tl == nil {
    46  		log.Info("Not starting Honeycomb tracing as HONEYCOMB_API_KEY not" +
    47  			" present")
    48  		return nil, nil
    49  	}
    50  	err = tl.AddEnvironment()
    51  	if err != nil {
    52  		return nil, fmt.Errorf("couldn't interpret environment variables: %v",
    53  			err)
    54  	}
    55  	tl.AddOnetDefaults(c.ServerIdentity())
    56  	tl.AddStats(c, time.Minute)
    57  	log.Info("Tracing with HoneyComb successfully set up")
    58  	return &tracer{
    59  		ServiceProcessor: onet.NewServiceProcessor(c),
    60  		tl:               tl,
    61  	}, nil
    62  }