Skip to content

Commit

Permalink
Add ability to specify multiple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton committed Jun 10, 2017
1 parent 27e49a8 commit 0073708
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 13 deletions.
3 changes: 2 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ func verify(fn http.HandlerFunc) http.HandlerFunc {
// note: javascript number precision may cause unexpected results (missing logs within 100 nanosecond window)
func GenerateArchiveEndpoint(archive drain.ArchiverDrain) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
// /logs?id=&type=app&start=0&end=0&limit=50
query := req.URL.Query()

host := query.Get("id")
tag := query.Get("tag")
tag := query["tag"]

kind := query.Get("type")
if kind == "" {
Expand Down
3 changes: 3 additions & 0 deletions boxfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deploy.config:
- go get github.com/nanopack/mist
- cp $(which mist) $APP_DIR/mist
- cp $CODE_DIR/config.json $APP_DIR/config.json
- cp $CODE_DIR/example-narc.conf $APP_DIR/narc.conf

data.storage:
image: nanobox/unfs
Expand All @@ -31,3 +32,5 @@ web.logvac:
start:
mist: './mist --server --listeners "tcp://0.0.0.0:1445"'
logvac: './logvac -c config.json'
debug-mist: './mist subscribe --tags log' # for debugging, send logs to `/var/log/gonano/logvac/current` to view in `deploy dry-run` output
debug-narc: 'narcd narc.conf' # for debugging, send logs to `/var/log/gonano/logvac/current` to view in `deploy dry-run` output
2 changes: 1 addition & 1 deletion collector/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GenerateHttpCollector() http.HandlerFunc {
// keep body as "message" and make up priority
msg.Content = string(body)
msg.Priority = 2
msg.Tag = "http-raw"
msg.Tag = []string{"http-raw"}
}

if msg.Type == "" {
Expand Down
7 changes: 5 additions & 2 deletions collector/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ func parseMessage(b []byte) (msg logvac.Message) {
// config.Log.Trace("Parsed data: %s", parsedData)
msg.Time = time.Now()
msg.UTime = msg.Time.UnixNano()
msg.Id = parsedData["hostname"].(string)
msg.Tag = parsedData["tag"].(string)
// if setting multiple tags in id, set hostname first
tTag := strings.Split(parsedData["hostname"].(string), ",")
msg.Id = tTag[0]
// combine all id's (split on ',') and add as tags
msg.Tag = append([]string{parsedData["tag"].(string)}, tTag...)
msg.Priority = adjust[parsedData["severity"].(int)] // parser guarantees [0,7]
msg.Content = parsedData["content"].(string)
return
Expand Down
6 changes: 3 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type (
Message struct {
Time time.Time `json:"time"`
UTime int64 `json:"utime"`
Id string `json:"id"` // ignoreifempty?
Tag string `json:"tag"` // ignoreifempty? // []string?
Type string `json:"type"`
Id string `json:"id"` // ignoreifempty? // If setting multiple tags in id (syslog), set hostname first
Tag []string `json:"tag"` // ignoreifempty?
Type string `json:"type"` // Can be set if logs are submitted via http (deploy logs)
Priority int `json:"priority"`
Content string `json:"message"`
}
Expand Down
21 changes: 17 additions & 4 deletions drain/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (a BoltArchive) Close() {
}

// Slice returns a slice of logs based on the name, offset, limit, and log-level
func (a BoltArchive) Slice(name, host, tag string, offset, end, limit int64, level int) ([]logvac.Message, error) {
func (a BoltArchive) Slice(name, host string, tag []string, offset, end, limit int64, level int) ([]logvac.Message, error) {
var messages []logvac.Message

err := a.db.View(func(tx *bolt.Tx) error {
Expand Down Expand Up @@ -113,12 +113,24 @@ func (a BoltArchive) Slice(name, host, tag string, offset, end, limit int64, lev
limit = 0
}
if msg.Priority >= level {
if msg.Id == host || host == "" {
// todo: negate here if tag[0] == "!"
if msg.Tag == tag || tag == "" {
if host == "" || msg.Id == host {
// todo: negate here if tag starts with "!"
if len(tag) == 0 {
limit--
// prepend messages with new message (display newest last)
messages = append([]logvac.Message{msg}, messages...)
} else {
for x := range msg.Tag {
for y := range tag {
if msg.Tag[x] == tag[y] {
limit--
// prepend messages with new message (display newest last)
messages = append([]logvac.Message{msg}, messages...)

return nil
}
}
}
}
}
}
Expand Down Expand Up @@ -244,6 +256,7 @@ func (a BoltArchive) Expire() {
// loop through and remove outdated logs
for k, v := c.First(); k != nil; k, v = c.Next() {
var logMessage logvac.Message
// todo: seems expensive...
err := json.Unmarshal([]byte(v), &logMessage)
if err != nil {
config.Log.Fatal("Bad JSON syntax in log message - %s", err)
Expand Down
2 changes: 1 addition & 1 deletion drain/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type (
// Init initializes the archiver drain
Init() error
// Slice returns a slice of logs based on the name, offset, limit, and log-level
Slice(name, host, tag string, offset, end, limit int64, level int) ([]logvac.Message, error)
Slice(name, host string, tag []string, offset, end, limit int64, level int) ([]logvac.Message, error)
// Write writes the message to database
Write(msg logvac.Message)
// Expire cleans up old logs
Expand Down
3 changes: 2 additions & 1 deletion drain/mist.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func (m Mist) Init() error {

// Publish utilizes mist's Publish to "drain" a log message
func (m *Mist) Publish(msg logvac.Message) {
tags := []string{"log", msg.Type, msg.Id, msg.Tag}
tags := []string{"log", msg.Type, msg.Id}
tags = append(tags, msg.Tag...)
// remove blank tags
cleanTags:
for i := range tags {
Expand Down
53 changes: 53 additions & 0 deletions example-narc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- mode: apacheconf; -*-
###########
# general #
###########

daemonize no
pidfile /var/run/narc.pid

###########
# logging #
###########

loglevel notice
#logfile /var/log/narc.log
syslog-enabled no
syslog-ident narc
syslog-facility local0

##########
# server #
##########

# remote syslog
remote-host 127.0.0.1
remote-port 6361
remote-proto udp

# max server connect attempts
max-connect-attempts 12
# millisecond delay between attempts
connect-retry-delay 5000

###########
# streams #
###########

# max file open attempts
max-open-attempts 12
# millisecond delay between attempts
open-retry-delay 5000

# identifier to prefix all messages with
stream-id web.logvac,12345678900asdfghjkl

# syslog facility for streams
stream-facility user

# syslog priority for streams
stream-priority error

# log streams
stream logvac[daemon] /var/log/gonano/logvac/current
stream mist[daemon] /var/log/gonano/mist/current

0 comments on commit 0073708

Please sign in to comment.