Skip to content
Permalink
Browse files

Instrument backendA

  • Loading branch information...
pboyd committed Aug 30, 2019
1 parent f0c2c22 commit 4eb87ce2530f8f41656b5ca65eb9f2318adc0514
Showing with 197 additions and 74 deletions.
  1. +40 −36 backendA/airport.go
  2. +3 −0 backendA/go.mod
  3. +61 −0 backendA/go.sum
  4. +2 −0 backendA/main.go
  5. +51 −0 backendA/metrics.go
  6. +40 −38 backendA/stats.go
@@ -33,14 +33,15 @@ var airportType = graphql.NewObject(
)

func resolveAirportQuery(db *sql.DB) graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
code, _ := p.Args["code"].(string)
code = strings.ToUpper(code)
if !isAirportCode(code) {
return nil, nil
}
return graphQLMetrics("airport",
func(p graphql.ResolveParams) (interface{}, error) {
code, _ := p.Args["code"].(string)
code = strings.ToUpper(code)
if !isAirportCode(code) {
return nil, nil
}

row := db.QueryRow(`
row := db.QueryRow(`
SELECT
code, name, city, state, lat, lng
FROM
@@ -50,17 +51,18 @@ func resolveAirportQuery(db *sql.DB) graphql.FieldResolveFn {
code=?
`, code)

var a airport
err := row.Scan(&a.Code, &a.Name, &a.City, &a.State, &a.Latitude, &a.Longitude)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
var a airport
err := row.Scan(&a.Code, &a.Name, &a.City, &a.State, &a.Latitude, &a.Longitude)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return nil, err
}

return &a, nil
}
return &a, nil
},
)
}

func isAirportCode(code string) bool {
@@ -78,15 +80,16 @@ func isAirportCode(code string) bool {
}

func resolveAirportList(db *sql.DB) graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
term, _ := p.Args["term"].(string)
if !checkAirportSearchTerm(term) {
return nil, nil
}
return graphQLMetrics("airport_list",
func(p graphql.ResolveParams) (interface{}, error) {
term, _ := p.Args["term"].(string)
if !checkAirportSearchTerm(term) {
return nil, nil
}

termLike := fmt.Sprintf("%%%s%%", term)
termLike := fmt.Sprintf("%%%s%%", term)

rows, err := db.Query(`
rows, err := db.Query(`
SELECT
code, name, city, state, lat, lng
FROM
@@ -98,24 +101,25 @@ func resolveAirportList(db *sql.DB) graphql.FieldResolveFn {
code LIKE ?
)
`, termLike, termLike, termLike)
if err != nil {
return nil, err
}
defer rows.Close()

results := []*airport{}
for rows.Next() {
var a airport
err := rows.Scan(&a.Code, &a.Name, &a.City, &a.State, &a.Latitude, &a.Longitude)
if err != nil {
return nil, err
}
defer rows.Close()

results = append(results, &a)
}
results := []*airport{}
for rows.Next() {
var a airport
err := rows.Scan(&a.Code, &a.Name, &a.City, &a.State, &a.Latitude, &a.Longitude)
if err != nil {
return nil, err
}

return results, nil
}
results = append(results, &a)
}

return results, nil
},
)
}

