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

Refactor tracing interceptor #450

Merged
merged 9 commits into from Apr 25, 2022

Conversation

XSAM
Copy link
Contributor

@XSAM XSAM commented Aug 8, 2021

Split from #312. I will create the remaining parts of #312 once this PR gets merged.

@bwplotka @yashrsharma44 PTAL

@codecov-commenter
Copy link

codecov-commenter commented Aug 8, 2021

Codecov Report

Merging #450 (7e04286) into v2 (6e2c2ac) will decrease coverage by 25.42%.
The diff coverage is 50.64%.

@@             Coverage Diff             @@
##               v2     #450       +/-   ##
===========================================
- Coverage   84.01%   58.59%   -25.43%     
===========================================
  Files          30       31        +1     
  Lines         932     1577      +645     
===========================================
+ Hits          783      924      +141     
- Misses        110      590      +480     
- Partials       39       63       +24     
Impacted Files Coverage Δ
chain.go 0.00% <ø> (-90.91%) ⬇️
interceptors/auth/auth.go 100.00% <ø> (ø)
interceptors/ratelimit/ratelimit.go 60.00% <0.00%> (-40.00%) ⬇️
interceptors/recovery/options.go 78.57% <ø> (ø)
metadata/single_key.go 60.00% <ø> (ø)
testing/testpb/interceptor_suite.go 0.00% <0.00%> (ø)
testing/testpb/test.manual_validator.pb.go 0.00% <0.00%> (ø)
util/backoffutils/backoff.go 60.00% <ø> (ø)
wrappers.go 66.66% <ø> (-33.34%) ⬇️
interceptors/reporter.go 45.45% <20.00%> (-17.05%) ⬇️
... and 25 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 72478fa...7e04286. Read the comment docs.

@XSAM XSAM force-pushed the refactor-tracing-interceptor branch from 03ffb9e to 8368b65 Compare August 8, 2021 15:55
@yashrsharma44 yashrsharma44 added this to In progress in Milestone v2 via automation Aug 13, 2021
@yashrsharma44
Copy link
Collaborator

Since #394 has been merged, there have been some changes in the structuring of tags(which are removed, replaced with logging.Fields) and tracing package has been removed.

Please rebase the changes.

Copy link
Collaborator

@yashrsharma44 yashrsharma44 left a comment

Choose a reason for hiding this comment

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

Looks good to me, I will have another round of review, particularly the kv package, seems like a lot of chunk for tracing is present there :)

interceptors/tracing/interceptors.go Outdated Show resolved Hide resolved
interceptors/tracing/reporter.go Outdated Show resolved Hide resolved
interceptors/tracing/reporter.go Outdated Show resolved Hide resolved
@XSAM XSAM force-pushed the refactor-tracing-interceptor branch from 8368b65 to 4a45a5b Compare August 25, 2021 08:27
@XSAM XSAM force-pushed the refactor-tracing-interceptor branch from 4a45a5b to d0650ba Compare August 25, 2021 09:26
Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice work! Did very quick pass with initial questions. Generally looks amazing! 💪🏽

// instead of calling kv.Key(name).Bool(value) consider using a
// convenience function provided by the api/key package -
// key.Bool(name, value).
func (k Key) Bool(v bool) KeyValue {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a fan of this package. Looks very low value to maintain another type system. Can we avoid it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OTel uses a type-safe way to describe the type of attributes, I am afraid we will lose type information if we don't implement a type system.

interceptors/tracing/reporter.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@yashrsharma44 yashrsharma44 left a comment

Choose a reason for hiding this comment

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

Everything looks good to me! Regarding the kv package, I don't have any solid suggestions, but do you think we can define the interface for the kv helper methods, and let the providers implement the kv implementation?

SpanKindClient SpanKind = "client"
)

