-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tracing and logs to dns handlers (#1343)
* add logs to dns context functions Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * add tracing to dns handlers Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * resolve conflict Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com> * use another diff package (looks better) Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * cleanup Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * add ResponseWriter wrapper Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * add ResponseWriter wrapper Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * add logResponse func Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * add wrapper on every trace chain element call (looks working) Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * cleanup Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * cleanup Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * resolve conflict Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com> * apply review comments Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> * fix after go mod tidy Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com> Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com> Signed-off-by: anastasia.malysheva <anastasia.malysheva@xored.com> Co-authored-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
- Loading branch information
1 parent
a7b84cf
commit dacfc98
Showing
10 changed files
with
293 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2022 Cisco Systems, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 chain provides a simple file for creating a dnsutils.Handler from a 'chain' of dnsutils.Handler | ||
package chain | ||
|
||
import ( | ||
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils" | ||
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/next" | ||
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/trace" | ||
) | ||
|
||
// NewDNSHandler - chains together a list of dnsutils.Handler with tracing | ||
func NewDNSHandler(handlers ...dnsutils.Handler) dnsutils.Handler { | ||
return next.NewDNSHandler( | ||
next.NewWrappedDNSHandler(trace.NewDNSHandler, handlers...), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2022 Cisco Systems, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 trace | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/miekg/dns" | ||
"github.com/r3labs/diff" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
) | ||
|
||
func logRequest(ctx context.Context, message *dns.Msg, prefixes ...string) { | ||
msg := strings.Join(append(prefixes, "request"), "-") | ||
diffMsg := strings.Join(append(prefixes, "request", "diff"), "-") | ||
|
||
messageInfo, ok := trace(ctx) | ||
if ok && !cmp.Equal(messageInfo.RequestMsg, message) { | ||
if messageInfo.RequestMsg != nil { | ||
messageDiff, _ := diff.Diff(messageInfo.RequestMsg, message) | ||
if len(messageDiff) > 0 { | ||
logObjectTrace(ctx, diffMsg, messageDiff) | ||
} | ||
} else { | ||
logObjectTrace(ctx, msg, message) | ||
} | ||
messageInfo.RequestMsg = message.Copy() | ||
return | ||
} | ||
} | ||
|
||
func logResponse(ctx context.Context, message *dns.Msg, prefixes ...string) { | ||
msg := strings.Join(append(prefixes, "response"), "-") | ||
diffMsg := strings.Join(append(prefixes, "response", "diff"), "-") | ||
|
||
messageInfo, ok := trace(ctx) | ||
if ok && !cmp.Equal(messageInfo.ResponseMsg, message) { | ||
if messageInfo.ResponseMsg != nil { | ||
messageDiff, _ := diff.Diff(messageInfo.ResponseMsg, message) | ||
if len(messageDiff) > 0 { | ||
logObjectTrace(ctx, diffMsg, messageDiff) | ||
} | ||
} else { | ||
logObjectTrace(ctx, msg, message) | ||
} | ||
messageInfo.ResponseMsg = message.Copy() | ||
return | ||
} | ||
} | ||
|
||
func logObjectTrace(ctx context.Context, k, v interface{}) { | ||
s := log.FromContext(ctx) | ||
msg := "" | ||
cc, err := json.Marshal(v) | ||
if err == nil { | ||
msg = string(cc) | ||
} else { | ||
msg = fmt.Sprint(v) | ||
} | ||
s.Tracef("%v=%s", k, msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2020-2022 Cisco Systems, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 trace provides a wrapper for tracing around a dnsutils.Handler | ||
package trace | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/miekg/dns" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" | ||
"github.com/networkservicemesh/sdk/pkg/tools/log/spanlogger" | ||
) | ||
|
||
type contextKeyType string | ||
|
||
const ( | ||
traceInfoKey contextKeyType = "MessageInfo" | ||
loggedType string = "dnsServer" | ||
) | ||
|
||
type traceInfo struct { | ||
RequestMsg *dns.Msg | ||
ResponseMsg *dns.Msg | ||
} | ||
|
||
func withLog(parent context.Context, operation, messageID string) (c context.Context, f func()) { | ||
if parent == nil { | ||
panic("cannot create context from nil parent") | ||
} | ||
|
||
if log.IsTracingEnabled() { | ||
ctx, sLogger, span, sFinish := spanlogger.FromContext(parent, operation, map[string]interface{}{"type": loggedType, "id": messageID}) | ||
ctx, lLogger, lFinish := logruslogger.FromSpan(ctx, span, operation, map[string]interface{}{"type": loggedType, "id": messageID}) | ||
return withTrace(log.WithLog(ctx, sLogger, lLogger)), func() { | ||
sFinish() | ||
lFinish() | ||
} | ||
} | ||
return log.WithLog(parent), func() {} | ||
} | ||
|
||
func withTrace(parent context.Context) context.Context { | ||
if parent == nil { | ||
panic("cannot create context from nil parent") | ||
} | ||
if _, ok := trace(parent); ok { | ||
return parent | ||
} | ||
|
||
return context.WithValue(parent, traceInfoKey, &traceInfo{}) | ||
} | ||
|
||
func trace(ctx context.Context) (*traceInfo, bool) { | ||
val, ok := ctx.Value(traceInfoKey).(*traceInfo) | ||
return val, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2022 Cisco Systems, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// 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 trace | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/miekg/dns" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils" | ||
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/next" | ||
"github.com/networkservicemesh/sdk/pkg/tools/typeutils" | ||
) | ||
|
||
type beginTraceHandler struct { | ||
traced dnsutils.Handler | ||
} | ||
|
||
type endTraceHandler struct{} | ||
|
||
// NewDNSHandler - wraps tracing around the supplied traced | ||
func NewDNSHandler(traced dnsutils.Handler) dnsutils.Handler { | ||
return next.NewDNSHandler( | ||
&beginTraceHandler{traced: traced}, | ||
&endTraceHandler{}, | ||
) | ||
} | ||
|
||
func (t *beginTraceHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, m *dns.Msg) { | ||
operation := typeutils.GetFuncName(t.traced, "ServeDNS") | ||
ctx, finish := withLog(ctx, operation, strconv.Itoa(int(m.Id))) | ||
defer finish() | ||
|
||
logRequest(ctx, m, "message") | ||
|
||
wrapper := wrapResponseWriter(rw) | ||
t.traced.ServeDNS(ctx, wrapper, m) | ||
|
||
logResponse(ctx, &wrapper.responseMsg, "message") | ||
} | ||
|
||
func (t *endTraceHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, m *dns.Msg) { | ||
logRequest(ctx, m, "message") | ||
|
||
wrapper := wrapResponseWriter(rw) | ||
next.Handler(ctx).ServeDNS(ctx, wrapper, m) | ||
|
||
logResponse(ctx, &wrapper.responseMsg, "message") | ||
} | ||
|
||
func wrapResponseWriter(rw dns.ResponseWriter) *traceResponseWriter { | ||
wrapper, ok := rw.(*traceResponseWriter) | ||
if !ok { | ||
wrapper = &traceResponseWriter{ | ||
ResponseWriter: rw, | ||
} | ||
} | ||
|
||
return wrapper | ||
} | ||
|
||
type traceResponseWriter struct { | ||
dns.ResponseWriter | ||
responseMsg dns.Msg | ||
} | ||
|
||
func (rw *traceResponseWriter) WriteMsg(m *dns.Msg) error { | ||
rw.responseMsg = *m.Copy() | ||
return rw.ResponseWriter.WriteMsg(m) | ||
} |