...

Source file src/github.com/palantir/go-baseapp/baseapp/params.go

Documentation: github.com/palantir/go-baseapp/baseapp

     1  // Copyright 2018 Palantir Technologies, Inc.
     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 baseapp
    16  
    17  import (
    18  	"net/http"
    19  	"time"
    20  
    21  	"github.com/rcrowley/go-metrics"
    22  	"github.com/rs/zerolog"
    23  )
    24  
    25  const (
    26  	// nanoTimeFormat is the same as time.RFC3339Nano, but uses a consistent
    27  	// number of digits instead of removing trailing zeros
    28  	nanoTimeFormat = "2006-01-02T15:04:05.000000000Z07:00"
    29  )
    30  
    31  // DefaultParams returns a recommended set of parameters for servers. It
    32  // enables logging and configures logging, adds metrics, and adds default
    33  // middleware. All component parameters are exported and can be selected
    34  // individually if desired.
    35  func DefaultParams(logger zerolog.Logger, metricsPrefix string) []Param {
    36  	var registry metrics.Registry
    37  	if metricsPrefix == "" {
    38  		registry = metrics.NewRegistry()
    39  	} else {
    40  		registry = metrics.NewPrefixedRegistry(metricsPrefix)
    41  	}
    42  
    43  	return []Param{
    44  		WithLogger(logger),
    45  		WithRegistry(registry),
    46  		WithMiddleware(DefaultMiddleware(logger, registry)...),
    47  		WithUTCNanoTime(),
    48  		WithErrorLogging(RichErrorMarshalFunc),
    49  		WithMetrics(),
    50  	}
    51  }
    52  
    53  // WithLogger sets a root logger used by the server.
    54  func WithLogger(logger zerolog.Logger) Param {
    55  	return func(b *Server) error {
    56  		b.logger = logger
    57  		return nil
    58  	}
    59  }
    60  
    61  // WithMiddleware sets middleware that is applied to all routes handled by the
    62  // server.
    63  func WithMiddleware(middleware ...func(http.Handler) http.Handler) Param {
    64  	return func(b *Server) error {
    65  		b.middleware = middleware
    66  		return nil
    67  	}
    68  }
    69  
    70  // WithUTCNanoTime adds a UTC timestamp with nanosecond precision to log lines.
    71  func WithUTCNanoTime() Param {
    72  	return func(b *Server) error {
    73  		zerolog.TimeFieldFormat = nanoTimeFormat
    74  		zerolog.TimestampFunc = func() time.Time {
    75  			return time.Now().UTC()
    76  		}
    77  		return nil
    78  	}
    79  }
    80  
    81  // WithErrorLogging sets a formatting function used to log errors.
    82  func WithErrorLogging(marshalFunc func(err error) interface{}) Param {
    83  	return func(b *Server) error {
    84  		zerolog.ErrorMarshalFunc = marshalFunc
    85  		return nil
    86  	}
    87  }
    88  
    89  // WithRegistry sets the metrics registry for the server.
    90  func WithRegistry(registry metrics.Registry) Param {
    91  	return func(b *Server) error {
    92  		b.registry = registry
    93  		return nil
    94  	}
    95  }
    96  
    97  // WithMetrics enables server and runtime metrics collection.
    98  func WithMetrics() Param {
    99  	return func(s *Server) error {
   100  		s.initFns = append(s.initFns, func(s *Server) { RegisterDefaultMetrics(s.Registry()) })
   101  		return nil
   102  	}
   103  }
   104  
   105  func WithHTTPServer(server *http.Server) Param {
   106  	return func(s *Server) error {
   107  		s.server = server
   108  		return nil
   109  	}
   110  }
   111  

View as plain text