func reportable(tracer Tracer) interceptors.CommonReportableFunc {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks neat!

interceptors/tracing/interceptors_test.go Outdated Show resolved Hide resolved
Co-authored-by: Yash Sharma <yashrsharma44@gmail.com>
@XSAM
Copy link
Contributor Author

XSAM commented Sep 1, 2021

do you think we can define the interface for the kv helper methods, and let the providers implement the kv implementation.

Sounds good. @bwplotka WDYT?

@bwplotka
Copy link
Collaborator

bwplotka commented Apr 6, 2022

Just doing pass on this, sorry for lag. Feel free to ping me on Go/CNCF slack next time. Do you wish to move forward with this @XSAM ? We need that (:

@XSAM
Copy link
Contributor Author

XSAM commented Apr 6, 2022

yeah, i can still work on this.

@bwplotka
Copy link
Collaborator

bwplotka commented Apr 6, 2022

Sweet, let me review this unless you want to change something

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Nice, some comment first.

Good work 💪🏽

s.statusMessage = message
}

func (s *mockSpan) AddEvent(name string, attrs ...kv.KeyValue) {
Copy link
Collaborator

@bwplotka bwplotka Apr 11, 2022

Choose a reason for hiding this comment

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

Suggested change
func (s *mockSpan) AddEvent(name string, attrs ...kv.KeyValue) {
func (s *mockSpan) AddEvent(name string, attrs ...any) {

How bad would be to operate on interfaces (any) and expect implementation to deal with types?

NOTE: I know we could use generics, but in practice they have still some overhead, plus not everyone moved to Go 1.18, so we can add generic type of implementation later on (maybe even in non braking compatibility manner)

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we would move to interfaces, we won't be needing kv package - Otel implementation can then use "go.opentelemetry.io/otel/attribute"

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

This looks amzing, LGTM. Just one comment. Do you have the OTLP side handy somewhere?

// The default span status is OK, so it is not necessary to
// explicitly set an OK status on successful Spans unless it
// is to add an OK message or to override a previous status on the Span.
SetStatus(code codes.Code, msg string)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Amazing idea to use gRPC codes here, love it!

s.statusMessage = message
}

func (s *mockSpan) AddEvent(name string, attrs ...tracing.KeyValue) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Idea: Remove kv and use keyvals ...any as in go-kit logger https://github.com/go-kit/kit/tree/master/log ? (:

Copy link
Collaborator

Choose a reason for hiding this comment

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

well ..interface{}, since we can't just support Go 1.18 yet

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then we can use something like

// borrowed from https://github.com/go-logfmt/logfmt/blob/main/encode.go#L75
func kvToAttr(keyvals ...interface{}) []attribute.KeyValue {
	if len(keyvals) == 0 {
		return nil
	}
	if len(keyvals)%2 == 1 {
		keyvals = append(keyvals, nil)
	}

	j := 0
	ret := make([]attribute.KeyValue, len(keyvals)/2)
	for i := 0; i < len(keyvals); i += 2 {
		k, ok := anyToString(keyvals[i])
		if !ok {
			k = "<unsupported key type>"
		}
		
		v, ok := anyToString(keyvals[i+1])
		if !ok {
			v = "<unsupported value type>"
		}
		
		ret[j] = attribute.String(k, v)
		j++
	}
	return ret
}

func anyToString(value interface{}) (string, bool) {
	switch v := value.(type) {
	case nil:
		return "nil", true
	case string:
		return v, true
	case []byte:
		return string(v), true
	case error:
		return v.Error(), true
	case fmt.Stringer:
		return v.String(), true
	default:
		rvalue := reflect.ValueOf(value)
		switch rvalue.Kind() {
		case reflect.Array, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Struct:
			return "", false
		case reflect.Ptr:
			if rvalue.IsNil() {
				return "nil", true
			}
			return anyToString(rvalue.Elem().Interface())
		}
		return fmt.Sprint(v), true
	}
}

@bwplotka
Copy link
Collaborator

Ping (:

Copy link
Collaborator

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Amazing, work thanks! Let's go!

@bwplotka bwplotka merged commit 83835dc into grpc-ecosystem:v2 Apr 25, 2022
Milestone v2 automation moved this from In progress to Done Apr 25, 2022
@XSAM XSAM deleted the refactor-tracing-interceptor branch April 26, 2022 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants