-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to drain to 3rd party services
Add support for papertrail Resolves #18
- Loading branch information
Showing
9 changed files
with
302 additions
and
8 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,83 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/nanopack/logvac/core" | ||
"github.com/nanopack/logvac/drain" | ||
) | ||
|
||
func addDrain(rw http.ResponseWriter, req *http.Request) { | ||
drainer := logvac.Drain{} | ||
|
||
err := parseBody(req, &drainer) | ||
if err != nil { | ||
rw.WriteHeader(400) | ||
rw.Write([]byte(fmt.Sprintf("Failed to parse drain - %s", err.Error()))) | ||
return | ||
} | ||
|
||
err = drain.AddDrain(drainer) | ||
if err != nil { | ||
rw.WriteHeader(400) | ||
rw.Write([]byte(fmt.Sprintf("Failed to add drain - %s", err.Error()))) | ||
return | ||
} | ||
|
||
rw.WriteHeader(200) | ||
rw.Write([]byte("success!\n")) | ||
} | ||
|
||
func updateDrain(rw http.ResponseWriter, req *http.Request) { | ||
drainType := req.URL.Query().Get(":drainType") | ||
|
||
drainer := logvac.Drain{} | ||
|
||
err := parseBody(req, &drainer) | ||
if err != nil { | ||
rw.WriteHeader(400) | ||
rw.Write([]byte(fmt.Sprintf("Failed to parse drain - %s", err.Error()))) | ||
return | ||
} | ||
|
||
if drainer.Type != drainType { | ||
err = drain.RemoveDrain(drainType) | ||
if err != nil { | ||
rw.WriteHeader(400) | ||
rw.Write([]byte(fmt.Sprintf("Failed to remove drain - %s", err.Error()))) | ||
return | ||
} | ||
} | ||
|
||
err = drain.AddDrain(drainer) | ||
if err != nil { | ||
rw.WriteHeader(400) | ||
rw.Write([]byte(fmt.Sprintf("Failed to add drain - %s", err.Error()))) | ||
return | ||
} | ||
|
||
rw.WriteHeader(200) | ||
rw.Write([]byte("success!\n")) | ||
} | ||
|
||
func deleteDrain(rw http.ResponseWriter, req *http.Request) { | ||
drainType := req.URL.Query().Get(":drainType") | ||
|
||
err := drain.RemoveDrain(drainType) | ||
if err != nil { | ||
rw.WriteHeader(400) | ||
rw.Write([]byte(fmt.Sprintf("Failed to remove drain - %s", err.Error()))) | ||
return | ||
} | ||
|
||
rw.WriteHeader(200) | ||
rw.Write([]byte("success!\n")) | ||
} | ||
|
||
func listDrains(rw http.ResponseWriter, req *http.Request) { | ||
drains := drain.ListDrains() | ||
|
||
rw.WriteHeader(200) | ||
rw.Write([]byte(fmt.Sprintf("%q\n", drains))) | ||
} |
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
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
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,51 @@ | ||
package drain | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
|
||
"github.com/nanopack/logvac/core" | ||
) | ||
|
||
// Papertrail drain implements the publisher interface for publishing logs to papertrail. | ||
type Papertrail struct { | ||
Conn io.WriteCloser | ||
} | ||
|
||
// NewPapertrailClient creates a new mist publisher | ||
func NewPapertrailClient(uri string) (*Papertrail, error) { | ||
addr, err := net.ResolveUDPAddr("udp", uri) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to resolve papertrail address - %s", err.Error()) | ||
} | ||
|
||
Conn, err := net.DialUDP("udp", nil, addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to dial papertrail - %s", err.Error()) | ||
} | ||
|
||
return &Papertrail{Conn}, nil | ||
} | ||
|
||
// Init initializes a connection to mist | ||
func (p Papertrail) Init() error { | ||
|
||
// add drain | ||
logvac.AddDrain("papertrail", p.Publish) | ||
|
||
return nil | ||
} | ||
|
||
// Publish utilizes mist's Publish to "drain" a log message | ||
func (p Papertrail) Publish(msg logvac.Message) { | ||
p.Conn.Write(msg.Raw) | ||
} | ||
|
||
// Close closes the connection to papertrail. | ||
func (p *Papertrail) Close() error { | ||
if p.Conn == nil { | ||
return nil | ||
} | ||
return p.Conn.Close() | ||
} |
Oops, something went wrong.