Skip to content

Commit

Permalink
Add docker development environment
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflint committed Sep 18, 2018
1 parent c8b22b7 commit 44dc7b7
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 88 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_PROJECT_NAME=logvac
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ logvac.json
*.cover*
db
vendor/*/

*.secret
dist
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.10

RUN apt-get update && \
apt-get install -y \
# build tools, for compiling
build-essential \
# install curl to fetch dev things
curl \
# we'll need git for fetching golang deps
git \
# we need aws-cli to publish
awscli

# install dep (not using it yet, but probably will switch to it)
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

# setup the app dir/working directory
RUN mkdir -p /go/src/github.com/nanopack/logvac
WORKDIR /go/src/github.com/nanopack/logvac
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: build deps #release run test

# directory to output build
DIST_DIR=./dist
# get the date and time to use as a buildstamp
DATE=$$(TZ=":US/Mountain" date '+%Y-%m-%d')
TIME=$$(TZ=":US/Mountain" date '+%I:%M:%S%p')
LDFLAGS="-s -w -X main.buildDate=$(DATE) -X main.buildTime=$(TIME)"

build:
@go build --ldflags=$(LDFLAGS) -o $(DIST_DIR)/logvac main.go

deps:
@go get -t -v ./...
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.1'

services:
dev:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/go/src/github.com/nanopack/logvac
env_file:
- '.env'
- '.env.secret'
144 changes: 72 additions & 72 deletions drain/datadog.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
package drain

import (
"fmt"
"net"
"time"

"github.com/DataDog/datadog-agent/pkg/logs/sender"

"github.com/nanopack/logvac/core"
)

// Datadog drain implements the publisher interface for publishing logs to datadog.
type Datadog struct {
connManager *sender.ConnectionManager
Conn net.Conn
Key string // datadog api key
}

// NewDatadogClient creates a new mist publisher
func NewDatadogClient(key string) (*Datadog, error) {
_, err := net.ResolveTCPAddr("tcp", "intake.logs.datadoghq.com:10514")
if err != nil {
return nil, fmt.Errorf("Failed to resolve datadog address - %s", err.Error())
}

cm := sender.NewConnectionManager("intake.logs.datadoghq.com", 10514, true)
conn := cm.NewConnection()

return &Datadog{Key: key, connManager: cm, Conn: conn}, nil
}

// Init initializes a connection to mist
func (p Datadog) Init() error {

// add drain
logvac.AddDrain("datadog", p.Publish)

return nil
}

// Publish utilizes mist's Publish to "drain" a log message
func (p *Datadog) Publish(msg logvac.Message) {
msg.PubTries++

if p.Conn == nil {
fmt.Println("Redialing datadog")
p.Conn = p.connManager.NewConnection() // doesn't block (don't goroutine call to Publish)
}

var ms []byte
if len(msg.Raw) > 4 {
ms = append(append([]byte(p.Key+" "), msg.Raw[4:]...), []byte("\n")...)
} else {
ms = append(append([]byte(p.Key+" "), msg.Raw...), []byte("\n")...)
}

_, err := p.Conn.Write(ms)
if err != nil {
fmt.Printf("Failed writing log - %s %d\n", err.Error(), msg.PubTries)
p.connManager.CloseConnection(p.Conn)
p.Conn = nil
if msg.PubTries <= 3 {
time.Sleep(2 * time.Second)
p.Publish(msg)
}
}
}

// Close closes the connection to datadog.
func (p *Datadog) Close() error {
p.connManager.CloseConnection(p.Conn)
return nil
}
// import (
// "fmt"
// "net"
// "time"
//
// "github.com/DataDog/datadog-agent/pkg/logs/sender"
//
// "github.com/nanopack/logvac/core"
// )
//
// // Datadog drain implements the publisher interface for publishing logs to datadog.
// type Datadog struct {
// connManager *sender.ConnectionManager
// Conn net.Conn
// Key string // datadog api key
// }
//
// // NewDatadogClient creates a new mist publisher
// func NewDatadogClient(key string) (*Datadog, error) {
// _, err := net.ResolveTCPAddr("tcp", "intake.logs.datadoghq.com:10514")
// if err != nil {
// return nil, fmt.Errorf("Failed to resolve datadog address - %s", err.Error())
// }
//
// cm := sender.NewConnectionManager("intake.logs.datadoghq.com", 10514, true)
// conn := cm.NewConnection()
//
// return &Datadog{Key: key, connManager: cm, Conn: conn}, nil
// }
//
// // Init initializes a connection to mist
// func (p Datadog) Init() error {
//
// // add drain
// logvac.AddDrain("datadog", p.Publish)
//
// return nil
// }
//
// // Publish utilizes mist's Publish to "drain" a log message
// func (p *Datadog) Publish(msg logvac.Message) {
// msg.PubTries++
//
// if p.Conn == nil {
// fmt.Println("Redialing datadog")
// p.Conn = p.connManager.NewConnection() // doesn't block (don't goroutine call to Publish)
// }
//
// var ms []byte
// if len(msg.Raw) > 4 {
// ms = append(append([]byte(p.Key+" "), msg.Raw[4:]...), []byte("\n")...)
// } else {
// ms = append(append([]byte(p.Key+" "), msg.Raw...), []byte("\n")...)
// }
//
// _, err := p.Conn.Write(ms)
// if err != nil {
// fmt.Printf("Failed writing log - %s %d\n", err.Error(), msg.PubTries)
// p.connManager.CloseConnection(p.Conn)
// p.Conn = nil
// if msg.PubTries <= 3 {
// time.Sleep(2 * time.Second)
// p.Publish(msg)
// }
// }
// }
//
// // Close closes the connection to datadog.
// func (p *Datadog) Close() error {
// p.connManager.CloseConnection(p.Conn)
// return nil
// }
30 changes: 15 additions & 15 deletions drain/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,21 @@ func AddDrain(d logvac.Drain) error {
}
drains["papertrail"] = pTrail
drainCfg["papertrail"] = d
case "datadog":
// if it already exists, close it and create a new one
if _, ok := drains["datadog"]; ok {
drains["datadog"].Close()
}
dDog, err := NewDatadogClient(d.AuthKey)
if err != nil {
return fmt.Errorf("Failed to create datadog client - %s", err)
}
err = dDog.Init()
if err != nil {
return fmt.Errorf("Datadog failed to initialize - %s", err)
}
drains["datadog"] = dDog
drainCfg["datadog"] = d
// case "datadog":
// // if it already exists, close it and create a new one
// if _, ok := drains["datadog"]; ok {
// drains["datadog"].Close()
// }
// dDog, err := NewDatadogClient(d.AuthKey)
// if err != nil {
// return fmt.Errorf("Failed to create datadog client - %s", err)
// }
// err = dDog.Init()
// if err != nil {
// return fmt.Errorf("Datadog failed to initialize - %s", err)
// }
// drains["datadog"] = dDog
// drainCfg["datadog"] = d
default:
return fmt.Errorf("Drain type not supported")
}
Expand Down

0 comments on commit 44dc7b7

Please sign in to comment.