get.porter.sh/porter@v1.3.0/pkg/config/logs.go (about)

     1  package config
     2  
     3  import (
     4  	"time"
     5  
     6  	"go.uber.org/zap/zapcore"
     7  )
     8  
     9  const (
    10  	LogLevelDebug LogLevel = "debug"
    11  	LogLevelInfo  LogLevel = "info"
    12  	LogLevelWarn  LogLevel = "warn"
    13  	LogLevelError LogLevel = "error"
    14  )
    15  
    16  // LogConfig are settings related to Porter's log files.
    17  type LogConfig struct {
    18  	// Structured indicates if the logs sent to the console should include timestamp and log levels
    19  	Structured bool     `mapstructure:"structured"`
    20  	LogToFile  bool     `mapstructure:"log-to-file"`
    21  	Level      LogLevel `mapstructure:"level"`
    22  }
    23  
    24  // TelemetryConfig specifies how to connect to an open telemetry collector.
    25  // See https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/otlp/otlptrace
    26  type TelemetryConfig struct {
    27  	Enabled     bool              `mapstructure:"enabled"`
    28  	Endpoint    string            `mapstructure:"endpoint"`
    29  	Protocol    string            `mapstructure:"protocol"`
    30  	Insecure    bool              `mapstructure:"insecure"`
    31  	Certificate string            `mapstructure:"certificate"`
    32  	Headers     map[string]string `mapstructure:"headers"`
    33  	Timeout     string            `mapstructure:"timeout"`
    34  	Compression string            `mapstructure:"compression"`
    35  
    36  	// RedirectToFile instructs Porter to write telemetry data to a file in
    37  	// PORTER_HOME/traces instead of exporting it to a collector
    38  	RedirectToFile bool `mapstructure:"redirect-to-file"`
    39  
    40  	// StartTimeout sets the amount of time to wait while establishing a connection
    41  	// to the OpenTelemetry collector.
    42  	StartTimeout string `mapstructure:"start-timeout"`
    43  }
    44  
    45  // GetStartTimeout returns the amount of time to wait for the collector to start
    46  // if a value was not configured, return the default timeout.
    47  func (c TelemetryConfig) GetStartTimeout() time.Duration {
    48  	if timeout, err := time.ParseDuration(c.StartTimeout); err == nil {
    49  		return timeout
    50  	}
    51  	return 100 * time.Millisecond
    52  }
    53  
    54  type LogLevel string
    55  
    56  // ParseLogLevel reads the string representation of a LogLevel and converts it to a LogLevel.
    57  // Unrecognized values default to info.
    58  func ParseLogLevel(value string) LogLevel {
    59  	switch value {
    60  	case "debug", "info", "warn", "error":
    61  		return LogLevel(value)
    62  	case "warning": // be nice to people who can't type
    63  		return "warn"
    64  	default:
    65  		return "info"
    66  	}
    67  }
    68  
    69  func (l LogLevel) Level() zapcore.Level {
    70  	switch l {
    71  	case "debug":
    72  		return zapcore.DebugLevel
    73  	case "info":
    74  		return zapcore.InfoLevel
    75  	case "warn", "warning":
    76  		return zapcore.WarnLevel
    77  	case "error":
    78  		return zapcore.ErrorLevel
    79  	default:
    80  		return zapcore.InfoLevel
    81  	}
    82  }