github.com/vseinstrumentiru/lego@v1.0.2/internal/lego/monitor/telemetry/runner.go (about)

     1  package telemetry
     2  
     3  import (
     4  	"emperror.dev/emperror"
     5  	"emperror.dev/errors"
     6  	health "github.com/AppsFlyer/go-sundheit"
     7  	appkitrun "github.com/sagikazarmark/appkit/run"
     8  	lego2 "github.com/vseinstrumentiru/lego/internal/lego"
     9  	"github.com/vseinstrumentiru/lego/internal/lego/monitor/log"
    10  	"go.opencensus.io/plugin/ocgrpc"
    11  	"go.opencensus.io/plugin/ochttp"
    12  	"go.opencensus.io/stats/view"
    13  	"io"
    14  	"logur.dev/logur"
    15  	"net/http"
    16  )
    17  
    18  func Run(p lego2.Process, router *http.ServeMux, config Config) io.Closer {
    19  	const name = "telemetry"
    20  	logger := logur.WithField(p.Log(), "server", name)
    21  
    22  	logger.Info("listening on address", map[string]interface{}{"address": config.Addr})
    23  
    24  	ln, err := p.Listen("tcp", config.Addr)
    25  	emperror.Panic(err)
    26  
    27  	server := &http.Server{
    28  		Handler:  router,
    29  		ErrorLog: log.NewErrorStandardLogger(logger),
    30  	}
    31  
    32  	p.Background(appkitrun.LogServe(logger)(appkitrun.HTTPServe(server, ln, p.ShutdownTimeout())))
    33  
    34  	registerStats(config.Stats)
    35  
    36  	return server
    37  }
    38  
    39  func registerStats(stats []*view.View) {
    40  	err := view.Register(
    41  		// Health checks
    42  		health.ViewCheckCountByNameAndStatus,
    43  		health.ViewCheckStatusByName,
    44  		health.ViewCheckExecutionTime,
    45  
    46  		// HTTP Client
    47  		ochttp.ClientCompletedCount,
    48  		ochttp.ClientSentBytesDistribution,
    49  		ochttp.ClientReceivedBytesDistribution,
    50  		ochttp.ClientRoundtripLatencyDistribution,
    51  
    52  		// HTTP
    53  		ochttp.ServerRequestCountView,
    54  		ochttp.ServerRequestBytesView,
    55  		ochttp.ServerResponseBytesView,
    56  		ochttp.ServerLatencyView,
    57  		ochttp.ServerRequestCountByMethod,
    58  		ochttp.ServerResponseCountByStatusCode,
    59  
    60  		// GRPC Client
    61  		ocgrpc.ClientSentBytesPerRPCView,
    62  		ocgrpc.ClientReceivedBytesPerRPCView,
    63  		ocgrpc.ClientRoundtripLatencyView,
    64  		ocgrpc.ClientRoundtripLatencyView,
    65  
    66  		// GRPC
    67  		ocgrpc.ServerReceivedBytesPerRPCView,
    68  		ocgrpc.ServerSentBytesPerRPCView,
    69  		ocgrpc.ServerLatencyView,
    70  		ocgrpc.ServerCompletedRPCsView,
    71  	)
    72  
    73  	emperror.Panic(errors.Wrap(err, "failed to register stat views"))
    74  
    75  	if len(stats) > 0 {
    76  		err = view.Register(stats...)
    77  
    78  		emperror.Panic(errors.Wrap(err, "failed to register app stat views"))
    79  	}
    80  }