|
|
@ -37,7 +37,9 @@ type Target struct { |
|
|
|
|
|
|
|
|
|
|
|
// HTTP(s) endpoint
|
|
|
|
// HTTP(s) endpoint
|
|
|
|
endpoint string |
|
|
|
endpoint string |
|
|
|
// User-Agent to be set on each log request sent to the `endpoint`
|
|
|
|
// Authorization token for `endpoint`
|
|
|
|
|
|
|
|
authToken string |
|
|
|
|
|
|
|
// User-Agent to be set on each log to `endpoint`
|
|
|
|
userAgent string |
|
|
|
userAgent string |
|
|
|
logKind string |
|
|
|
logKind string |
|
|
|
client http.Client |
|
|
|
client http.Client |
|
|
@ -53,7 +55,7 @@ func (h *Target) startHTTPLogger() { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, h.endpoint, bytes.NewBuffer(logJSON)) |
|
|
|
req, err := http.NewRequest(http.MethodPost, h.endpoint, bytes.NewReader(logJSON)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
@ -63,6 +65,10 @@ func (h *Target) startHTTPLogger() { |
|
|
|
// version to the configured log endpoint
|
|
|
|
// version to the configured log endpoint
|
|
|
|
req.Header.Set("User-Agent", h.userAgent) |
|
|
|
req.Header.Set("User-Agent", h.userAgent) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if h.authToken != "" { |
|
|
|
|
|
|
|
req.Header.Set("Authorization", h.authToken) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp, err := h.client.Do(req) |
|
|
|
resp, err := h.client.Do(req) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
h.client.CloseIdleConnections() |
|
|
|
h.client.CloseIdleConnections() |
|
|
@ -75,21 +81,62 @@ func (h *Target) startHTTPLogger() { |
|
|
|
}() |
|
|
|
}() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Option is a function type that accepts a pointer Target
|
|
|
|
|
|
|
|
type Option func(*Target) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithEndpoint adds a new endpoint
|
|
|
|
|
|
|
|
func WithEndpoint(endpoint string) Option { |
|
|
|
|
|
|
|
return func(t *Target) { |
|
|
|
|
|
|
|
t.endpoint = endpoint |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithLogKind adds a log type for this target
|
|
|
|
|
|
|
|
func WithLogKind(logKind string) Option { |
|
|
|
|
|
|
|
return func(t *Target) { |
|
|
|
|
|
|
|
t.logKind = strings.ToUpper(logKind) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithUserAgent adds a custom user-agent sent to the target.
|
|
|
|
|
|
|
|
func WithUserAgent(userAgent string) Option { |
|
|
|
|
|
|
|
return func(t *Target) { |
|
|
|
|
|
|
|
t.userAgent = userAgent |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithAuthToken adds a new authorization header to be sent to target.
|
|
|
|
|
|
|
|
func WithAuthToken(authToken string) Option { |
|
|
|
|
|
|
|
return func(t *Target) { |
|
|
|
|
|
|
|
t.authToken = authToken |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithTransport adds a custom transport with custom timeouts and tuning.
|
|
|
|
|
|
|
|
func WithTransport(transport *http.Transport) Option { |
|
|
|
|
|
|
|
return func(t *Target) { |
|
|
|
|
|
|
|
t.client = http.Client{ |
|
|
|
|
|
|
|
Transport: transport, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// New initializes a new logger target which
|
|
|
|
// New initializes a new logger target which
|
|
|
|
// sends log over http to the specified endpoint
|
|
|
|
// sends log over http to the specified endpoint
|
|
|
|
func New(endpoint, userAgent, logKind string, transport *http.Transport) *Target { |
|
|
|
func New(opts ...Option) *Target { |
|
|
|
h := Target{ |
|
|
|
h := &Target{ |
|
|
|
endpoint: endpoint, |
|
|
|
|
|
|
|
userAgent: userAgent, |
|
|
|
|
|
|
|
logKind: strings.ToUpper(logKind), |
|
|
|
|
|
|
|
client: http.Client{ |
|
|
|
|
|
|
|
Transport: transport, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
logCh: make(chan interface{}, 10000), |
|
|
|
logCh: make(chan interface{}, 10000), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Loop through each option
|
|
|
|
|
|
|
|
for _, opt := range opts { |
|
|
|
|
|
|
|
// Call the option giving the instantiated
|
|
|
|
|
|
|
|
// *Target as the argument
|
|
|
|
|
|
|
|
opt(h) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
h.startHTTPLogger() |
|
|
|
h.startHTTPLogger() |
|
|
|
return &h |
|
|
|
return h |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Send log message 'e' to http target.
|
|
|
|
// Send log message 'e' to http target.
|
|
|
@ -97,6 +144,7 @@ func (h *Target) Send(entry interface{}, errKind string) error { |
|
|
|
if h.logKind != errKind && h.logKind != "ALL" { |
|
|
|
if h.logKind != errKind && h.logKind != "ALL" { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
select { |
|
|
|
case h.logCh <- entry: |
|
|
|
case h.logCh <- entry: |
|
|
|
default: |
|
|
|
default: |
|
|
|