github.com/newrelic/newrelic-client-go@v1.1.0/pkg/config/config.go (about)

     1  // Package config provides cross-cutting configuration support for the newrelic-client-go project.
     2  package config
     3  
     4  import (
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/newrelic/newrelic-client-go/internal/version"
     9  	"github.com/newrelic/newrelic-client-go/pkg/logging"
    10  	"github.com/newrelic/newrelic-client-go/pkg/region"
    11  )
    12  
    13  // Config contains all the configuration data for the API Client.
    14  type Config struct {
    15  	// LicenseKey to authenticate Log API requests
    16  	// see: https://docs.newrelic.com/docs/accounts/accounts-billing/account-setup/new-relic-license-key
    17  	LicenseKey string
    18  
    19  	// PersonalAPIKey to authenticate API requests
    20  	// see: https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys#personal-api-key
    21  	PersonalAPIKey string
    22  
    23  	// AdminAPIKey to authenticate API requests
    24  	// Deprecated.  Use a Personal API key for authentication.
    25  	// see: https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys#admin
    26  	AdminAPIKey string
    27  
    28  	// InsightsInsertKey to send custom events to Insights
    29  	InsightsInsertKey string
    30  
    31  	// region of the New Relic platform to use
    32  	region *region.Region
    33  
    34  	// Timeout is the client timeout for HTTP requests.
    35  	Timeout *time.Duration
    36  
    37  	// HTTPTransport allows customization of the client's underlying transport.
    38  	HTTPTransport http.RoundTripper
    39  
    40  	// Compression used in sending data in HTTP requests.
    41  	Compression CompressionType
    42  
    43  	// UserAgent updates the default user agent string used by the client.
    44  	UserAgent string
    45  
    46  	// ServiceName is for New Relic internal use only.
    47  	ServiceName string
    48  
    49  	// LogLevel can be one of the following values:
    50  	// "panic", "fatal", "error", "warn", "info", "debug", "trace"
    51  	LogLevel string
    52  
    53  	// LogJSON toggles formatting of log entries in JSON format.
    54  	LogJSON bool
    55  
    56  	// Logger allows customization of the client's underlying logger.
    57  	Logger logging.Logger
    58  }
    59  
    60  // New creates a default configuration and returns it
    61  func New() Config {
    62  	reg, _ := region.Get(region.Default)
    63  
    64  	return Config{
    65  		region:      reg,
    66  		LogLevel:    "info",
    67  		Compression: Compression.None,
    68  	}
    69  }
    70  
    71  // Region returns the region configuration struct
    72  // if one has not been set, use the default region
    73  func (c *Config) Region() *region.Region {
    74  	if c.region == nil {
    75  		reg, _ := region.Get(region.Default)
    76  		c.region = reg
    77  	}
    78  
    79  	return c.region
    80  }
    81  
    82  // SetRegion configures the region
    83  func (c *Config) SetRegion(reg *region.Region) error {
    84  	if reg == nil {
    85  		return region.ErrorNil()
    86  	}
    87  
    88  	c.region = reg
    89  
    90  	return nil
    91  }
    92  
    93  // GetLogger returns a logger instance based on the config values.
    94  func (c *Config) GetLogger() logging.Logger {
    95  	if c.Logger != nil {
    96  		return c.Logger
    97  	}
    98  
    99  	l := logging.NewLogrusLogger()
   100  	l.SetDefaultFields(map[string]string{"newrelic-client-go": version.Version})
   101  	l.SetLogJSON(c.LogJSON)
   102  	l.SetLevel(c.LogLevel)
   103  
   104  	// c.Logger = l
   105  
   106  	return l
   107  }