xmidt-org / tr1d1um

Compare 9396233 ... +45 ... f6196ba

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 10 of 47 files from the diff.
Newly tracked file
basculeLogging.go created.
Newly tracked file
main.go changed.

@@ -0,0 +1,40 @@
Loading
1 +
package main
2 +
3 +
import (
4 +
	"context"
5 +
	"net/http"
6 +
	"strings"
7 +
8 +
	"github.com/go-kit/kit/log"
9 +
	"github.com/xmidt-org/candlelight"
10 +
	"github.com/xmidt-org/webpa-common/v2/logging"
11 +
)
12 +
13 +
func sanitizeHeaders(headers http.Header) (filtered http.Header) {
14 +
	filtered = headers.Clone()
15 +
	if authHeader := filtered.Get("Authorization"); authHeader != "" {
16 +
		filtered.Del("Authorization")
17 +
		parts := strings.Split(authHeader, " ")
18 +
		if len(parts) == 2 {
19 +
			filtered.Set("Authorization-Type", parts[0])
20 +
		}
21 +
	}
22 +
	return
23 +
}
24 +
25 +
func setLogger(logger log.Logger) func(delegate http.Handler) http.Handler {
26 +
	return func(delegate http.Handler) http.Handler {
27 +
		return http.HandlerFunc(
28 +
			func(w http.ResponseWriter, r *http.Request) {
29 +
				kvs := []interface{}{"requestHeaders", sanitizeHeaders(r.Header), "requestURL", r.URL.EscapedPath(), "method", r.Method}
30 +
				kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs)
31 +
				ctx := r.WithContext(logging.WithLogger(r.Context(), log.With(logger, kvs...)))
32 +
				delegate.ServeHTTP(w, ctx)
33 +
			})
34 +
	}
35 +
}
36 +
37 +
func getLogger(ctx context.Context) log.Logger {
38 +
	logger := log.With(logging.GetLogger(ctx), "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
39 +
	return logger
40 +
}

@@ -8,19 +8,17 @@
Loading
8 8
	"net/http"
9 9
	"strings"
10 10
11 -
	"github.com/xmidt-org/tr1d1um/common"
12 -
13 -
	"github.com/justinas/alice"
14 -
	"github.com/xmidt-org/wrp-go/wrp"
15 -
16 11
	kitlog "github.com/go-kit/kit/log"
17 12
	kithttp "github.com/go-kit/kit/transport/http"
18 -
19 13
	"github.com/gorilla/mux"
20 -
	"github.com/xmidt-org/bascule"
21 -
	"github.com/xmidt-org/webpa-common/basculechecks"
14 +
	"github.com/justinas/alice"
15 +
	"github.com/spf13/cast"
22 16
23 -
	"github.com/xmidt-org/wrp-go/wrp/wrphttp"
17 +
	"github.com/xmidt-org/bascule"
18 +
	"github.com/xmidt-org/tr1d1um/common"
19 +
	"github.com/xmidt-org/webpa-common/v2/basculechecks"
20 +
	"github.com/xmidt-org/wrp-go/v3"
21 +
	"github.com/xmidt-org/wrp-go/v3/wrphttp"
24 22
)
25 23
26 24
const (
@@ -102,11 +100,16 @@
Loading
102 100
	if tokenType != "jwt" {
103 101
		return getPartnerIDs(r.Header)
104 102
	}
105 -
	partnerIDs, ok := auth.Token.Attributes().GetStringSlice(basculechecks.PartnerKey)
103 +
	partnerVal, ok := bascule.GetNestedAttribute(auth.Token.Attributes(), basculechecks.PartnerKeys()...)
106 104
	//if no partner ids
107 105
	if !ok {
108 106
		return getPartnerIDs(r.Header)
109 107
	}
108 +
	partnerIDs, err := cast.ToStringSliceE(partnerVal)
109 +
110 +
	if err != nil {
111 +
		return getPartnerIDs(r.Header)
112 +
	}
110 113
	return partnerIDs
111 114
}
112 115

@@ -9,11 +9,13 @@
Loading
9 9
	"strings"
10 10
	"time"
11 11
12 +
	"github.com/xmidt-org/candlelight"
13 +
12 14
	"github.com/xmidt-org/bascule"
13 15
14 16
	kitlog "github.com/go-kit/kit/log"
15 17
	kithttp "github.com/go-kit/kit/transport/http"
16 -
	"github.com/xmidt-org/webpa-common/logging"
18 +
	"github.com/xmidt-org/webpa-common/v2/logging"
