mirror of
https://github.com/christianselig/apollo-backend
synced 2024-12-22 22:35:27 +00:00
measure reddit connections
This commit is contained in:
parent
9156b7f07d
commit
b55bb4839b
3 changed files with 36 additions and 4 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/DataDog/datadog-go/statsd"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
|
@ -46,7 +47,16 @@ func main() {
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
rc := reddit.NewClient(os.Getenv("REDDIT_CLIENT_ID"), os.Getenv("REDDIT_CLIENT_SECRET"))
|
statsd, err := statsd.New("127.0.0.1:8125")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rc := reddit.NewClient(
|
||||||
|
os.Getenv("REDDIT_CLIENT_ID"),
|
||||||
|
os.Getenv("REDDIT_CLIENT_SECRET"),
|
||||||
|
statsd,
|
||||||
|
)
|
||||||
|
|
||||||
app := &application{
|
app := &application{
|
||||||
cfg,
|
cfg,
|
||||||
|
|
|
@ -182,8 +182,6 @@ func main() {
|
||||||
logger.Printf("Couldn't find .env so I will read from existing ENV.")
|
logger.Printf("Couldn't find .env so I will read from existing ENV.")
|
||||||
}
|
}
|
||||||
|
|
||||||
rc := reddit.NewClient(os.Getenv("REDDIT_CLIENT_ID"), os.Getenv("REDDIT_CLIENT_SECRET"))
|
|
||||||
|
|
||||||
dburl, ok := os.LookupEnv("DATABASE_CONNECTION_POOL_URL")
|
dburl, ok := os.LookupEnv("DATABASE_CONNECTION_POOL_URL")
|
||||||
if !ok {
|
if !ok {
|
||||||
dburl = os.Getenv("DATABASE_URL")
|
dburl = os.Getenv("DATABASE_URL")
|
||||||
|
@ -204,6 +202,12 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rc := reddit.NewClient(
|
||||||
|
os.Getenv("REDDIT_CLIENT_ID"),
|
||||||
|
os.Getenv("REDDIT_CLIENT_SECRET"),
|
||||||
|
statsd,
|
||||||
|
)
|
||||||
|
|
||||||
// This is a very conservative value -- seen as most of the work that is done in these jobs is
|
// This is a very conservative value -- seen as most of the work that is done in these jobs is
|
||||||
//
|
//
|
||||||
runtime.GOMAXPROCS(workers)
|
runtime.GOMAXPROCS(workers)
|
||||||
|
|
|
@ -4,9 +4,11 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptrace"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/DataDog/datadog-go/statsd"
|
||||||
"github.com/valyala/fastjson"
|
"github.com/valyala/fastjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,14 +20,26 @@ type Client struct {
|
||||||
id string
|
id string
|
||||||
secret string
|
secret string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
tracer *httptrace.ClientTrace
|
||||||
parser *fastjson.Parser
|
parser *fastjson.Parser
|
||||||
|
statsd *statsd.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(id, secret string) *Client {
|
func NewClient(id, secret string, statsd *statsd.Client) *Client {
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
||||||
MaxIdleConnsPerHost: 128,
|
MaxIdleConnsPerHost: 128,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracer := &httptrace.ClientTrace{
|
||||||
|
GotConn: func(info httptrace.GotConnInfo) {
|
||||||
|
if info.Reused {
|
||||||
|
statsd.Incr("reddit.api.connections.reused", []string{}, 0.1)
|
||||||
|
} else {
|
||||||
|
statsd.Incr("reddit.api.connections.created", []string{}, 0.1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
client := &http.Client{Transport: tr}
|
client := &http.Client{Transport: tr}
|
||||||
|
|
||||||
parser := &fastjson.Parser{}
|
parser := &fastjson.Parser{}
|
||||||
|
@ -34,7 +48,9 @@ func NewClient(id, secret string) *Client {
|
||||||
id,
|
id,
|
||||||
secret,
|
secret,
|
||||||
client,
|
client,
|
||||||
|
tracer,
|
||||||
parser,
|
parser,
|
||||||
|
statsd,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +72,8 @@ func (rac *AuthenticatedClient) request(r *Request) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req = req.WithContext(httptrace.WithClientTrace(req.Context(), rac.tracer))
|
||||||
|
|
||||||
resp, err := rac.client.Do(req)
|
resp, err := rac.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue