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

passing getlogger to errorencoder on ancla level #233

Merged
merged 9 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Bumped argus to v0.6.0. [#233](https://github.com/xmidt-org/tr1d1um/pull/233)
- Bumped ancla to v0.3.5:
- Changed server log source address field. [#231](https://github.com/xmidt-org/tr1d1um/pull/231)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was right - this was already released in the previous version.

- Changed errorEncoder to pull logger from context. [#233](https://github.com/xmidt-org/tr1d1um/pull/233)

## [v0.6.4]
- Bumped ancla to v0.3.4:
Expand Down
17 changes: 17 additions & 0 deletions basculeLogging.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 main

import (
Expand Down
17 changes: 17 additions & 0 deletions common/context.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 common

type contextKey int
Expand Down
17 changes: 17 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 common

import (
Expand Down
17 changes: 17 additions & 0 deletions common/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 common

import (
Expand Down
17 changes: 17 additions & 0 deletions common/transactor.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 common

import (
Expand Down
17 changes: 17 additions & 0 deletions common/transactor_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 common

import (
Expand Down
28 changes: 25 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"net"
"net/http"
"strings"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/xmidt-org/bascule"

kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/xmidt-org/webpa-common/v2/logging"
)
Expand All @@ -52,6 +54,8 @@ type transactionResponse struct {
Headers interface{} `json:"headers,omitempty"`
}

type GetLoggerFunc func(context.Context) kitlog.Logger

func (rs *transactionResponse) MarshalJSON() ([]byte, error) {
return json.Marshal(rs)
}
Expand Down Expand Up @@ -115,10 +119,23 @@ func ForwardHeadersByPrefix(p string, from http.Header, to http.Header) {

// ErrorLogEncoder decorates the errorEncoder in such a way that
// errors are logged with their corresponding unique request identifier
func ErrorLogEncoder(logger kitlog.Logger, ee kithttp.ErrorEncoder) kithttp.ErrorEncoder {
var errorLogger = logging.Error(logger)
func ErrorLogEncoder(getLogger GetLoggerFunc, ee kithttp.ErrorEncoder) kithttp.ErrorEncoder {
if getLogger == nil {
getLogger = func(_ context.Context) kitlog.Logger {
return nil
}
}

return func(ctx context.Context, e error, w http.ResponseWriter) {
errorLogger.Log(logging.ErrorKey(), e.Error(), "tid", ctx.Value(ContextKeyRequestTID).(string))
code := http.StatusInternalServerError
var sc kithttp.StatusCoder
if errors.As(e, &sc) {
code = sc.StatusCode()
}
logger := getLogger(ctx)
if logger != nil && code != http.StatusNotFound {
logger.Log("sending non-200 response, non-404 response", level.Key(), level.ErrorValue(), logging.ErrorKey(), e.Error(), "tid", ctx.Value(ContextKeyRequestTID).(string))
}
ee(ctx, e, w)
}
}
Expand Down Expand Up @@ -191,3 +208,8 @@ func genTID() (tid string) {
}
return
}

func GetLogger(ctx context.Context) kitlog.Logger {
logger := kitlog.With(logging.GetLogger(ctx), "ts", kitlog.DefaultTimestampUTC)
return logger
}
34 changes: 26 additions & 8 deletions common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,34 @@ func TestForwardHeadersByPrefix(t *testing.T) {
}

func TestErrorLogEncoder(t *testing.T) {
assert := assert.New(t)
e := func(ctx context.Context, _ error, _ http.ResponseWriter) {
assert.EqualValues("tid00", ctx.Value(ContextKeyRequestTID))
tcs := []struct {
desc string
getLogger GetLoggerFunc
}{
{
desc: "nil getlogger",
getLogger: nil,
},
{
desc: "valid getlogger",
getLogger: GetLogger,
},
}
le := ErrorLogEncoder(logging.DefaultLogger(), e)

assert.NotPanics(func() {
//assumes TID is context
le(context.WithValue(context.TODO(), ContextKeyRequestTID, "tid00"), errors.New("test"), nil)
})
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
assert := assert.New(t)
e := func(ctx context.Context, _ error, _ http.ResponseWriter) {
assert.EqualValues("tid00", ctx.Value(ContextKeyRequestTID))
}
le := ErrorLogEncoder(tc.getLogger, e)

assert.NotPanics(func() {
//assumes TID is context
le(context.WithValue(context.TODO(), ContextKeyRequestTID, "tid00"), errors.New("test"), nil)
})
})
}
}

func TestWelcome(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
github.com/xmidt-org/ancla v0.3.4
github.com/xmidt-org/ancla v0.3.5
github.com/xmidt-org/bascule v0.10.2
github.com/xmidt-org/candlelight v0.0.5
github.com/xmidt-org/webpa-common/v2 v2.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vmware/govmomi v0.18.0/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59bHWk6aFU=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xmidt-org/ancla v0.3.4 h1:Zc7hy0hX0ppVo+buT+ODtI3Z6P1w0JyxI6RP5QltkRA=
github.com/xmidt-org/ancla v0.3.4/go.mod h1:UO6v7K+KQ7OtFi0nFF4zAZRMW9T83m4mZqw6iirQSs4=
github.com/xmidt-org/ancla v0.3.5 h1:zXDFTxZHVl5UTyIFQYB2FdQRMlAcp16MMEOYagIdCeo=
github.com/xmidt-org/ancla v0.3.5/go.mod h1:UO6v7K+KQ7OtFi0nFF4zAZRMW9T83m4mZqw6iirQSs4=
github.com/xmidt-org/argus v0.3.9/go.mod h1:mDFS44R704gl9Fif3gkfAyvnZa53SvMepmXjYWABPvk=
github.com/xmidt-org/argus v0.3.10-0.20201105190057-402fede05764/go.mod h1:lnMCVB/i0gOlUOOd2WbzDDgzTEqP5TipzQ8xKIw+N/I=
github.com/xmidt-org/argus v0.3.10-0.20201217204602-66f69b12c498/go.mod h1:lnMCVB/i0gOlUOOd2WbzDDgzTEqP5TipzQ8xKIw+N/I=
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,12 @@ func tr1d1um(arguments []string) (exitCode int) {
MetricsProvider: metricsRegistry,
V: builtValidators,
DisablePartnerIDs: webhookConfig.DisablePartnerIDs,
GetLogger: getLogger,
})

getAllWebhooksHandler := ancla.NewGetAllWebhooksHandler(svc)
getAllWebhooksHandler := ancla.NewGetAllWebhooksHandler(svc, ancla.HandlerConfig{
GetLogger: getLogger,
})

APIRouter.Handle("/hook", authenticate.Then(addWebhookHandler)).Methods(http.MethodPost)
APIRouter.Handle("/hooks", authenticate.Then(getAllWebhooksHandler)).Methods(http.MethodGet)
Expand Down
19 changes: 18 additions & 1 deletion stat/transport.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 stat

import (
Expand Down Expand Up @@ -31,7 +48,7 @@ type Options struct {
func ConfigHandler(c *Options) {
opts := []kithttp.ServerOption{
kithttp.ServerBefore(common.Capture(c.Log)),
kithttp.ServerErrorEncoder(common.ErrorLogEncoder(c.Log, encodeError)),
kithttp.ServerErrorEncoder(common.ErrorLogEncoder(common.GetLogger, encodeError)),
kithttp.ServerFinalizer(common.TransactionLogging(c.ReducedLoggingResponseCodes, c.Log)),
}

Expand Down
19 changes: 18 additions & 1 deletion translation/transport.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 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 translation

import (
Expand Down Expand Up @@ -50,7 +67,7 @@ type Options struct {
func ConfigHandler(c *Options) {
opts := []kithttp.ServerOption{
kithttp.ServerBefore(common.Capture(c.Log), captureWDMPParameters),
kithttp.ServerErrorEncoder(common.ErrorLogEncoder(c.Log, encodeError)),
kithttp.ServerErrorEncoder(common.ErrorLogEncoder(common.GetLogger, encodeError)),
kithttp.ServerFinalizer(common.TransactionLogging(c.ReducedLoggingResponseCodes, c.Log)),
}

Expand Down