func NewRawAdapter(route *router.Route) (router.LogAdapter, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { transport, found = router.AdapterTransports.Lookup(route.AdapterTransport("unix")) if !found { return nil, errors.New("bad transport: " + route.Adapter) } } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } tmplStr := "{{.Data}}\n" if os.Getenv("RAW_FORMAT") != "" { tmplStr = os.Getenv("RAW_FORMAT") } tmpl, err := template.New("raw").Parse(tmplStr) if err != nil { return nil, err } return &RawAdapter{ route: route, conn: conn, tmpl: tmpl, }, nil }
func NewRawAdapter(route *router.Route) (router.LogAdapter, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { return nil, errors.New("bad transport: " + route.Adapter) } address = route.Address netType = strings.Replace(route.Adapter, "raw+", "", -1) conn, err := transport.Dial(route.Address, route.Options) connection = conn if err != nil { return nil, err } tmplStr := "{{.Data}}\n" if os.Getenv("RAW_FORMAT") != "" { tmplStr = os.Getenv("RAW_FORMAT") } tmpl, err := template.New("raw").Parse(tmplStr) if err != nil { return nil, err } go connPing() return &RawAdapter{ route: route, conn: conn, tmpl: tmpl, }, nil }
func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { return nil, errors.New("bad transport: " + route.Adapter) } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } format := getopt("SYSLOG_FORMAT", "rfc5424") priority := getopt("SYSLOG_PRIORITY", "{{.Priority}}") hostname := getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}") pid := getopt("SYSLOG_PID", "{{.Container.State.Pid}}") tag := getopt("SYSLOG_TAG", "{{.ContainerName}}"+route.Options["append_tag"]) structuredData := getopt("SYSLOG_STRUCTURED_DATA", "") if route.Options["structured_data"] != "" { structuredData = route.Options["structured_data"] } data := getopt("SYSLOG_DATA", "{{.Data}}") json := getoptexists("SYSLOG_JSON") if structuredData == "" { structuredData = "-" } else { structuredData = fmt.Sprintf("[%s]", structuredData) } var tmplStr string switch format { case "rfc5424": tmplStr = fmt.Sprintf("<%s>1 {{.Timestamp}} %s %s %s - %s %s\n", priority, hostname, tag, pid, structuredData, data) case "rfc3164": tmplStr = fmt.Sprintf("<%s>{{.Timestamp}} %s %s[%s]: %s\n", priority, hostname, tag, pid, data) default: return nil, errors.New("unsupported syslog format: " + format) } tmpl, err := template.New("syslog").Parse(tmplStr) if err != nil { return nil, err } return &SyslogAdapter{ route: route, conn: conn, tmpl: tmpl, transport: transport, json: json, }, nil }
func newDeisLogAdapter(route *router.Route) (router.LogAdapter, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("deis-udp")) if !found { return nil, errors.New("bad transport: " + route.Adapter) } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } return &deisLogAdapter{ route: route, conn: conn, }, nil }
// NewLogstashAdapter creates a LogstashAdapter with UDP as the default transport. func NewLogstashAdapter(route *router.Route) (router.LogAdapter, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { return nil, errors.New("unable to find adapter: " + route.Adapter) } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } return &LogstashAdapter{ route: route, conn: conn, }, nil }
// NewFluentdAdapter creates a Logspout fluentd adapter instance. func NewFluentdAdapter(route *router.Route) (router.LogAdapter, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("tcp")) if !found { return nil, errors.New("bad transport: " + route.Adapter) } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } return &FluentdAdapter{ conn: conn, route: route, transport: transport, }, nil }
func NewRedisAdapter(route *router.Route) (router.LogAdapter, error) { address := getopt("REDIS_HOST", "") if address == "" { address := route.Address if !strings.Contains(address, ":") { address = address + ":6379" } } else { port := getopt("REDIS_PORT", "6379") address = address + ":" + port } route.Address = address key := route.Options["key"] if key == "" { key = getopt("REDIS_KEY", "logspout") } password := route.Options["password"] if password == "" { password = getopt("REDIS_PASSWORD", "") } docker_host := getopt("REDIS_DOCKER_HOST", "") use_v0 := route.Options["use_v0_layout"] != "" if !use_v0 { use_v0 = getopt("REDIS_USE_V0_LAYOUT", "") != "" } if os.Getenv("DEBUG") != "" { log.Printf("Using Redis server '%s', password: %t, pushkey: '%s', v0 layout: %t\n", address, password != "", key, use_v0) } pool := newRedisConnectionPool(address, password) // lets test the water conn := pool.Get() defer conn.Close() res, err := conn.Do("PING") if err != nil { return nil, errorf("Cannot connect to Redis server %s: %v", address, err) } if os.Getenv("DEBUG") != "" { log.Printf("Redis connect successful, got response: %s\n", res) } return &RedisAdapter{ route: route, pool: pool, key: key, docker_host: docker_host, use_v0: use_v0, }, nil }
func connect(route *router.Route) (net.Conn, error) { transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { return nil, errors.New("unable to find adapter: " + route.Adapter) } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } if reflect.TypeOf(conn).String() == "*net.TCPConn" { tcpconn := conn.(*net.TCPConn) err = tcpconn.SetKeepAlive(true) if err != nil { return nil, err } } return conn, nil }
func NewCloudWatchLogsAdapter(route *router.Route) (router.LogAdapter, error) { // Connect this adapter. transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { return nil, errors.New("Bad transport: " + route.Adapter) } conn, err := transport.Dial(route.Address, route.Options) if err != nil { return nil, err } // Build out what the logs template should look like. tmpl, err := template.New("cloudwatchlogs").Parse("{{.Data}}\n") if err != nil { return nil, err } return &CloudWatchLogsAdapter{ route: route, conn: conn, tmpl: tmpl, }, nil }
func NewKafkaRawAdapter(route *router.Route) (router.LogAdapter, error) { topic = os.Getenv("TOPIC") compressType := os.Getenv("COMPRESS_TYPE") if topic == "" { err := errors.New("not found kafka topic") return nil, err } transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp")) if !found { return nil, errors.New("bad transport: " + route.Adapter) } _ = transport config := kafka.NewConfig() if compressType == "gzip" { config.Producer.Compression = kafka.CompressionGZIP } else if compressType == "snappy" { config.Producer.Compression = kafka.CompressionSnappy } else { config.Producer.Compression = kafka.CompressionNone } producer, err := kafka.NewSyncProducer([]string{route.Address}, config) if err != nil { return nil, err } tmplStr := "{{.Data}}\n" if os.Getenv("RAW_FORMAT") != "" { tmplStr = os.Getenv("RAW_FORMAT") } tmpl, err := template.New("raw").Parse(tmplStr) if err != nil { return nil, err } return &RawAdapter{ route: route, producer: producer, tmpl: tmpl, }, nil }
func LogStreamer() http.Handler { logs := mux.NewRouter() logsHandler := func(w http.ResponseWriter, req *http.Request) { params := mux.Vars(req) route := new(router.Route) if params["value"] != "" { switch params["predicate"] { case "id": route.FilterID = params["value"] if len(route.ID) > 12 { route.FilterID = route.FilterID[:12] } case "name": route.FilterName = params["value"] } } if route.FilterID != "" && !router.Routes.RoutingFrom(route.FilterID) { http.NotFound(w, req) return } defer debug("http: logs streamer disconnected") logstream := make(chan *router.Message) defer close(logstream) var closer <-chan bool if req.Header.Get("Upgrade") == "websocket" { debug("http: logs streamer connected [websocket]") closerBi := make(chan bool) defer websocketStreamer(w, req, logstream, closerBi) closer = closerBi } else { debug("http: logs streamer connected [http]") defer httpStreamer(w, req, logstream, route.MultiContainer()) closer = w.(http.CloseNotifier).CloseNotify() } route.OverrideCloser(closer) router.Routes.Route(route, logstream) } logs.HandleFunc("/logs/{predicate:[a-zA-Z]+}:{value}", logsHandler).Methods("GET") logs.HandleFunc("/logs", logsHandler).Methods("GET") return logs }
func rawTCPAdapter(route *router.Route) (router.LogAdapter, error) { route.Adapter = "raw+tcp" return raw.NewRawAdapter(route) }
func rawKafkaAdapter(route *router.Route) (router.LogAdapter, error) { route.Adapter = "kafkaraw+tcp" return kafkaraw.NewKafkaRawAdapter(route) }
func rawUnixAdapter(route *router.Route) (router.LogAdapter, error) { route.Adapter = "raw+unix" return raw.NewRawAdapter(route) }