Skip to content

Commit

Permalink
Merge e1183d7 into 5409c4b
Browse files Browse the repository at this point in the history
  • Loading branch information
threez committed Mar 4, 2020
2 parents 5409c4b + e1183d7 commit e5fa2bb
Show file tree
Hide file tree
Showing 53 changed files with 17,559 additions and 9,865 deletions.
11 changes: 2 additions & 9 deletions .travis.yml
@@ -1,20 +1,13 @@
language: go

before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover

after_success:
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci
- go run ./vendor/github.com/mattn/goveralls -coverprofile=coverage.out -service=travis-ci

services:
- docker

env:
- GO111MODULE=on

go:
- 1.13.x
- 1.14.x

script:
- docker-compose run testserver make ci
4 changes: 2 additions & 2 deletions backend/objstore/objstore.go
Expand Up @@ -37,9 +37,9 @@ func init() {
if err != nil {
log.Fatalf("Failed to create object storage client: %v", err)
}
servicehealthcheck.RegisterHealthCheck(&HealthCheck{
servicehealthcheck.RegisterHealthCheck("objstore", &HealthCheck{
Client: client,
}, "objstore")
})

ok, err := client.BucketExists(cfg.HealthCheckBucketName)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions backend/postgres/health_postgres.go
Expand Up @@ -19,12 +19,12 @@ type HealthCheck struct {
}

type postgresQueryExecutor interface {
Exec(query interface{}, params ...interface{}) (res orm.Result, err error)
Exec(ctx context.Context, query interface{}, params ...interface{}) (res orm.Result, err error)
}

// Init initialises the test table
func (h *HealthCheck) Init() error {
_, errWrite := h.Pool.Exec(`CREATE TABLE IF NOT EXISTS ` + cfg.HealthCheckTableName + `(ok boolean);`)
// Init initializes the test table
func (h *HealthCheck) Init(ctx context.Context) error {
_, errWrite := h.Pool.Exec(ctx, `CREATE TABLE IF NOT EXISTS `+cfg.HealthCheckTableName+`(ok boolean);`)
return errWrite
}

Expand All @@ -37,19 +37,19 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
}

// Readcheck
if _, err := h.Pool.Exec(`SELECT 1;`); err != nil {
if _, err := h.Pool.Exec(ctx, `SELECT 1;`); err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
}
// writecheck - add Data to configured Table
_, err := h.Pool.Exec("INSERT INTO " + cfg.HealthCheckTableName + "(ok) VALUES (true);")
_, err := h.Pool.Exec(ctx, "INSERT INTO "+cfg.HealthCheckTableName+"(ok) VALUES (true);")
if err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
}
// and while we're at it, check delete as well (so as not to clutter the database
// because UPSERT is impractical here
_, err = h.Pool.Exec("DELETE FROM " + cfg.HealthCheckTableName + ";")
_, err = h.Pool.Exec(ctx, "DELETE FROM "+cfg.HealthCheckTableName+";")
if err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
Expand All @@ -61,7 +61,7 @@ func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.Health
}

// CleanUp drops the test table.
func (h *HealthCheck) CleanUp() error {
_, err := h.Pool.Exec("DROP TABLE IF EXISTS " + cfg.HealthCheckTableName)
func (h *HealthCheck) CleanUp(ctx context.Context) error {
_, err := h.Pool.Exec(ctx, "DROP TABLE IF EXISTS "+cfg.HealthCheckTableName)
return err
}
2 changes: 1 addition & 1 deletion backend/postgres/health_postgres_test.go
Expand Up @@ -52,7 +52,7 @@ type testPool struct {
err error
}

func (t *testPool) Exec(query interface{}, params ...interface{}) (res orm.Result, err error) {
func (t *testPool) Exec(ctx context.Context, query interface{}, params ...interface{}) (res orm.Result, err error) {
return nil, t.err
}

Expand Down
6 changes: 3 additions & 3 deletions backend/postgres/postgres.go
Expand Up @@ -147,9 +147,9 @@ func init() {
}
}

servicehealthcheck.RegisterHealthCheck(&HealthCheck{
Pool: DefaultConnectionPool(),
}, "postgresdefault")
servicehealthcheck.RegisterHealthCheck("postgresdefault", &HealthCheck{
Pool: &pgPoolAdapter{db: DefaultConnectionPool()},
})
}

