mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 18:20:27 +00:00
Vendor new weaveworks/common
This commit is contained in:
135
vendor/github.com/weaveworks/common/logging/event.go
generated
vendored
135
vendor/github.com/weaveworks/common/logging/event.go
generated
vendored
@@ -1,135 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/fluent/fluent-logger-golang/fluent"
|
||||
)
|
||||
|
||||
const maxBufferedEvents = 100
|
||||
|
||||
// Event is a user event to be sent to out analytics system
|
||||
type Event struct {
|
||||
ID string `msg:"event"`
|
||||
SessionID string `msg:"session_id"`
|
||||
Product string `msg:"product"`
|
||||
Version string `msg:"version"`
|
||||
UserAgent string `msg:"user_agent"`
|
||||
ClientID string `msg:"client_id"`
|
||||
OrganizationID string `msg:"org_id"`
|
||||
UserID string `msg:"user_id"`
|
||||
Values string `msg:"values"`
|
||||
}
|
||||
|
||||
// EventLogger logs events to the analytics system
|
||||
type EventLogger struct {
|
||||
stop chan struct{}
|
||||
events chan Event
|
||||
logger *fluent.Fluent
|
||||
}
|
||||
|
||||
// NewEventLogger creates a new EventLogger.
|
||||
func NewEventLogger(fluentHostPort string) (*EventLogger, error) {
|
||||
host, port, err := net.SplitHostPort(fluentHostPort)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
intPort, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger, err := fluent.New(fluent.Config{
|
||||
FluentPort: intPort,
|
||||
FluentHost: host,
|
||||
AsyncConnect: true,
|
||||
MaxRetry: -1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
el := &EventLogger{
|
||||
stop: make(chan struct{}),
|
||||
events: make(chan Event, maxBufferedEvents),
|
||||
logger: logger,
|
||||
}
|
||||
go el.logLoop()
|
||||
return el, nil
|
||||
}
|
||||
|
||||
func (el *EventLogger) post(e Event) {
|
||||
if err := el.logger.Post("events", e); err != nil {
|
||||
log.Warnf("EventLogger: failed to log event: %v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func (el *EventLogger) logLoop() {
|
||||
for done := false; !done; {
|
||||
select {
|
||||
case event := <-el.events:
|
||||
el.post(event)
|
||||
case <-el.stop:
|
||||
done = true
|
||||
}
|
||||
}
|
||||
|
||||
// flush remaining events
|
||||
for done := false; !done; {
|
||||
select {
|
||||
case event := <-el.events:
|
||||
el.post(event)
|
||||
default:
|
||||
done = true
|
||||
}
|
||||
}
|
||||
|
||||
el.logger.Close()
|
||||
}
|
||||
|
||||
// Close closes and deallocates the event logger
|
||||
func (el *EventLogger) Close() error {
|
||||
close(el.stop)
|
||||
return nil
|
||||
}
|
||||
|
||||
// LogEvent logs an event to the analytics system
|
||||
func (el *EventLogger) LogEvent(e Event) error {
|
||||
select {
|
||||
case <-el.stop:
|
||||
return fmt.Errorf("Stopping, discarding event: %v", e)
|
||||
default:
|
||||
}
|
||||
|
||||
select {
|
||||
case el.events <- e: // Put event in the channel unless it is full
|
||||
return nil
|
||||
default:
|
||||
// full
|
||||
}
|
||||
return fmt.Errorf("Reached event buffer limit (%d), discarding event: %v", maxBufferedEvents, e)
|
||||
}
|
||||
|
||||
// HTTPEventExtractor extracts an event from an http requests indicating whether it should be loggged
|
||||
type HTTPEventExtractor func(*http.Request) (Event, bool)
|
||||
|
||||
// HTTPEventLogger logs an events extracted from an http request
|
||||
type HTTPEventLogger struct {
|
||||
Extractor HTTPEventExtractor
|
||||
Logger *EventLogger
|
||||
}
|
||||
|
||||
// Wrap implements middleware.Wrap()
|
||||
func (el HTTPEventLogger) Wrap(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if event, shouldLog := el.Extractor(r); shouldLog {
|
||||
if err := el.Logger.LogEvent(event); err != nil {
|
||||
log.Warnf("HTTPEventLogger: failed to log event: %v", err)
|
||||
}
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
16
vendor/github.com/weaveworks/common/middleware/logging.go
generated
vendored
16
vendor/github.com/weaveworks/common/middleware/logging.go
generated
vendored
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"time"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
@@ -12,7 +13,8 @@ import (
|
||||
|
||||
// Log middleware logs http requests
|
||||
type Log struct {
|
||||
LogSuccess bool // LogSuccess true -> log successful queries; false -> only log failed queries
|
||||
LogSuccess bool // LogSuccess true -> log successful queries; false -> only log failed queries
|
||||
LogRequestHeaders bool // LogRequestHeaders true -> dump http headers at debug log level
|
||||
}
|
||||
|
||||
// Wrap implements Middleware
|
||||
@@ -20,6 +22,15 @@ func (l Log) Wrap(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
begin := time.Now()
|
||||
uri := r.RequestURI // capture the URI before running next, as it may get rewritten
|
||||
if l.LogRequestHeaders {
|
||||
// Log headers before running 'next' in case other interceptors change the data.
|
||||
headers, err := httputil.DumpRequest(r, false)
|
||||
if err != nil {
|
||||
log.Warnf("Could not dump request headers: %v", err)
|
||||
return
|
||||
}
|
||||
log.Debugf(string(headers))
|
||||
}
|
||||
i := &interceptor{ResponseWriter: w, statusCode: http.StatusOK}
|
||||
next.ServeHTTP(i, r)
|
||||
if l.LogSuccess || !(100 <= i.statusCode && i.statusCode < 400) {
|
||||
@@ -31,7 +42,8 @@ func (l Log) Wrap(next http.Handler) http.Handler {
|
||||
// Logging middleware logs each HTTP request method, path, response code and
|
||||
// duration for all HTTP requests.
|
||||
var Logging = Log{
|
||||
LogSuccess: true,
|
||||
LogSuccess: true,
|
||||
LogRequestHeaders: false,
|
||||
}
|
||||
|
||||
// LogFailed middleware logs each HTTP request method, path, response code and
|
||||
|
||||
48
vendor/github.com/weaveworks/common/middleware/redirect.go
generated
vendored
Normal file
48
vendor/github.com/weaveworks/common/middleware/redirect.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Redirect middleware, will redirect requests to hosts which match any of the
|
||||
// Matches to RedirectScheme://RedirectHost
|
||||
type Redirect struct {
|
||||
Matches []Match
|
||||
|
||||
RedirectHost string
|
||||
RedirectScheme string
|
||||
}
|
||||
|
||||
// Match specifies a match for a redirect. Host and/or Scheme can be empty
|
||||
// signify match-all.
|
||||
type Match struct {
|
||||
Host, Scheme string
|
||||
}
|
||||
|
||||
func (m Match) match(u *url.URL) bool {
|
||||
if m.Host != "" && m.Host != u.Host {
|
||||
return false
|
||||
}
|
||||
|
||||
if m.Scheme != "" && m.Scheme != u.Scheme {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Wrap implements Middleware
|
||||
func (m Redirect) Wrap(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
for _, match := range m.Matches {
|
||||
if match.match(r.URL) {
|
||||
r.URL.Host = m.RedirectHost
|
||||
r.URL.Scheme = m.RedirectScheme
|
||||
http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
|
||||
return
|
||||
}
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
2
vendor/manifest
vendored
2
vendor/manifest
vendored
@@ -1361,7 +1361,7 @@
|
||||
"importpath": "github.com/weaveworks/common",
|
||||
"repository": "https://github.com/weaveworks/common",
|
||||
"vcs": "git",
|
||||
"revision": "cc20acf03ebf74be0facaae4259dff6cde01ce77",
|
||||
"revision": "033d5c87743012524cc813c4a66f7d18afdb20b6",
|
||||
"branch": "master",
|
||||
"notests": true
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user