17 19
)
18 20
19 21
type transactionRequest struct {
@@ -45,10 +47,12 @@
Loading
45 47
	errorLogger := logging.Error(logger)
46 48
	return func(ctx context.Context, code int, r *http.Request) {
47 49
		tid, _ := ctx.Value(ContextKeyRequestTID).(string)
48 -
		transactionInfoLogger, ok := ctx.Value(ContextKeyTransactionInfoLogger).(kitlog.Logger)
50 +
		transactionInfoLogger, transactionLoggerOk := ctx.Value(ContextKeyTransactionInfoLogger).(kitlog.Logger)
49 51
50 -
		if !ok {
51 -
			errorLogger.Log(logging.MessageKey(), "transaction logger not found in context", "tid", tid)
52 +
		if !transactionLoggerOk {
53 +
			var kvs = []interface{}{logging.MessageKey(), "transaction logger not found in context", "tid", tid}
54 +
			kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs)
55 +
			errorLogger.Log(kvs)
52 56
			return
53 57
		}
54 58
@@ -57,7 +61,9 @@
Loading
57 61
		if ok {
58 62
			transactionInfoLogger = kitlog.WithPrefix(transactionInfoLogger, "duration", time.Since(requestArrival))
59 63
		} else {
60 -
			errorLogger.Log(logging.ErrorKey(), "Request arrival not capture for transaction logger", "tid", tid)
64 +
			kvs := []interface{}{logging.ErrorKey(), "Request arrival not capture for transaction logger", "tid", tid}
65 +
			kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs)
66 +
			errorLogger.Log(kvs)
61 67
		}
62 68
63 69
		includeHeaders := true
@@ -116,7 +122,6 @@
Loading
116 122
// intended to be used only throughout the gokit server flow: (request decoding, business logic,  response encoding)
117 123
func Capture(logger kitlog.Logger) kithttp.RequestFunc {
118 124
	var transactionInfoLogger = logging.Info(logger)
119 -
120 125
	return func(ctx context.Context, r *http.Request) (nctx context.Context) {
121 126
		var tid string
122 127
@@ -133,8 +138,7 @@
Loading
133 138
			satClientID = auth.Token.Principal()
134 139
		}
135 140
136 -
		transactionInfoLogger := kitlog.WithPrefix(transactionInfoLogger,
137 -
			logging.MessageKey(), "record",
141 +
		logKVs := []interface{}{logging.MessageKey(), "record",
138 142
			"request", transactionRequest{
139 143
				Address: r.RemoteAddr,
140 144
				Path:    r.URL.Path,
@@ -143,8 +147,10 @@
Loading
143 147
			},
144 148
			"tid", tid,
145 149
			"satClientID", satClientID,
146 -
		)
150 +
		}
147 151
152 +
		logKVs, _ = candlelight.AppendTraceInfo(ctx, logKVs)
153 +
		transactionInfoLogger := kitlog.WithPrefix(transactionInfoLogger, logKVs...)
148 154
		return context.WithValue(nctx, ContextKeyTransactionInfoLogger, transactionInfoLogger)
149 155
	}
150 156
}

@@ -2,19 +2,20 @@
Loading
2 2
3 3
import (
4 4
	"bytes"
5 +
	"context"
5 6
6 7
	"net/http"
7 8
8 9
	"github.com/xmidt-org/bascule/acquire"
9 10
	"github.com/xmidt-org/tr1d1um/common"
10 11
11 -
	"github.com/xmidt-org/wrp-go/wrp"
12 +
	"github.com/xmidt-org/wrp-go/v3"
12 13
)
13 14
14 15
// Service represents the Webpa-Tr1d1um component that translates WDMP data into WRP
15 16
// which is compatible with the XMiDT API.
16 17
type Service interface {
17 -
	SendWRP(*wrp.Message, string) (*common.XmidtResponse, error)
18 +
	SendWRP(context.Context, *wrp.Message, string) (*common.XmidtResponse, error)
18 19
}
19 20
20 21
// ServiceOptions defines the options needed to build a new translation WRP service.
@@ -54,7 +55,7 @@
Loading
54 55
}
55 56
56 57
// SendWRP sends the given wrpMsg to the XMiDT cluster and returns the response if any.
57 -
func (w *service) SendWRP(wrpMsg *wrp.Message, authHeaderValue string) (*common.XmidtResponse, error) {
58 +
func (w *service) SendWRP(ctx context.Context, wrpMsg *wrp.Message, authHeaderValue string) (*common.XmidtResponse, error) {
58 59
	wrpMsg.Source = w.wrpSource
59 60
60 61
	var payload []byte
@@ -65,7 +66,7 @@
Loading
65 66
		return nil, err
66 67
	}
67 68
68 -
	r, err := http.NewRequest(http.MethodPost, w.xmidtWrpURL, bytes.NewBuffer(payload))
69 +
	r, err := http.NewRequestWithContext(ctx, http.MethodPost, w.xmidtWrpURL, bytes.NewBuffer(payload))
69 70
70 71
	if err != nil {
71 72
		return nil, err
@@ -80,6 +81,5 @@
Loading
80 81
81 82
	r.Header.Set("Content-Type", wrp.Msgpack.ContentType())
82 83
	r.Header.Set("Authorization", authHeaderValue)
83 -
84 84
	return w.transactor.Transact(r)
85 85
}

@@ -7,7 +7,7 @@
Loading
7 7
8 8
	"github.com/xmidt-org/tr1d1um/common"
9 9
10 -
	"github.com/xmidt-org/webpa-common/device"
10 +
	"github.com/xmidt-org/webpa-common/v2/device"
11 11
12 12
	kitlog "github.com/go-kit/kit/log"
13 13
	kithttp "github.com/go-kit/kit/transport/http"

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Learn more Showing 2 files with coverage changes found.

New file main.go
New
Loading file...
New file basculeLogging.go
New
Loading file...

47 Commits

+9
+9
+73
+45
+28
+2 Files
+283
+7
+276
-2
-5
+3
+6
+5
+1
Hiding 2 contexual commits
Hiding 3 contexual commits
+2
-1
-1
-2 Files
-81
-43
-8
-30
-62
-50
+1
-13
Files Coverage
common -2.16% 60.34%
stat 73.01%
translation 0.17% 84.79%
basculeLogging.go 40.00%
main.go 0.00%
Project Totals (13 files) 46.08%
Loading