github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/config/logging.go (about)

     1  package config
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/smartcontractkit/chainlink-testing-framework/libs/blockchain"
     8  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/net"
     9  )
    10  
    11  type LoggingConfig struct {
    12  	TestLogCollect *bool            `toml:"test_log_collect"`
    13  	RunId          *string          `toml:"run_id"`
    14  	Loki           *LokiConfig      `toml:"Loki"`
    15  	Grafana        *GrafanaConfig   `toml:"Grafana"`
    16  	LogStream      *LogStreamConfig `toml:"LogStream"`
    17  }
    18  
    19  // Validate executes config validation for LogStream, Grafana and Loki
    20  func (l *LoggingConfig) Validate() error {
    21  	if l.LogStream != nil {
    22  		if err := l.LogStream.Validate(); err != nil {
    23  			return fmt.Errorf("invalid log stream config: %w", err)
    24  		}
    25  	}
    26  
    27  	if l.Grafana != nil {
    28  		if err := l.Grafana.Validate(); err != nil {
    29  			return fmt.Errorf("invalid grafana config: %w", err)
    30  		}
    31  	}
    32  
    33  	if l.Loki != nil {
    34  		if err := l.Loki.Validate(); err != nil {
    35  			return fmt.Errorf("invalid loki config: %w", err)
    36  		}
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  type LogStreamConfig struct {
    43  	LogTargets            []string                `toml:"log_targets"`
    44  	LogProducerTimeout    *blockchain.StrDuration `toml:"log_producer_timeout"`
    45  	LogProducerRetryLimit *uint                   `toml:"log_producer_retry_limit"`
    46  }
    47  
    48  // Validate checks that the log stream config is valid, which means that
    49  // log targets are valid and log producer timeout is greater than 0
    50  func (l *LogStreamConfig) Validate() error {
    51  	if len(l.LogTargets) > 0 {
    52  		for _, target := range l.LogTargets {
    53  			if target != "loki" && target != "file" && target != "in-memory" {
    54  				return fmt.Errorf("invalid log target %s", target)
    55  			}
    56  		}
    57  	}
    58  
    59  	if l.LogProducerTimeout != nil {
    60  		if l.LogProducerTimeout.Duration == 0 {
    61  			return errors.New("log producer timeout must be greater than 0")
    62  		}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  type LokiConfig struct {
    69  	TenantId    *string `toml:"tenant_id"`
    70  	Endpoint    *string `toml:"endpoint"`
    71  	BasicAuth   *string `toml:"basic_auth_secret"`
    72  	BearerToken *string `toml:"bearer_token_secret"`
    73  }
    74  
    75  // Validate checks that the loki config is valid, which means that
    76  // endpoint is a valid URL and tenant id is not empty
    77  func (l *LokiConfig) Validate() error {
    78  	if l.Endpoint != nil {
    79  		if !net.IsValidURL(*l.Endpoint) {
    80  			return fmt.Errorf("invalid loki endpoint %s", *l.Endpoint)
    81  		}
    82  	}
    83  	if l.TenantId == nil || *l.TenantId == "" {
    84  		return errors.New("loki tenant id must be set")
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  type GrafanaConfig struct {
    91  	BaseUrl      *string `toml:"base_url"`
    92  	DashboardUrl *string `toml:"dashboard_url"`
    93  	BearerToken  *string `toml:"bearer_token_secret"`
    94  }
    95  
    96  // Validate checks that the grafana config is valid, which means that
    97  // base url is a valid URL and dashboard url and bearer token are not empty
    98  // but that only applies if they are set
    99  func (c *GrafanaConfig) Validate() error {
   100  	if c.BaseUrl != nil {
   101  		if !net.IsValidURL(*c.BaseUrl) {
   102  			return fmt.Errorf("invalid grafana url %s", *c.BaseUrl)
   103  		}
   104  	}
   105  	if c.DashboardUrl != nil {
   106  		if *c.DashboardUrl == "" {
   107  			return errors.New("if set, grafana dashboard url cannot be an empty string")
   108  		}
   109  	}
   110  	if c.BearerToken != nil {
   111  		if *c.BearerToken == "" {
   112  			return errors.New("if set, grafana Bearer token cannot be an empty string")
   113  		}
   114  	}
   115  
   116  	return nil
   117  }