You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.2 KiB
95 lines
2.2 KiB
6 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
6 years ago
|
package http
|
||
6 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
6 years ago
|
"io"
|
||
|
"io/ioutil"
|
||
6 years ago
|
gohttp "net/http"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Target implements logger.Target and sends the json
|
||
6 years ago
|
// format of a log entry to the configured http endpoint.
|
||
|
// An internal buffer of logs is maintained but when the
|
||
|
// buffer is full, new logs are just ignored and an error
|
||
|
// is returned to the caller.
|
||
6 years ago
|
type Target struct {
|
||
6 years ago
|
// Channel of log entries
|
||
6 years ago
|
logCh chan interface{}
|
||
|
|
||
6 years ago
|
// HTTP(s) endpoint
|
||
|
endpoint string
|
||
6 years ago
|
client gohttp.Client
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (h *Target) startHTTPLogger() {
|
||
6 years ago
|
// Create a routine which sends json logs received
|
||
|
// from an internal channel.
|
||
|
go func() {
|
||
|
for entry := range h.logCh {
|
||
|
logJSON, err := json.Marshal(&entry)
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
|
||
6 years ago
|
req, err := gohttp.NewRequest("POST", h.endpoint, bytes.NewBuffer(logJSON))
|
||
6 years ago
|
req.Header.Set("Content-Type", "application/json")
|
||
|
|
||
|
resp, err := h.client.Do(req)
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
if resp.Body != nil {
|
||
6 years ago
|
buf := make([]byte, 512)
|
||
|
io.CopyBuffer(ioutil.Discard, resp.Body, buf)
|
||
6 years ago
|
resp.Body.Close()
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
6 years ago
|
// New initializes a new logger target which
|
||
6 years ago
|
// sends log over http to the specified endpoint
|
||
6 years ago
|
func New(endpoint string, transport *gohttp.Transport) *Target {
|
||
|
h := Target{
|
||
6 years ago
|
endpoint: endpoint,
|
||
6 years ago
|
client: gohttp.Client{
|
||
6 years ago
|
Transport: transport,
|
||
|
},
|
||
6 years ago
|
logCh: make(chan interface{}, 10000),
|
||
6 years ago
|
}
|
||
|
|
||
|
h.startHTTPLogger()
|
||
|
return &h
|
||
|
}
|
||
|
|
||
6 years ago
|
// Send log message 'e' to http target.
|
||
|
func (h *Target) Send(entry interface{}) error {
|
||
6 years ago
|
select {
|
||
|
case h.logCh <- entry:
|
||
|
default:
|
||
|
// log channel is full, do not wait and return
|
||
|
// an error immediately to the caller
|
||
|
return errors.New("log buffer full")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|