-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"io" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type BaseConsumer struct { | ||
HostFieldName string // name of "host" field in json log message | ||
MessageFieldName string // name of "message" field in json log message | ||
} | ||
|
||
type LogMessage struct { | ||
Host string | ||
Message string | ||
|
||
data map[string]interface{} | ||
} | ||
|
||
func (c *BaseConsumer) parseJSONs(body io.ReadCloser) []*LogMessage { | ||
messages := make([]*LogMessage, 0) // TODO: change to channel (don't parse all json messages into memory) | ||
|
||
reader := bufio.NewReader(body) | ||
for { | ||
bytes, errReader := reader.ReadBytes('\n') | ||
|
||
if errReader != nil && errReader != io.EOF { | ||
log.WithError(errReader).Error("can't read line from POST body") | ||
break | ||
} | ||
|
||
var data map[string]interface{} | ||
|
||
if err := json.Unmarshal(bytes, &data); err != nil { | ||
log.WithError(err).WithField("line", string(bytes)).Error("can't parse line from POST body as JSON") | ||
break | ||
} | ||
|
||
var host, message string | ||
|
||
if value, ok := data[c.HostFieldName].(string); ok { | ||
host = value | ||
} else { | ||
log.WithField("field_name", c.HostFieldName). | ||
WithField("value", data[c.HostFieldName]). | ||
Error("can't find/convert 'host' field from JSON to string") | ||
break | ||
} | ||
|
||
if value, ok := data[c.MessageFieldName].(string); ok { | ||
message = value | ||
} else { | ||
log.WithField("field_name", c.MessageFieldName). | ||
WithField("value", data[c.MessageFieldName]). | ||
Error("can't find/convert 'message' field from JSON to string") | ||
break | ||
} | ||
|
||
logMessage := &LogMessage{ | ||
Host: host, | ||
Message: message, | ||
data: data, | ||
} | ||
|
||
messages = append(messages, logMessage) | ||
|
||
if errReader == io.EOF { | ||
break | ||
} | ||
} | ||
|
||
return messages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kataras/iris" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type RootConsumer struct { | ||
RouteFieldName string | ||
consumers map[string]*Consumer // route name to consumer map | ||
|
||
BaseConsumer | ||
} | ||
|
||
func (c *RootConsumer) Handle(ctx iris.Context) { | ||
for _, message := range c.parseJSONs(ctx.Request().Body) { | ||
var route string | ||
|
||
if value, ok := message.data[c.RouteFieldName].(string); ok { | ||
route = value | ||
} else { | ||
log.WithField("field_name", c.RouteFieldName). | ||
WithField("value", message.data[c.RouteFieldName]). | ||
Error("can't find/convert 'route' field from JSON to string") | ||
continue | ||
} | ||
|
||
if consumer, found := c.consumers[route]; found { | ||
log.WithField("message", message).WithField("route", route).Debug("consumer: sending message to queue of route") | ||
consumer.queue <- message | ||
} else { | ||
log.WithField("message", message).WithField("route", route).Error("consumer: can't found consumer for route") | ||
} | ||
} | ||
} | ||
|
||
func (c *RootConsumer) AddConsumer(route string, consumer *Consumer) { | ||
c.consumers[route] = consumer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/kataras/iris/context" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type LogMessage struct { | ||
Host string | ||
Message string | ||
} | ||
|
||
type Consumer struct { | ||
HostFieldName string // name of "host" field in json log message | ||
MessageFieldName string // name of "message" field in json log message | ||
queue chan *LogMessage | ||
queue chan *LogMessage | ||
|
||
BaseConsumer | ||
} | ||
|
||
func (c *Consumer) Handle(ctx context.Context) { | ||
reader := bufio.NewReader(ctx.Request().Body) | ||
for { | ||
bytes, errReader := reader.ReadBytes('\n') | ||
|
||
if errReader != nil && errReader != io.EOF { | ||
log.WithError(errReader).Error("can't read line from POST body") | ||
return | ||
} | ||
|
||
var data map[string]interface{} | ||
|
||
if err := json.Unmarshal(bytes, &data); err != nil { | ||
log.WithError(err).WithField("line", string(bytes)).Error("can't parse line from POST body as JSON") | ||
return | ||
} | ||
|
||
var host, message string | ||
|
||
if value, ok := data[c.HostFieldName].(string); ok { | ||
host = value | ||
} else { | ||
log.WithField("field_name", c.HostFieldName). | ||
WithField("value", data[c.HostFieldName]). | ||
Error("can't find/convert 'host' field from JSON to string") | ||
return | ||
} | ||
|
||
if value, ok := data[c.MessageFieldName].(string); ok { | ||
message = value | ||
} else { | ||
log.WithField("field_name", c.MessageFieldName). | ||
WithField("value", data[c.MessageFieldName]). | ||
Error("can't find/convert 'message' field from JSON to string") | ||
return | ||
} | ||
|
||
logMessage := &LogMessage{ | ||
Host: host, | ||
Message: message, | ||
} | ||
|
||
for _, message := range c.parseJSONs(ctx.Request().Body) { | ||
log.WithField("message", message).Debug("consumer: sending message to queue") | ||
c.queue <- logMessage | ||
|
||
if errReader == io.EOF { | ||
return | ||
} | ||
c.queue <- message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters