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