func checkAirportSearchTerm(term string) bool {
@@ -3,7 +3,10 @@ module github.com/pboyd/flightranker-backend/backendA
require (
github.com/go-sql-driver/mysql v1.4.1
github.com/graphql-go/graphql v0.7.8
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pboyd/flightranker-backend/backendtest v0.0.0
github.com/prometheus/client_golang v1.1.0
google.golang.org/appengine v1.6.1 // indirect
)

@@ -1,24 +1,85 @@
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/graphql-go/graphql v0.7.8 h1:769CR/2JNAhLG9+aa8pfLkKdR0H+r5lsQqling5WwpU=
github.com/graphql-go/graphql v0.7.8/go.mod h1:k6yrAYQaSP59DC5UVxbgxESlmVyojThKdORUqGDGmrI=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.1.0 h1:BQ53HtBmfOitExawJ6LokA4x8ov/z0SYYb0+HxJfRI8=
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.6.0 h1:kRhiuYSXR3+uv2IbVbZhUxK5zVD/2pp3Gd2PpvPkpEo=
github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.3 h1:CTwfnzjQ+8dS6MhHHu4YswVAD99sL2wjPqP+VkURmKE=
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -9,6 +9,7 @@ import (

"github.com/go-sql-driver/mysql"
"github.com/graphql-go/graphql"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
@@ -18,6 +19,7 @@ func main() {
}

http.Handle("/", graphqlHandler(db))
http.Handle("/metrics", promhttp.Handler())

log.Fatal(http.ListenAndServe(":8080", nil))
}
@@ -0,0 +1,51 @@
package main

import (
"github.com/graphql-go/graphql"
"github.com/prometheus/client_golang/prometheus"
)

func graphQLMetrics(name string, fn graphql.FieldResolveFn) graphql.FieldResolveFn {
requests := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "graphql",
Subsystem: name,
Name: "requests",
})
prometheus.MustRegister(requests)

errors := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "graphql",
Subsystem: name,
Name: "errors",
})
prometheus.MustRegister(errors)

inflight := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "graphql",
Subsystem: name,
Name: "inflight",
})
prometheus.MustRegister(inflight)

responseTime := prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "graphql",
Subsystem: name,
Name: "response_time",
})
prometheus.MustRegister(responseTime)

return func(p graphql.ResolveParams) (r interface{}, err error) {
timer := prometheus.NewTimer(responseTime)
requests.Inc()
inflight.Inc()
defer func() {
timer.ObserveDuration()
inflight.Dec()

if err != nil {
errors.Inc()
}
}()
return fn(p)
}
}
@@ -30,44 +30,46 @@ var airlineStatsType = graphql.NewObject(
)

func resolveFlightStatsByAirline(db *sql.DB) graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
origin, _ := p.Args["origin"].(string)
origin = strings.ToUpper(origin)

dest, _ := p.Args["destination"].(string)
dest = strings.ToUpper(dest)

if !isAirportCode(origin) || !isAirportCode(dest) {
return nil, nil
}

stats, err := airlineFlightInfo(p.Context, db, origin, dest)
if err != nil {
return nil, err
}

delays, err := delaysByAirline(p.Context, db, origin, dest)
if err != nil {
return nil, err
}

for code := range stats {
stats[code].OnTimePercentage = (1.0 - float64(delays[code])/float64(stats[code].TotalFlights)) * 100
}

statsRows := make([]*airlineStats, len(stats))
i := 0
for code := range stats {
statsRows[i] = stats[code]
i++
}

sort.Slice(statsRows, func(i, j int) bool {
return statsRows[j].OnTimePercentage < statsRows[i].OnTimePercentage
})

return statsRows, nil
}
return graphQLMetrics("flightstats_by_airline",
func(p graphql.ResolveParams) (interface{}, error) {
origin, _ := p.Args["origin"].(string)
origin = strings.ToUpper(origin)

dest, _ := p.Args["destination"].(string)
dest = strings.ToUpper(dest)

if !isAirportCode(origin) || !isAirportCode(dest) {
return nil, nil
}

stats, err := airlineFlightInfo(p.Context, db, origin, dest)
if err != nil {
return nil, err
}

delays, err := delaysByAirline(p.Context, db, origin, dest)
if err != nil {
return nil, err
}

for code := range stats {
stats[code].OnTimePercentage = (1.0 - float64(delays[code])/float64(stats[code].TotalFlights)) * 100
}

statsRows := make([]*airlineStats, len(stats))
i := 0
for code := range stats {
statsRows[i] = stats[code]
i++
}

sort.Slice(statsRows, func(i, j int) bool {
return statsRows[j].OnTimePercentage < statsRows[i].OnTimePercentage
})

return statsRows, nil
},
)
}

func airlineFlightInfo(ctx context.Context, db *sql.DB, origin, dest string) (map[string]*airlineStats, error) {

0 comments on commit 4eb87ce

Please sign in to comment.
You can’t perform that action at this time.