Skip to content

Commit

Permalink
Added logger wrapper for health (#28)
Browse files Browse the repository at this point in the history
* Added logger wrapper for health endpoint

* Used configuration for ping interval
  • Loading branch information
kristinapathak authored Apr 1, 2019
1 parent 064b634 commit 6609e32
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 29 deletions.
39 changes: 27 additions & 12 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (

"github.com/go-kit/kit/metrics/provider"
"github.com/goph/emperror"

"github.com/InVisionApp/go-health"
"github.com/InVisionApp/go-health/checkers"
)

var (
Expand Down Expand Up @@ -66,6 +69,7 @@ type Connection struct {
stats stats
gennericDB *sql.DB

health *health.Health
measures Measures
stopThreads []chan struct{}
}
Expand Down Expand Up @@ -141,14 +145,16 @@ func (Record) TableName() string {
}

// CreateDbConnection creates db connection and returns the struct to the consumer.
func CreateDbConnection(config Config, provider provider.Provider) (*Connection, error) {
func CreateDbConnection(config Config, provider provider.Provider, health *health.Health) (*Connection, error) {
var (
conn *dbDecorator
err error
connectionURL string
)

db := Connection{}
db := Connection{
health: health,
}

// pq expects seconds
connectTimeout := strconv.Itoa(int(config.ConnectTimeout.Seconds()))
Expand Down Expand Up @@ -197,6 +203,7 @@ func CreateDbConnection(config Config, provider provider.Provider) (*Connection,
db.gennericDB = conn.DB.DB()
db.measures = NewMeasures(provider)

db.setupHealthCheck(config.PingInterval)
db.setupMetrics()
db.configure(config.MaxIdleConns, config.MaxOpenConns)

Expand All @@ -211,18 +218,26 @@ func (db *Connection) configure(maxIdleConns int, maxOpenConns int) {
db.gennericDB.SetMaxOpenConns(maxOpenConns)
}

func (db *Connection) setupMetrics() {
// ping to check status
pingStop := doEvery(time.Second, func() {
err := db.Ping()
if err != nil {
db.measures.ConnectionStatus.Set(0.0)
} else {
db.measures.ConnectionStatus.Set(1.0)
}
func (db *Connection) setupHealthCheck(interval time.Duration) {
if db.health == nil {
return
}
sqlCheck, err := checkers.NewSQL(&checkers.SQLConfig{
Pinger: db.gennericDB,
})
db.stopThreads = append(db.stopThreads, pingStop)
if err != nil {
// todo: capture this error somehow
}

db.health.AddCheck(&health.Config{
Name: "sql-check",
Checker: sqlCheck,
Interval: interval,
Fatal: true,
})
}

func (db *Connection) setupMetrics() {
// baseline
startStats := db.stats.getStats()
prevWaitCount := startStats.WaitCount
Expand Down
8 changes: 0 additions & 8 deletions db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const (

const (
RetryCounter = "retry_count"
ConnectionStatusGauge = "connection_status"
PoolOpenConnectionsGauge = "pool_open_connections"
PoolInUseConnectionsGauge = "pool_in_use_connections"
PoolIdleConnectionsGauge = "pool_idle_connections"
Expand All @@ -56,11 +55,6 @@ func Metrics() []xmetrics.Metric {
Type: "counter",
Help: "Indicates the number of retries for sql queries",
},
{
Name: ConnectionStatusGauge,
Type: "gauge",
Help: "Indicates whether a sql connection is currently running",
},
{
Name: PoolOpenConnectionsGauge,
Type: "gauge",
Expand Down Expand Up @@ -124,7 +118,6 @@ func Metrics() []xmetrics.Metric {

type Measures struct {
Retry xmetrics.Incrementer
ConnectionStatus metrics.Gauge
PoolOpenConnections metrics.Gauge
PoolInUseConnections metrics.Gauge
PoolIdleConnections metrics.Gauge
Expand All @@ -142,7 +135,6 @@ type Measures struct {
func NewMeasures(p provider.Provider) Measures {
return Measures{
Retry: xmetrics.NewIncrementer(p.NewCounter(RetryCounter)),
ConnectionStatus: p.NewGauge(ConnectionStatusGauge),
PoolOpenConnections: p.NewGauge(PoolOpenConnectionsGauge),
PoolInUseConnections: p.NewGauge(PoolInUseConnectionsGauge),
PoolIdleConnections: p.NewGauge(PoolIdleConnectionsGauge),
Expand Down
13 changes: 4 additions & 9 deletions deploy/docker-compose/docFiles/gungnir.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
---
primary:
address: ":7000"
health:
address: ":7001"
options:
- "PayloadsOverZero"
- "PayloadsOverHundred"
- "PayloadsOverThousand"
- "PayloadsOverTenThousand"
readTimeout: "15s"
idleTimeout: "15s"
pprof:
address: ":7002"
readTimeout: "15s"
Expand All @@ -28,6 +19,9 @@

authHeader: ["YXV0aEhlYWRlcg=="]

health:
port: :7001
endpoint: /health
getLimit: 2
getRetries: 0
retryInterval: 0s
Expand All @@ -37,5 +31,6 @@
database: "devices"
numRetries: 4
waitTimeMult: 5
pingInterval: 1s
connectTimeout: 1m
opTimeout: 100ms
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.12
require (
github.com/Comcast/webpa-common v0.0.0-20190312224005-400bb4f8fc50
github.com/DATA-DOG/godog v0.7.13
github.com/InVisionApp/go-health v2.1.0+incompatible
github.com/InVisionApp/go-logger v1.0.1
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/go-kit/kit v0.8.0
github.com/go-logfmt/logfmt v0.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ github.com/Comcast/webpa-common v0.9.0-alpha h1:dYlsMUnTF2vgfhxPT8NFivBm49kFaix0
github.com/DATA-DOG/godog v0.7.13 h1:JmgpKcra7Vf3yzI9vPsWyoQRx13tyKziHtXWDCUUgok=
github.com/DATA-DOG/godog v0.7.13/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0=
github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/InVisionApp/go-health v2.1.0+incompatible h1:m5nRf/RKaMCkob7V5Vc3tuzlpqY2K9hL5awZomjzuCk=
github.com/InVisionApp/go-health v2.1.0+incompatible/go.mod h1:/+Gv1o8JUsrjC6pi6MN6/CgKJo4OqZ6x77XAnImrzhg=
github.com/InVisionApp/go-logger v1.0.1 h1:WFL19PViM1mHUmUWfsv5zMo379KSWj2MRmBlzMFDRiE=
github.com/InVisionApp/go-logger v1.0.1/go.mod h1:+cGTDSn+P8105aZkeOfIhdd7vFO5X1afUHcjvanY0L8=
github.com/Jeffail/gabs v1.1.1/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.16.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
Expand Down
95 changes: 95 additions & 0 deletions healthlogger/healthlogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright 2019 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package healthlogger

import (
"fmt"

"github.com/Comcast/webpa-common/logging"
hlog "github.com/InVisionApp/go-logger"
"github.com/go-kit/kit/log"
)

type HealthLogger struct {
log.Logger
keyValPairs []interface{}
}

type Option func(*HealthLogger)

func NewHealthLogger(logger log.Logger) hlog.Logger {
h := HealthLogger{logger, []interface{}{}}

return &h
}

func (h *HealthLogger) Debug(msg ...interface{}) {
logging.Debug(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Info(msg ...interface{}) {
logging.Info(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Warn(msg ...interface{}) {
logging.Warn(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Error(msg ...interface{}) {
logging.Error(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Debugln(msg ...interface{}) {
logging.Debug(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Infoln(msg ...interface{}) {
logging.Info(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Warnln(msg ...interface{}) {
logging.Warn(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Errorln(msg ...interface{}) {
logging.Error(h, h.keyValPairs...).Log(logging.MessageKey(), msg)
}

func (h *HealthLogger) Debugf(format string, args ...interface{}) {
logging.Debug(h, h.keyValPairs...).Log(logging.MessageKey(), fmt.Sprintf(format, args...))
}

func (h *HealthLogger) Infof(format string, args ...interface{}) {
logging.Info(h, h.keyValPairs...).Log(logging.MessageKey(), fmt.Sprintf(format, args...))
}

func (h *HealthLogger) Warnf(format string, args ...interface{}) {
logging.Warn(h, h.keyValPairs...).Log(logging.MessageKey(), fmt.Sprintf(format, args...))
}

func (h *HealthLogger) Errorf(format string, args ...interface{}) {
logging.Error(h, h.keyValPairs...).Log(logging.MessageKey(), fmt.Sprintf(format, args...))
}

func (h *HealthLogger) WithFields(fields hlog.Fields) hlog.Logger {
newKeyVals := h.keyValPairs
for key, val := range fields {
newKeyVals = append(newKeyVals, key, val)
}
return &HealthLogger{h, newKeyVals}
}
34 changes: 34 additions & 0 deletions healthlogger/healthlogger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2019 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package healthlogger

import (
"testing"

hlog "github.com/InVisionApp/go-logger"
"github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
)

func TestImplementsInterface(t *testing.T) {
logger := log.NewNopLogger()
var hlogger hlog.Logger
hlogger = NewHealthLogger(logger)
assert := assert.New(t)
assert.NotNil(hlogger)
}

0 comments on commit 6609e32

Please sign in to comment.