Skip to content

Commit

Permalink
fix golint issues (zeromicro#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Sep 4, 2021
1 parent 20f665e commit 9bdadf2
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 77 deletions.
33 changes: 14 additions & 19 deletions core/opentelemetry/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,38 @@ package opentelemetry
import (
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
grpc_codes "google.golang.org/grpc/codes"
gcodes "google.golang.org/grpc/codes"
)

const (
// GRPCStatusCodeKey is convention for numeric status code of a gRPC request.
GRPCStatusCodeKey = attribute.Key("rpc.grpc.status_code")

// Name of message transmitted or received.
// RPCNameKey is the name of message transmitted or received.
RPCNameKey = attribute.Key("name")

// Type of message transmitted or received.
// RPCMessageTypeKey is the type of message transmitted or received.
RPCMessageTypeKey = attribute.Key("message.type")

// Identifier of message transmitted or received.
// RPCMessageIDKey is the identifier of message transmitted or received.
RPCMessageIDKey = attribute.Key("message.id")

// The compressed size of the message transmitted or received in bytes.
// RPCMessageCompressedSizeKey is the compressed size of the message transmitted or received in bytes.
RPCMessageCompressedSizeKey = attribute.Key("message.compressed_size")

// The uncompressed size of the message transmitted or received in
// bytes.
// RPCMessageUncompressedSizeKey is the uncompressed size of the message
// transmitted or received in bytes.
RPCMessageUncompressedSizeKey = attribute.Key("message.uncompressed_size")
)

// Semantic conventions for common RPC attributes.
var (
// Semantic convention for gRPC as the remoting system.
// RPCSystemGRPC is the semantic convention for gRPC as the remoting system.
RPCSystemGRPC = semconv.RPCSystemKey.String("grpc")

// Semantic convention for a message named message.
// RPCNameMessage is the semantic convention for a message named message.
RPCNameMessage = RPCNameKey.String("message")

// Semantic conventions for RPC message types.
RPCMessageTypeSent = RPCMessageTypeKey.String("SENT")
// RPCMessageTypeSent is the semantic conventions for sent RPC message types.
RPCMessageTypeSent = RPCMessageTypeKey.String("SENT")
// RPCMessageTypeReceived is the semantic conventions for the received RPC message types.
RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED")
)

func StatusCodeAttr(c grpc_codes.Code) attribute.KeyValue {
// StatusCodeAttr returns a attribute.KeyValue that represents the give c.
func StatusCodeAttr(c gcodes.Code) attribute.KeyValue {
return GRPCStatusCodeKey.Int64(int64(c))
}
18 changes: 8 additions & 10 deletions core/opentelemetry/clientstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
"google.golang.org/protobuf/proto"
)

const (
receiveEndEvent streamEventType = iota
errorEvent
)

var _ = proto.Marshal

type streamEventType int

type streamEvent struct {
Type streamEventType
Err error
}

const (
receiveEndEvent streamEventType = iota
errorEvent
)

type clientStream struct {
grpc.ClientStream

Expand All @@ -33,11 +35,8 @@ type clientStream struct {
sentMessageID int
}

var _ = proto.Marshal

func (w *clientStream) RecvMsg(m interface{}) error {
err := w.ClientStream.RecvMsg(m)

if err == nil && !w.desc.ServerStreams {
w.sendStreamEvent(receiveEndEvent, nil)
} else if err == io.EOF {
Expand All @@ -54,10 +53,8 @@ func (w *clientStream) RecvMsg(m interface{}) error {

func (w *clientStream) SendMsg(m interface{}) error {
err := w.ClientStream.SendMsg(m)

w.sentMessageID++
MessageSent.Event(w.Context(), w.sentMessageID, m)

if err != nil {
w.sendStreamEvent(errorEvent, err)
}
Expand Down Expand Up @@ -90,6 +87,7 @@ func (w *clientStream) sendStreamEvent(eventType streamEventType, err error) {
}
}

// WrapClientStream wraps s with given ctx and desc.
func WrapClientStream(ctx context.Context, s grpc.ClientStream, desc *grpc.StreamDesc) *clientStream {
events := make(chan streamEvent)
eventsDone := make(chan struct{})
Expand Down
1 change: 1 addition & 0 deletions core/opentelemetry/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package opentelemetry

// TraceName represents the tracing name.
const TraceName = "go-zero"

// A Config is a opentelemetry config.
Expand Down
4 changes: 3 additions & 1 deletion core/opentelemetry/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

var (
MessageSent = messageType(RPCMessageTypeSent)
// MessageSent is the type of sent messages.
MessageSent = messageType(RPCMessageTypeSent)
// MessageReceived is the type of received messages.
MessageReceived = messageType(RPCMessageTypeReceived)
)

Expand Down
1 change: 1 addition & 0 deletions core/opentelemetry/serverstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (w *serverStream) SendMsg(m interface{}) error {
return err
}

// WrapServerStream wraps the given grpc.ServerStream with the given context.
func WrapServerStream(ctx context.Context, ss grpc.ServerStream) *serverStream {
return &serverStream{
ServerStream: ss,
Expand Down
2 changes: 2 additions & 0 deletions core/opentelemetry/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ func (s *metadataSupplier) Keys() []string {
return out
}

// Inject injects the metadata into ctx.
func Inject(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) {
p.Inject(ctx, &metadataSupplier{
metadata: metadata,
})
}

// Extract extracts the metadata from ctx.
func Extract(ctx context.Context, p propagation.TextMapPropagator, metadata *metadata.MD) (baggage.Baggage, trace.SpanContext) {
ctx = p.Extract(ctx, &metadataSupplier{
metadata: metadata,
Expand Down
6 changes: 6 additions & 0 deletions core/opentelemetry/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import (
"google.golang.org/grpc/peer"
)

// PeerFromCtx returns the peer from ctx.
func PeerFromCtx(ctx context.Context) string {
p, ok := peer.FromContext(ctx)
if !ok {
return ""
}

return p.Addr.String()
}

// SpanInfo returns the span info.
func SpanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
attrs := []attribute.KeyValue{RPCSystemGRPC}
name, mAttrs := ParseFullMethod(fullMethod)
Expand All @@ -26,6 +29,7 @@ func SpanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
return name, attrs
}

// ParseFullMethod returns the method name and attributes.
func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
name := strings.TrimLeft(fullMethod, "/")
parts := strings.SplitN(name, "/", 2)
Expand All @@ -41,9 +45,11 @@ func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
if method := parts[1]; method != "" {
attrs = append(attrs, semconv.RPCMethodKey.String(method))
}

return name, attrs
}

// PeerAttr returns the peer attributes.
func PeerAttr(addr string) []attribute.KeyValue {
host, port, err := net.SplitHostPort(addr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions core/proc/signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
}()
}

// Done returns the channel that notifies the process quitting.
func Done() <-chan struct{} {
return done
}
1 change: 1 addition & 0 deletions core/trace/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package trace

const (
// TraceIdKey is the trace id header.
TraceIdKey = "X-Trace-ID"

spanIdKey = "X-Span-ID"
Expand Down
1 change: 1 addition & 0 deletions rest/handler/otelhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
oteltrace "go.opentelemetry.io/otel/trace"
)

// OtelHandler return a middleware that process the opentelemetry.
func OtelHandler(path string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
if !opentelemetry.Enabled() {
Expand Down
35 changes: 12 additions & 23 deletions zrpc/internal/clientinterceptors/opentracinginterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"google.golang.org/grpc/status"
)

// OpenTracingInterceptor returns a grpc.UnaryClientInterceptor for opentelemetry.
func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
propagator := otel.GetTextMapPropagator()
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if !opentelemetry.Enabled() {
return invoker(ctx, method, req, reply, cc, opts...)
}
Expand All @@ -24,13 +26,8 @@ func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
metadataCopy := requestMetadata.Copy()
tr := otel.Tracer(opentelemetry.TraceName)
name, attr := opentelemetry.SpanInfo(method, cc.Target())

var span trace.Span
ctx, span = tr.Start(ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attr...),
)
ctx, span := tr.Start(ctx, name, trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attr...))
defer span.End()

opentelemetry.Inject(ctx, propagator, &metadataCopy)
Expand All @@ -50,30 +47,23 @@ func OpenTracingInterceptor() grpc.UnaryClientInterceptor {
}
}

// StreamOpenTracingInterceptor returns a grpc.StreamClientInterceptor for opentelemetry.
func StreamOpenTracingInterceptor() grpc.StreamClientInterceptor {
propagator := otel.GetTextMapPropagator()
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
if !opentelemetry.Enabled() {
return streamer(ctx, desc, cc, method, opts...)
}

requestMetadata, _ := metadata.FromOutgoingContext(ctx)
metadataCopy := requestMetadata.Copy()

tr := otel.Tracer(opentelemetry.TraceName)

name, attr := opentelemetry.SpanInfo(method, cc.Target())
var span trace.Span
ctx, span = tr.Start(
ctx,
name,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attr...),
)

ctx, span := tr.Start(ctx, name, trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attr...))
opentelemetry.Inject(ctx, propagator, &metadataCopy)
ctx = metadata.NewOutgoingContext(ctx, metadataCopy)

s, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
grpcStatus, _ := status.FromError(err)
Expand All @@ -82,12 +72,11 @@ func StreamOpenTracingInterceptor() grpc.StreamClientInterceptor {
span.End()
return s, err
}

stream := opentelemetry.WrapClientStream(ctx, s, desc)

go func() {
err := <-stream.Finished

if err != nil {
if err := <-stream.Finished; err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(opentelemetry.StatusCodeAttr(s.Code()))
Expand Down
6 changes: 6 additions & 0 deletions zrpc/internal/resolver/kube/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import (
v1 "k8s.io/api/core/v1"
)

// EventHandler is ResourceEventHandler implementation.
type EventHandler struct {
update func([]string)
endpoints map[string]lang.PlaceholderType
lock sync.Mutex
}

// NewEventHandler returns an EventHandler.
func NewEventHandler(update func([]string)) *EventHandler {
return &EventHandler{
update: update,
endpoints: make(map[string]lang.PlaceholderType),
}
}

// OnAdd handles the endpoints add events.
func (h *EventHandler) OnAdd(obj interface{}) {
endpoints, ok := obj.(*v1.Endpoints)
if !ok {
Expand All @@ -46,6 +49,7 @@ func (h *EventHandler) OnAdd(obj interface{}) {
}
}

// OnDelete handles the endpoints delete events.
func (h *EventHandler) OnDelete(obj interface{}) {
endpoints, ok := obj.(*v1.Endpoints)
if !ok {
Expand All @@ -71,6 +75,7 @@ func (h *EventHandler) OnDelete(obj interface{}) {
}
}

// OnUpdate handles the endpoints update events.
func (h *EventHandler) OnUpdate(oldObj, newObj interface{}) {
oldEndpoints, ok := oldObj.(*v1.Endpoints)
if !ok {
Expand All @@ -91,6 +96,7 @@ func (h *EventHandler) OnUpdate(oldObj, newObj interface{}) {
h.Update(newEndpoints)
}

// Update updates the endpoints.
func (h *EventHandler) Update(endpoints *v1.Endpoints) {
h.lock.Lock()
defer h.lock.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions zrpc/internal/resolver/kube/targetparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ const (

var emptyService Service

// Service represents a service with namespace, name and port.
type Service struct {
Namespace string
Name string
Port int
}

// ParseTarget parses the resolver.Target.
func ParseTarget(target resolver.Target) (Service, error) {
var service Service
service.Namespace = target.Authority
Expand Down
Loading

0 comments on commit 9bdadf2

Please sign in to comment.