Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Write simple metrics and events to Datadog
Browse files Browse the repository at this point in the history
  • Loading branch information
scoiatael committed Mar 6, 2017
1 parent 6da8734 commit 1b709d6
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 9 deletions.
126 changes: 125 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions actions/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions
import (
"github.com/scoiatael/archai/http"
"github.com/scoiatael/archai/persistence"
"github.com/scoiatael/archai/telemetry"
)

type HttpHandler interface {
Expand All @@ -17,6 +18,7 @@ type Context interface {
Version() string
HandleErr(error)
HttpHandler() HttpHandler
Telemetry() telemetry.Datadog
}

type Action interface {
Expand Down
2 changes: 2 additions & 0 deletions actions/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (hs HttpServer) Run(c Context) error {
events[i]["blob"] = payload
}
root["results"] = events
c.Telemetry().Incr("read", []string{"stream:" + stream})
ctx.SendJson(root)
}
})
Expand All @@ -64,6 +65,7 @@ func (hs HttpServer) Run(c Context) error {
c.HandleErr(err)
ctx.ServerErr(err)
} else {
c.Telemetry().Incr("write", []string{"stream:" + stream})
ctx.SendJson("OK")
}
})
Expand Down
1 change: 1 addition & 0 deletions actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var _ = BeforeSuite(func() {
config = Config{}
config.Hosts = []string{"127.0.0.1"}
config.Keyspace = testingKeyspace
config.StatsdAddr = "dd-agent.service.consul:8125"
err := config.Init()
if err != nil {
panic(err)
Expand Down
27 changes: 22 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ import (
"github.com/scoiatael/archai/actions"
"github.com/scoiatael/archai/http"
"github.com/scoiatael/archai/persistence"
"github.com/scoiatael/archai/telemetry"
)

// Config is a context for all application actions.
type Config struct {
Keyspace string
Hosts []string
Actions []actions.Action
Keyspace string
Hosts []string
Actions []actions.Action
StatsdAddr string
Features map[string]bool

provider persistence.Provider
telemetry telemetry.Datadog
initialized bool
}

func (c Config) HandleErr(err error) {
log.Print(err)
c.Telemetry().Failure("server_error", err.Error())
}

func (c Config) Migrations() map[string]persistence.Migration {
Expand All @@ -30,18 +36,25 @@ func (c Config) Migrations() map[string]persistence.Migration {

func (c Config) Persistence() persistence.Provider {
if !c.initialized {
panic(fmt.Errorf("Persistence not initialized!"))
panic(fmt.Errorf("Config not initialized!"))
}
return c.provider
}

func (c Config) Telemetry() telemetry.Datadog {
if !c.initialized {
panic(fmt.Errorf("Config not initialized!"))
}
return c.telemetry
}

// Version returns current version
func (c Config) Version() string {
return Version
}

func (c Config) HttpHandler() actions.HttpHandler {
return http.NewIris(c)
return http.NewIris(c, c.Features["dev_logger"])
}

func (c *Config) Init() error {
Expand All @@ -51,6 +64,10 @@ func (c *Config) Init() error {
return err
}
c.provider = &new_provider

dd := telemetry.NewDatadog(c.StatsdAddr, c.Keyspace)
c.telemetry = dd

c.initialized = true
return nil
}
Expand Down
10 changes: 7 additions & 3 deletions http/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import (
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
)

func NewIris(c Context) *IrisHandler {
func NewIris(c Context, useDevLogger bool) *IrisHandler {
handler := IrisHandler{}
handler.context = c

app := iris.New()
if useDevLogger {
app.Adapt(
// adapt a logger which prints all errors to the os.Stdout
iris.DevLogger(),
)
}
app.Adapt(
// adapt a logger which prints all errors to the os.Stdout
iris.DevLogger(),
// adapt the adaptors/httprouter or adaptors/gorillamux
httprouter.New(),
)
Expand Down
53 changes: 53 additions & 0 deletions telemetry/datadog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package telemetry

import (
"log"

"github.com/DataDog/datadog-go/statsd"
"github.com/pkg/errors"
)

type Datadog interface {
Incr(name string, tags []string)
Failure(title, text string)
}

type Client struct {
client *statsd.Client
initialized bool
}

func (c *Client) on_error(err error) {
log.Println(err)
}

func (c *Client) Failure(title, text string) {
if c.initialized {
ev := statsd.NewEvent(title, text)
ev.AlertType = statsd.Error
err := errors.Wrap(c.client.Event(ev), "Failed sending event to DataDog")
if err != nil {
c.on_error(err)
}
}
}

func (c *Client) Incr(name string, tags []string) {
if c.initialized {
err := c.client.Incr(name, tags, 1.0)
if err != nil {
c.on_error(err)
}
}
}

func NewDatadog(addr string, namespace string) Datadog {
c, err := statsd.New(addr)
if err != nil {
client := &Client{}
client.on_error(err)
return client
}
c.Namespace = namespace + "."
return &Client{client: c, initialized: true}
}

0 comments on commit 1b709d6

Please sign in to comment.