var (
Expand Down
20 changes: 20 additions & 0 deletions backend/postgres/query_ctx.go
@@ -0,0 +1,20 @@
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved.
// Created at 2020/02/04 by Vincent Landgraf

package postgres

import (
"context"

"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
)

type pgPoolAdapter struct {
db *pg.DB
}

func (a *pgPoolAdapter) Exec(ctx context.Context, query interface{}, params ...interface{}) (res orm.Result, err error) {
db := a.db.WithContext(ctx)
return db.Exec(query, params...)
}
7 changes: 5 additions & 2 deletions backend/redis/health_redis.go
Expand Up @@ -22,17 +22,20 @@ type HealthCheck struct {
// redis is checked for writeability and readability,
// otherwise return the old result
func (h *HealthCheck) HealthCheck(ctx context.Context) servicehealthcheck.HealthCheckResult {
client := h.Client.WithContext(ctx)

if time.Since(h.state.LastChecked()) <= cfg.HealthCheckResultTTL {
// the last health check is not outdated, an can be reused.
return h.state.GetState()
}

// Try writing
if err := h.Client.Append(cfg.HealthCheckKey, "true").Err(); err != nil {
if err := client.Append(cfg.HealthCheckKey, "true").Err(); err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
}
// If writing worked try reading
err := h.Client.Get(cfg.HealthCheckKey).Err()
err := client.Get(cfg.HealthCheckKey).Err()
if err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
Expand Down
4 changes: 2 additions & 2 deletions backend/redis/redis.go
Expand Up @@ -79,9 +79,9 @@ func init() {
log.Fatalf("Failed to parse redis environment: %v", err)
}

servicehealthcheck.RegisterHealthCheck(&HealthCheck{
servicehealthcheck.RegisterHealthCheck("redis", &HealthCheck{
Client: Client(),
}, "redis")
})
}

// Client with environment based configuration
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Expand Up @@ -55,9 +55,9 @@ services:
- JAEGER_SAMPLER_TYPE=const
- JAEGER_SAMPLER_PARAM=1
- JAEGER_SERVICE_NAME=testserver
- SENTRY_DSN="https://71e5037808ff4acf9e18cd0ab5ee472a:f58b0c9ba48447af953cf377cf1d9b9c@sentry.jamit.de/164"
- SENTRY_DSN=https://35223d89148145ae9d47ba3f268e6ece:1756d00edb714cf1aa07b478627176f3@sentry.jamit.de/193
- SENTRY_ENVIRONMENT=development
- SENTRY_RELEASE=`git rev-parse --short HEAD`
- SENTRY_RELEASE=HEAD
- PACE_LIVETEST_INTERVAL=10s
- LOG_FORMAT=console
- S3_ENDPOINT=minio:9000
Expand Down
7 changes: 4 additions & 3 deletions go.mod
@@ -1,6 +1,6 @@
module github.com/pace/bricks

go 1.13
go 1.14

require (
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf
Expand All @@ -20,6 +20,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/mattn/go-isatty v0.0.8
github.com/mattn/goveralls v0.0.5
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/minio/minio-go/v6 v6.0.44
github.com/opentracing/opentracing-go v1.0.2
Expand All @@ -41,6 +42,6 @@ require (
github.com/uber/jaeger-lib v1.5.0
github.com/zenazn/goji v0.9.0
go.uber.org/atomic v1.3.2 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898
golang.org/x/tools v0.0.0-20200304024140-c4206d458c3f
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
)
15 changes: 15 additions & 0 deletions go.sum
Expand Up @@ -49,6 +49,8 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/goveralls v0.0.5 h1:spfq8AyZ0cCk57Za6/juJ5btQxeE1FaEGMdfcI+XO48=
github.com/mattn/goveralls v0.0.5/go.mod h1:Xg2LHi51faXLyKXwsndxiW6uxEEQT9+3sjGzzwU4xy0=
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/minio/minio-go/v6 v6.0.44 h1:CVwVXw+uCOcyMi7GvcOhxE8WgV+Xj8Vkf2jItDf/EGI=
Expand Down Expand Up @@ -116,16 +118,22 @@ golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPl
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/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-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -139,9 +147,16 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74 h1:4cFkmztxtMslUX2SctSl+blCyXfpzhGOy9LhKAqSMA4=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200113040837-eac381796e91 h1:OOkytthzFBKHY5EfEgLUabprb0LtJVkQtNxAQ02+UE4=
golang.org/x/tools v0.0.0-20200113040837-eac381796e91/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200304024140-c4206d458c3f h1:haxFuLhmPh0vRpVv5MeXoGyfCB39/Ohsq7A68h65qAg=
golang.org/x/tools v0.0.0-20200304024140-c4206d458c3f/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
Expand Down
1 change: 0 additions & 1 deletion http/jsonapi/runtime/error.go
Expand Up @@ -118,7 +118,6 @@ func WriteError(w http.ResponseWriter, code int, err error) {
if source := ei.Source; source != nil {
ev = ev.Fields(*source)
}
ev.Err(ei).Msgf("Error sent to the client. Details: %v", ei.Detail)
}
}

Expand Down
1 change: 0 additions & 1 deletion http/oauth2/oauth2.go
Expand Up @@ -67,7 +67,6 @@ func introspectRequest(r *http.Request, w http.ResponseWriter, tokenIntro TokenI

tok := security.GetBearerTokenFromHeader(r.Header.Get(oAuth2Header))
if tok == "" {
log.Req(r).Info().Msg("no bearer token in header \"Authorization\" present")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return nil, false
}
Expand Down
3 changes: 1 addition & 2 deletions http/router.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/pace/bricks/maintenance/log"
"github.com/pace/bricks/maintenance/metric"
"github.com/pace/bricks/maintenance/tracing"
"github.com/pace/bricks/maintenance/util"
)

// Router returns the default microservice endpoints for
Expand All @@ -26,7 +25,7 @@ func Router() *mux.Router {

// the logging middleware needs to be registered before the error
// middleware to make it possible to send panics to sentry
r.Use(util.NewIgnorePrefixMiddleware(log.Handler(), "/health"))
r.Use(log.Handler())

// last resort error handler
r.Use(errors.Handler())
Expand Down
7 changes: 6 additions & 1 deletion maintenance/health/servicehealthcheck/health_handler.go
Expand Up @@ -5,12 +5,17 @@ package servicehealthcheck

import (
"net/http"

"github.com/pace/bricks/maintenance/log"
)

type healthHandler struct{}

func (h *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, res := range check(&requiredChecks) {
s := log.Sink{Silent: true}
ctx := log.ContextWithSink(r.Context(), &s)

for _, res := range check(ctx, &requiredChecks) {
if res.State == Err {
writeResult(w, http.StatusServiceUnavailable, string(Err))
return
Expand Down

0 comments on commit e5fa2bb

Please sign in to comment.