-
Notifications
You must be signed in to change notification settings - Fork 917
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
Ftr:otel trace #1886
Merged
Merged
Ftr:otel trace #1886
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43e2705
otel trace (#1774)
pherzheyu 5e12eb3
metdata use map (#1774)
pherzheyu bda51ce
metadata update
pherzheyu 1d4bc71
otel version use dubbo version
pherzheyu 8424de3
update version ut
pherzheyu 794059a
update version ut
pherzheyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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" | ||
) | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/baggage" | ||
|
||
"go.opentelemetry.io/otel/propagation" | ||
|
||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type metadataSupplier struct { | ||
metadata *map[string]interface{} | ||
} | ||
|
||
var _ propagation.TextMapCarrier = &metadataSupplier{} | ||
|
||
func (s *metadataSupplier) Get(key string) string { | ||
AlexStocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if s.metadata == nil { | ||
return "" | ||
} | ||
item, ok := (*s.metadata)[key] | ||
if !ok { | ||
return "" | ||
} | ||
value, ok := item.([]string) | ||
if !ok { | ||
return "" | ||
} | ||
if len(value) == 0 { | ||
return "" | ||
} | ||
return value[0] | ||
} | ||
|
||
func (s *metadataSupplier) Set(key string, value string) { | ||
if s.metadata == nil { | ||
s.metadata = &map[string]interface{}{} | ||
} | ||
(*s.metadata)[key] = value | ||
} | ||
|
||
func (s *metadataSupplier) Keys() []string { | ||
out := make([]string, 0, len(*s.metadata)) | ||
for key := range *s.metadata { | ||
out = append(out, key) | ||
} | ||
return out | ||
} | ||
|
||
// Inject injects correlation context and span context into the dubbo | ||
// metadata object. This function is meant to be used on outgoing | ||
// requests. | ||
func Inject(ctx context.Context, metadata *map[string]interface{}, propagators propagation.TextMapPropagator) { | ||
propagators.Inject(ctx, &metadataSupplier{ | ||
metadata: metadata, | ||
}) | ||
} | ||
|
||
// Extract returns the correlation context and span context that | ||
// another service encoded in the dubbo metadata object with Inject. | ||
// This function is meant to be used on incoming requests. | ||
func Extract(ctx context.Context, metadata *map[string]interface{}, propagators propagation.TextMapPropagator) (baggage.Baggage, trace.SpanContext) { | ||
ctx = propagators.Extract(ctx, &metadataSupplier{ | ||
metadata: metadata, | ||
}) | ||
return baggage.FromContext(ctx), trace.SpanContextFromContext(ctx) | ||
} |
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,131 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_metadataSupplier_Keys(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metadata *map[string]interface{} | ||
want []string | ||
}{ | ||
{ | ||
name: "test", | ||
metadata: &map[string]interface{}{ | ||
"key1": nil, | ||
}, | ||
want: []string{"key1"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &metadataSupplier{ | ||
metadata: tt.metadata, | ||
} | ||
if got := s.Keys(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Keys() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_metadataSupplier_Set(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metadata *map[string]interface{} | ||
key string | ||
value string | ||
}{ | ||
{ | ||
name: "test", | ||
metadata: nil, | ||
key: "key", | ||
value: "value", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &metadataSupplier{ | ||
metadata: tt.metadata, | ||
} | ||
s.Set(tt.key, tt.value) | ||
}) | ||
} | ||
} | ||
|
||
func Test_metadataSupplier_Get(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
metadata *map[string]interface{} | ||
key string | ||
want string | ||
}{ | ||
{ | ||
name: "nil metadata", | ||
metadata: nil, | ||
key: "key", | ||
want: "", | ||
}, | ||
{ | ||
name: "not exist", | ||
metadata: &map[string]interface{}{ | ||
"k": nil, | ||
}, | ||
key: "key", | ||
want: "", | ||
}, | ||
{ | ||
name: "nil slice", | ||
metadata: &map[string]interface{}{ | ||
"key": nil, | ||
}, | ||
key: "key", | ||
want: "", | ||
}, | ||
{ | ||
name: "empty slice", | ||
metadata: &map[string]interface{}{ | ||
"key": []string{}, | ||
}, | ||
key: "key", | ||
want: "", | ||
}, | ||
{ | ||
name: "test", | ||
metadata: &map[string]interface{}{ | ||
"key": []string{"test"}, | ||
}, | ||
key: "key", | ||
want: "test", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &metadataSupplier{ | ||
metadata: tt.metadata, | ||
} | ||
if got := s.Get(tt.key); got != tt.want { | ||
t.Errorf("Get() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,20 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 instruments dubbogo with open-telemetry | ||
// (https://github.com/open-telemetry/opentelemetry-go). | ||
package trace |
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,141 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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" | ||
) | ||
|
||
import ( | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/baggage" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/propagation" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.7.0" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
import ( | ||
"dubbo.apache.org/dubbo-go/v3/common/constant" | ||
"dubbo.apache.org/dubbo-go/v3/common/extension" | ||
"dubbo.apache.org/dubbo-go/v3/filter" | ||
"dubbo.apache.org/dubbo-go/v3/protocol" | ||
) | ||
|
||
const ( | ||
instrumentationName = "dubbo.apache.org/dubbo-go/v3/oteldubbo" | ||
) | ||
|
||
func init() { | ||
extension.SetFilter(constant.OTELServerTraceKey, func() filter.Filter { | ||
return &otelServerFilter{ | ||
Propagators: otel.GetTextMapPropagator(), | ||
TracerProvider: otel.GetTracerProvider(), | ||
} | ||
}) | ||
extension.SetFilter(constant.OTELClientTraceKey, func() filter.Filter { | ||
return &otelClientFilter{ | ||
Propagators: otel.GetTextMapPropagator(), | ||
TracerProvider: otel.GetTracerProvider(), | ||
} | ||
}) | ||
} | ||
|
||
type otelServerFilter struct { | ||
Propagators propagation.TextMapPropagator | ||
TracerProvider trace.TracerProvider | ||
} | ||
|
||
func (f *otelServerFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, protocol protocol.Invocation) protocol.Result { | ||
return result | ||
} | ||
|
||
func (f *otelServerFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
attachments := invocation.Attachments() | ||
bags, spanCtx := Extract(ctx, &attachments, f.Propagators) | ||
ctx = baggage.ContextWithBaggage(ctx, bags) | ||
|
||
tracer := f.TracerProvider.Tracer( | ||
instrumentationName, | ||
trace.WithInstrumentationVersion(SemVersion()), | ||
) | ||
|
||
ctx, span := tracer.Start( | ||
trace.ContextWithRemoteSpanContext(ctx, spanCtx), | ||
invocation.ActualMethodName(), | ||
trace.WithSpanKind(trace.SpanKindServer), | ||
trace.WithAttributes( | ||
RPCSystemDubbo, | ||
semconv.RPCServiceKey.String(invoker.GetURL().ServiceKey()), | ||
semconv.RPCMethodKey.String(invocation.MethodName()), | ||
), | ||
) | ||
defer span.End() | ||
|
||
result := invoker.Invoke(ctx, invocation) | ||
|
||
if result.Error() != nil { | ||
span.SetStatus(codes.Error, result.Error().Error()) | ||
} else { | ||
span.SetStatus(codes.Ok, codes.Ok.String()) | ||
} | ||
return result | ||
} | ||
|
||
type otelClientFilter struct { | ||
Propagators propagation.TextMapPropagator | ||
TracerProvider trace.TracerProvider | ||
} | ||
|
||
func (f *otelClientFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, protocol protocol.Invocation) protocol.Result { | ||
return result | ||
} | ||
|
||
func (f *otelClientFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
tracer := f.TracerProvider.Tracer( | ||
instrumentationName, | ||
trace.WithInstrumentationVersion(SemVersion()), | ||
) | ||
|
||
var span trace.Span | ||
ctx, span = tracer.Start( | ||
ctx, | ||
invocation.ActualMethodName(), | ||
trace.WithSpanKind(trace.SpanKindClient), | ||
trace.WithAttributes( | ||
RPCSystemDubbo, | ||
semconv.RPCServiceKey.String(invoker.GetURL().ServiceKey()), | ||
semconv.RPCMethodKey.String(invocation.MethodName()), | ||
), | ||
) | ||
defer span.End() | ||
|
||
attachments := make(map[string]interface{}) | ||
Inject(ctx, &(attachments), f.Propagators) | ||
for k, v := range attachments { | ||
invocation.SetAttachment(k, v) | ||
} | ||
result := invoker.Invoke(ctx, invocation) | ||
|
||
if result.Error() != nil { | ||
span.SetStatus(codes.Error, result.Error().Error()) | ||
} else { | ||
span.SetStatus(codes.Ok, codes.Ok.String()) | ||
} | ||
return result | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个为啥用指针,map 应该本来就是个指针吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map是个引用类型,和指针还是有区别的,当触发map扩容时,引用地址可能会变