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

Opentelemetry fixes #1450

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions pkg/networkservice/core/trace/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -46,8 +46,8 @@ func NewNetworkServiceClient(traced networkservice.NetworkServiceClient) network

func (t *beginTraceClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
// Create a new logger
operation := typeutils.GetFuncName(t.traced, "Request")
ctx, finish := withLog(ctx, operation, request.GetConnection().GetId())
operation := typeutils.GetFuncName(t.traced, methodNameRequest)
ctx, finish := withLog(ctx, operation, methodNameRequest, request.GetConnection().GetId())
defer finish()

logRequest(ctx, request, "request")
Expand All @@ -62,8 +62,8 @@ func (t *beginTraceClient) Request(ctx context.Context, request *networkservice.

func (t *beginTraceClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
// Create a new logger
operation := typeutils.GetFuncName(t.traced, "Close")
ctx, finish := withLog(ctx, operation, conn.GetId())
operation := typeutils.GetFuncName(t.traced, methodNameClose)
ctx, finish := withLog(ctx, operation, methodNameClose, conn.GetId())
defer finish()

logRequest(ctx, conn, "close")
Expand Down
9 changes: 7 additions & 2 deletions pkg/networkservice/core/trace/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -33,6 +33,11 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

const (
methodNameRequest = "Request"
methodNameClose = "Close"
)

func logRequest(ctx context.Context, request proto.Message, prefixes ...string) {
msg := strings.Join(prefixes, "-")
diffMsg := strings.Join(append(prefixes, "diff"), "-")
Expand Down
12 changes: 7 additions & 5 deletions pkg/networkservice/core/trace/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2022 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -46,7 +46,7 @@ type traceInfo struct {
}

// withLog - provides corresponding logger in context
func withLog(parent context.Context, operation, connectionID string) (c context.Context, f func()) {
func withLog(parent context.Context, operation, methodName, connectionID string) (c context.Context, f func()) {
if parent == nil {
panic("cannot create context from nil parent")
}
Expand All @@ -56,8 +56,10 @@ func withLog(parent context.Context, operation, connectionID string) (c context.

if grpcTraceState := grpcutils.TraceFromContext(parent); (grpcTraceState == grpcutils.TraceOn) ||
(grpcTraceState == grpcutils.TraceUndefined && log.IsTracingEnabled()) {
ctx, sLogger, span, sFinish := spanlogger.FromContext(parent, operation, map[string]interface{}{"type": loggedType, "id": connectionID})
ctx, lLogger, lFinish := logruslogger.FromSpan(ctx, span, operation, map[string]interface{}{"type": loggedType, "id": connectionID})
fields := []*log.Field{log.NewField("id", connectionID), log.NewField("type", loggedType)}

ctx, sLogger, span, sFinish := spanlogger.FromContext(parent, operation, methodName, fields)
ctx, lLogger, lFinish := logruslogger.FromSpan(ctx, span, operation, fields)
return withTrace(log.WithLog(ctx, sLogger, lLogger)), func() {
sFinish()
lFinish()
Expand Down
12 changes: 6 additions & 6 deletions pkg/networkservice/core/trace/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -45,8 +45,8 @@ func NewNetworkServiceServer(traced networkservice.NetworkServiceServer) network

func (t *beginTraceServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
// Create a new logger
operation := typeutils.GetFuncName(t.traced, "Request")
ctx, finish := withLog(ctx, operation, request.GetConnection().GetId())
operation := typeutils.GetFuncName(t.traced, methodNameRequest)
ctx, finish := withLog(ctx, operation, methodNameRequest, request.GetConnection().GetId())
defer finish()

logRequest(ctx, request, "request")
Expand All @@ -61,8 +61,8 @@ func (t *beginTraceServer) Request(ctx context.Context, request *networkservice.

func (t *beginTraceServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
// Create a new logger
operation := typeutils.GetFuncName(t.traced, "Close")
ctx, finish := withLog(ctx, operation, conn.GetId())
operation := typeutils.GetFuncName(t.traced, methodNameClose)
ctx, finish := withLog(ctx, operation, methodNameClose, conn.GetId())
defer finish()

logRequest(ctx, conn, "close")
Expand Down
10 changes: 10 additions & 0 deletions pkg/registry/core/trace/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,6 +28,14 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

const (
methodNameRegister = "Register"
methodNameUnregister = "Unregister"
methodNameFind = "Find"
methodNameSend = "Send"
methodNameRecv = "Recv"
)

type stackTracer interface {
StackTrace() errors.StackTrace
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/registry/core/trace/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2022 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2023 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -32,7 +32,7 @@ const (
)

// withLog - provides corresponding logger in context
func withLog(parent context.Context, operation string) (c context.Context, f func()) {
func withLog(parent context.Context, operation, methodName string) (c context.Context, f func()) {
if parent == nil {
panic("cannot create context from nil parent")
}
Expand All @@ -42,8 +42,10 @@ func withLog(parent context.Context, operation string) (c context.Context, f fun

if grpcTraceState := grpcutils.TraceFromContext(parent); (grpcTraceState == grpcutils.TraceOn) ||
(grpcTraceState == grpcutils.TraceUndefined && log.IsTracingEnabled()) {
ctx, sLogger, span, sFinish := spanlogger.FromContext(parent, operation, map[string]interface{}{"type": loggedType})
ctx, lLogger, lFinish := logruslogger.FromSpan(ctx, span, operation, map[string]interface{}{"type": loggedType})
fields := []*log.Field{log.NewField("type", loggedType)}

ctx, sLogger, span, sFinish := spanlogger.FromContext(parent, operation, methodName, fields)
ctx, lLogger, lFinish := logruslogger.FromSpan(ctx, span, operation, fields)
return log.WithLog(ctx, sLogger, lLogger), func() {
sFinish()
lFinish()
Expand Down
32 changes: 16 additions & 16 deletions pkg/registry/core/trace/ns_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type traceNetworkServiceRegistryFindClient struct {
}

func (t *traceNetworkServiceRegistryFindClient) Recv() (*registry.NetworkServiceResponse, error) {
operation := typeutils.GetFuncName(t.NetworkServiceRegistry_FindClient, "Recv")
ctx, finish := withLog(t.Context(), operation)
operation := typeutils.GetFuncName(t.NetworkServiceRegistry_FindClient, methodNameRecv)
ctx, finish := withLog(t.Context(), operation, methodNameRecv)
defer finish()

s := streamcontext.NetworkServiceRegistryFindClient(ctx, t.NetworkServiceRegistry_FindClient)
Expand All @@ -65,8 +65,8 @@ func (t *traceNetworkServiceRegistryFindClient) Recv() (*registry.NetworkService
}

func (t *traceNetworkServiceRegistryClient) Register(ctx context.Context, in *registry.NetworkService, opts ...grpc.CallOption) (*registry.NetworkService, error) {
operation := typeutils.GetFuncName(t.traced, "Register")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameRegister)
ctx, finish := withLog(ctx, operation, methodNameRegister)
defer finish()

logObjectTrace(ctx, "register", in)
Expand All @@ -79,8 +79,8 @@ func (t *traceNetworkServiceRegistryClient) Register(ctx context.Context, in *re
return rv, nil
}
func (t *traceNetworkServiceRegistryClient) Find(ctx context.Context, in *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) {
operation := typeutils.GetFuncName(t.traced, "Find")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameFind)
ctx, finish := withLog(ctx, operation, methodNameFind)
defer finish()

logObjectTrace(ctx, "find", in)
Expand All @@ -96,8 +96,8 @@ func (t *traceNetworkServiceRegistryClient) Find(ctx context.Context, in *regist
}

func (t *traceNetworkServiceRegistryClient) Unregister(ctx context.Context, in *registry.NetworkService, opts ...grpc.CallOption) (*empty.Empty, error) {
operation := typeutils.GetFuncName(t.traced, "Unregister")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameUnregister)
ctx, finish := withLog(ctx, operation, methodNameUnregister)
defer finish()

logObjectTrace(ctx, "unregister", in)
Expand All @@ -121,8 +121,8 @@ type traceNetworkServiceRegistryServer struct {
}

func (t *traceNetworkServiceRegistryServer) Register(ctx context.Context, in *registry.NetworkService) (*registry.NetworkService, error) {
operation := typeutils.GetFuncName(t.traced, "Register")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameRegister)
ctx, finish := withLog(ctx, operation, methodNameRegister)
defer finish()

logObjectTrace(ctx, "register", in)
Expand All @@ -136,8 +136,8 @@ func (t *traceNetworkServiceRegistryServer) Register(ctx context.Context, in *re
}

func (t *traceNetworkServiceRegistryServer) Find(in *registry.NetworkServiceQuery, s registry.NetworkServiceRegistry_FindServer) error {
operation := typeutils.GetFuncName(t.traced, "Find")
ctx, finish := withLog(s.Context(), operation)
operation := typeutils.GetFuncName(t.traced, methodNameFind)
ctx, finish := withLog(s.Context(), operation, methodNameFind)
defer finish()

s = &traceNetworkServiceRegistryFindServer{
Expand All @@ -154,8 +154,8 @@ func (t *traceNetworkServiceRegistryServer) Find(in *registry.NetworkServiceQuer
}

func (t *traceNetworkServiceRegistryServer) Unregister(ctx context.Context, in *registry.NetworkService) (*empty.Empty, error) {
operation := typeutils.GetFuncName(t.traced, "Unregister")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameUnregister)
ctx, finish := withLog(ctx, operation, methodNameUnregister)
defer finish()

logObjectTrace(ctx, "unregister", in)
Expand All @@ -179,8 +179,8 @@ type traceNetworkServiceRegistryFindServer struct {
}

func (t *traceNetworkServiceRegistryFindServer) Send(nsResp *registry.NetworkServiceResponse) error {
operation := typeutils.GetFuncName(t.NetworkServiceRegistry_FindServer, "Send")
ctx, finish := withLog(t.Context(), operation)
operation := typeutils.GetFuncName(t.NetworkServiceRegistry_FindServer, methodNameSend)
ctx, finish := withLog(t.Context(), operation, methodNameSend)
defer finish()

logObjectTrace(ctx, "network service", nsResp.NetworkService)
Expand Down
33 changes: 16 additions & 17 deletions pkg/registry/core/trace/nse_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ type traceNetworkServiceEndpointRegistryFindClient struct {
}

func (t *traceNetworkServiceEndpointRegistryFindClient) Recv() (*registry.NetworkServiceEndpointResponse, error) {
operation := typeutils.GetFuncName(t.NetworkServiceEndpointRegistry_FindClient, "Recv")

ctx, finish := withLog(t.Context(), operation)
operation := typeutils.GetFuncName(t.NetworkServiceEndpointRegistry_FindClient, methodNameRecv)
ctx, finish := withLog(t.Context(), operation, methodNameRecv)
defer finish()

s := streamcontext.NetworkServiceEndpointRegistryFindClient(ctx, t.NetworkServiceEndpointRegistry_FindClient)
Expand All @@ -65,8 +64,8 @@ func (t *traceNetworkServiceEndpointRegistryFindClient) Recv() (*registry.Networ
}

func (t *traceNetworkServiceEndpointRegistryClient) Register(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) {
operation := typeutils.GetFuncName(t.traced, "Register")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameRegister)
ctx, finish := withLog(ctx, operation, methodNameRegister)
defer finish()

logObjectTrace(ctx, "register", in)
Expand All @@ -78,8 +77,8 @@ func (t *traceNetworkServiceEndpointRegistryClient) Register(ctx context.Context
return rv, nil
}
func (t *traceNetworkServiceEndpointRegistryClient) Find(ctx context.Context, in *registry.NetworkServiceEndpointQuery, opts ...grpc.CallOption) (registry.NetworkServiceEndpointRegistry_FindClient, error) {
operation := typeutils.GetFuncName(t.traced, "Find")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameFind)
ctx, finish := withLog(ctx, operation, methodNameFind)
defer finish()

logObjectTrace(ctx, "find", in)
Expand All @@ -95,8 +94,8 @@ func (t *traceNetworkServiceEndpointRegistryClient) Find(ctx context.Context, in
}

func (t *traceNetworkServiceEndpointRegistryClient) Unregister(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*empty.Empty, error) {
operation := typeutils.GetFuncName(t.traced, "Unregister")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameUnregister)
ctx, finish := withLog(ctx, operation, methodNameUnregister)
defer finish()

logObjectTrace(ctx, "unregister", in)
Expand All @@ -120,8 +119,8 @@ type traceNetworkServiceEndpointRegistryServer struct {
}

func (t *traceNetworkServiceEndpointRegistryServer) Register(ctx context.Context, in *registry.NetworkServiceEndpoint) (*registry.NetworkServiceEndpoint, error) {
operation := typeutils.GetFuncName(t.traced, "Register")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameRegister)
ctx, finish := withLog(ctx, operation, methodNameRegister)
defer finish()

logObjectTrace(ctx, "register", in)
Expand All @@ -135,8 +134,8 @@ func (t *traceNetworkServiceEndpointRegistryServer) Register(ctx context.Context
}

func (t *traceNetworkServiceEndpointRegistryServer) Find(in *registry.NetworkServiceEndpointQuery, s registry.NetworkServiceEndpointRegistry_FindServer) error {
operation := typeutils.GetFuncName(t.traced, "Find")
ctx, finish := withLog(s.Context(), operation)
operation := typeutils.GetFuncName(t.traced, methodNameFind)
ctx, finish := withLog(s.Context(), operation, methodNameFind)
defer finish()

s = &traceNetworkServiceEndpointRegistryFindServer{
Expand All @@ -155,8 +154,8 @@ func (t *traceNetworkServiceEndpointRegistryServer) Find(in *registry.NetworkSer
}

func (t *traceNetworkServiceEndpointRegistryServer) Unregister(ctx context.Context, in *registry.NetworkServiceEndpoint) (*empty.Empty, error) {
operation := typeutils.GetFuncName(t.traced, "Unregister")
ctx, finish := withLog(ctx, operation)
operation := typeutils.GetFuncName(t.traced, methodNameUnregister)
ctx, finish := withLog(ctx, operation, methodNameUnregister)
defer finish()

logObjectTrace(ctx, "unregister", in)
Expand All @@ -180,8 +179,8 @@ type traceNetworkServiceEndpointRegistryFindServer struct {
}

func (t *traceNetworkServiceEndpointRegistryFindServer) Send(nseResp *registry.NetworkServiceEndpointResponse) error {
operation := typeutils.GetFuncName(t.NetworkServiceEndpointRegistry_FindServer, "Send")
ctx, finish := withLog(t.Context(), operation)
operation := typeutils.GetFuncName(t.NetworkServiceEndpointRegistry_FindServer, methodNameSend)
ctx, finish := withLog(t.Context(), operation, methodNameSend)
defer finish()

logObjectTrace(ctx, "network service endpoint", nseResp.NetworkServiceEndpoint)
Expand Down
10 changes: 6 additions & 4 deletions pkg/tools/dnsutils/trace/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -39,14 +39,16 @@ type traceInfo struct {
ResponseMsg *dns.Msg
}

func withLog(parent context.Context, operation, messageID string) (c context.Context, f func()) {
func withLog(parent context.Context, operation, methodName, 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})
fields := []*log.Field{log.NewField("type", loggedType), log.NewField("id", messageID)}

ctx, sLogger, span, sFinish := spanlogger.FromContext(parent, operation, methodName, fields)
ctx, lLogger, lFinish := logruslogger.FromSpan(ctx, span, operation, fields)
return withTrace(log.WithLog(ctx, sLogger, lLogger)), func() {
sFinish()
lFinish()
Expand Down
7 changes: 4 additions & 3 deletions pkg/tools/dnsutils/trace/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco Systems, Inc.
// Copyright (c) 2022-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -42,8 +42,9 @@ func NewDNSHandler(traced dnsutils.Handler) dnsutils.Handler {
}

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)))
methodName := "ServeDNS"
operation := typeutils.GetFuncName(t.traced, methodName)
ctx, finish := withLog(ctx, operation, methodName, strconv.Itoa(int(m.Id)))
defer finish()

logRequest(ctx, m, "message")
Expand Down
Loading