Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailchimp report plugin #408

Merged
merged 2 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
- [#410](https://github.com/influxdb/telegraf/pull/410): Additional redis metrics. Thanks @vlaadbrain!
- [#414](https://github.com/influxdb/telegraf/issues/414): Jolokia plugin auth parameters
- [#415](https://github.com/influxdb/telegraf/issues/415): memcached plugin: support unix sockets
- [#418](https://github.com/influxdb/telegraf/pull/418): memcached plugin additional unit tests.
- [#408](https://github.com/influxdb/telegraf/pull/408): MailChimp plugin.

### Bugfixes
- [#405](https://github.com/influxdb/telegraf/issues/405): Prometheus output cardinality issue
- [#388](https://github.com/influxdb/telegraf/issues/388): Fix collection hangup when cpu times decrement.

## v0.2.3 [2015-11-30]

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ docker-kill:
-docker rm nsq aerospike redis opentsdb rabbitmq postgres memcached mysql kafka mqtt riemann

# Run full unit tests using docker containers (includes setup and teardown)
test: docker-kill prepare docker-run
test: docker-kill docker-run
# Sleeping for kafka leadership election, TSDB setup, etc.
sleep 60
# SUCCESS, running tests
go test -race ./...

# Run "short" unit tests
test-short: prepare
test-short:
go test -short ./...

.PHONY: test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Telegraf currently has support for collecting metrics from:
* jolokia (remote JMX with JSON over HTTP)
* leofs
* lustre2
* mailchimp
* memcached
* mongodb
* mysql
Expand Down
4 changes: 4 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
}(plugin)
}

if counter == 0 {
return nil
}

wg.Wait()

elapsed := time.Since(start)
Expand Down
1 change: 1 addition & 0 deletions plugins/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "github.com/influxdb/telegraf/plugins/kafka_consumer"
_ "github.com/influxdb/telegraf/plugins/leofs"
_ "github.com/influxdb/telegraf/plugins/lustre2"
_ "github.com/influxdb/telegraf/plugins/mailchimp"
_ "github.com/influxdb/telegraf/plugins/memcached"
_ "github.com/influxdb/telegraf/plugins/mongodb"
_ "github.com/influxdb/telegraf/plugins/mysql"
Expand Down
2 changes: 1 addition & 1 deletion plugins/apache/apache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ConnsAsyncClosing: 205
Scoreboard: WW_____W_RW_R_W__RRR____WR_W___WW________W_WW_W_____R__R_WR__WRWR_RRRW___R_RWW__WWWRW__R_RW___RR_RW_R__W__WR_WWW______WWR__R___R_WR_W___RW______RR________________W______R__RR______W________________R____R__________________________RW_W____R_____W_R_________________R____RR__W___R_R____RW______R____W______W_W_R_R______R__R_R__________R____W_______WW____W____RR__W_____W_R_______W__________W___W____________W_______WRR_R_W____W_____R____W_WW_R____RRW__W............................................................................................................................................................................................................................................................................................................WRRWR____WR__RR_R___RWR_________W_R____RWRRR____R_R__RW_R___WWW_RW__WR_RRR____W___R____WW_R__R___RR_W_W_RRRRWR__RRWR__RRW_W_RRRW_R_RR_W__RR_RWRR_R__R___RR_RR______R__RR____R_____W_R_R_R__R__R__________W____WW_R___R_R___R_________RR__RR____RWWWW___W_R________R_R____R_W___W___R___W_WRRWW_______R__W_RW_______R________RR__R________W_______________________W_W______________RW_________WR__R___R__R_______________WR_R_________W___RW_____R____________W____......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
`

func TestHTTPInflux(t *testing.T) {
func TestHTTPApache(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, apacheStatus)
Expand Down
234 changes: 234 additions & 0 deletions plugins/mailchimp/chimp_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package mailchimp

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"sync"
)

const (
reports_endpoint string = "/3.0/reports"
reports_endpoint_campaign string = "/3.0/reports/%s"
)

var mailchimp_datacenter = regexp.MustCompile("[a-z]+[0-9]+$")

type ChimpAPI struct {
Transport http.RoundTripper
Debug bool

sync.Mutex

url *url.URL
}

type ReportsParams struct {
Count string
Offset string
SinceSendTime string
BeforeSendTime string
}

func (p *ReportsParams) String() string {
v := url.Values{}
if p.Count != "" {
v.Set("count", p.Count)
}
if p.Offset != "" {
v.Set("offset", p.Offset)
}
if p.BeforeSendTime != "" {
v.Set("before_send_time", p.BeforeSendTime)
}
if p.SinceSendTime != "" {
v.Set("since_send_time", p.SinceSendTime)
}
return v.Encode()
}

func NewChimpAPI(apiKey string) *ChimpAPI {
u := &url.URL{}
u.Scheme = "https"
u.Host = fmt.Sprintf("%s.api.mailchimp.com", mailchimp_datacenter.FindString(apiKey))
u.User = url.UserPassword("", apiKey)
return &ChimpAPI{url: u}
}

type APIError struct {
Status int `json:"status"`
Type string `json:"type"`
Title string `json:"title"`
Detail string `json:"detail"`
Instance string `json:"instance"`
}

func (e APIError) Error() string {
return fmt.Sprintf("ERROR %v: %v. See %v", e.Status, e.Title, e.Type)
}

func chimpErrorCheck(body []byte) error {
var e APIError
json.Unmarshal(body, &e)
if e.Title != "" || e.Status != 0 {
return e
}
return nil
}

func (a *ChimpAPI) GetReports(params ReportsParams) (ReportsResponse, error) {
a.Lock()
defer a.Unlock()
a.url.Path = reports_endpoint

var response ReportsResponse
rawjson, err := runChimp(a, params)
if err != nil {
return response, err
}

err = json.Unmarshal(rawjson, &response)
if err != nil {
return response, err
}

return response, nil
}

func (a *ChimpAPI) GetReport(campaignID string) (Report, error) {
a.Lock()
defer a.Unlock()
a.url.Path = fmt.Sprintf(reports_endpoint_campaign, campaignID)

var response Report
rawjson, err := runChimp(a, ReportsParams{})
if err != nil {
return response, err
}

err = json.Unmarshal(rawjson, &response)
if err != nil {
return response, err
}

return response, nil
}

func runChimp(api *ChimpAPI, params ReportsParams) ([]byte, error) {
client := &http.Client{Transport: api.Transport}

var b bytes.Buffer
req, err := http.NewRequest("GET", api.url.String(), &b)
if err != nil {
return nil, err
}
req.URL.RawQuery = params.String()
req.Header.Set("User-Agent", "Telegraf-MailChimp-Plugin")
if api.Debug {
log.Printf("Request URL: %s", req.URL.String())
}

resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if api.Debug {
log.Printf("Response Body:%s", string(body))
}

if err = chimpErrorCheck(body); err != nil {
return nil, err
}
return body, nil
}

type ReportsResponse struct {
Reports []Report `json:"reports"`
TotalItems int `json:"total_items"`
}

type Report struct {
ID string `json:"id"`
CampaignTitle string `json:"campaign_title"`
Type string `json:"type"`
EmailsSent int `json:"emails_sent"`
AbuseReports int `json:"abuse_reports"`
Unsubscribed int `json:"unsubscribed"`
SendTime string `json:"send_time"`

TimeSeries []TimeSerie
Bounces Bounces `json:"bounces"`
Forwards Forwards `json:"forwards"`
Opens Opens `json:"opens"`
Clicks Clicks `json:"clicks"`
FacebookLikes FacebookLikes `json:"facebook_likes"`
IndustryStats IndustryStats `json:"industry_stats"`
ListStats ListStats `json:"list_stats"`
}

type Bounces struct {
HardBounces int `json:"hard_bounces"`
SoftBounces int `json:"soft_bounces"`
SyntaxErrors int `json:"syntax_errors"`
}

type Forwards struct {
ForwardsCount int `json:"forwards_count"`
ForwardsOpens int `json:"forwards_opens"`
}

type Opens struct {
OpensTotal int `json:"opens_total"`
UniqueOpens int `json:"unique_opens"`
OpenRate float64 `json:"open_rate"`
LastOpen string `json:"last_open"`
}

type Clicks struct {
ClicksTotal int `json:"clicks_total"`
UniqueClicks int `json:"unique_clicks"`
UniqueSubscriberClicks int `json:"unique_subscriber_clicks"`
ClickRate float64 `json:"click_rate"`
LastClick string `json:"last_click"`
}

type FacebookLikes struct {
RecipientLikes int `json:"recipient_likes"`
UniqueLikes int `json:"unique_likes"`
FacebookLikes int `json:"facebook_likes"`
}

type IndustryStats struct {
Type string `json:"type"`
OpenRate float64 `json:"open_rate"`
ClickRate float64 `json:"click_rate"`
BounceRate float64 `json:"bounce_rate"`
UnopenRate float64 `json:"unopen_rate"`
UnsubRate float64 `json:"unsub_rate"`
AbuseRate float64 `json:"abuse_rate"`
}

type ListStats struct {
SubRate float64 `json:"sub_rate"`
UnsubRate float64 `json:"unsub_rate"`
OpenRate float64 `json:"open_rate"`
ClickRate float64 `json:"click_rate"`
}

type TimeSerie struct {
TimeStamp string `json:"timestamp"`
EmailsSent int `json:"emails_sent"`
UniqueOpens int `json:"unique_opens"`
RecipientsClick int `json:"recipients_click"`
}
Loading