github.com/blend/go-sdk@v1.20220411.3/stats/vaultstats/listeners.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package vaultstats
     9  
    10  import (
    11  	"context"
    12  	"strconv"
    13  
    14  	"github.com/blend/go-sdk/logger"
    15  	"github.com/blend/go-sdk/stats"
    16  	"github.com/blend/go-sdk/timeutil"
    17  	"github.com/blend/go-sdk/vault"
    18  )
    19  
    20  // AddListeners adds web listeners.
    21  func AddListeners(log logger.Listenable, collector stats.Collector, opts ...stats.AddListenerOption) {
    22  	if log == nil || collector == nil {
    23  		return
    24  	}
    25  
    26  	options := stats.NewAddListenerOptions(opts...)
    27  
    28  	log.Listen(vault.Flag, stats.ListenerNameStats, vault.NewEventListener(func(ctx context.Context, ve vault.Event) {
    29  		tags := []string{
    30  			stats.Tag("method", ve.Method),
    31  			stats.Tag("status", strconv.Itoa(ve.StatusCode)),
    32  			stats.Tag("path", ve.Path),
    33  		}
    34  		tags = append(tags, options.GetLoggerLabelsAsTags(ctx)...)
    35  		_ = collector.Increment("vault.request", tags...)
    36  		_ = collector.TimeInMilliseconds("vault.request.elapsed", ve.Elapsed, tags...)
    37  		_ = collector.Histogram("vault.request.elapsed", timeutil.Milliseconds(ve.Elapsed), tags...)
    38  	}))
    39  }