fix golint issues in core/utils (#520)

* fix golint issues in core/utils

* fix golint issues in core/trace

* fix golint issues in core/trace
This commit is contained in:
Kevin Wan
2021-02-26 16:20:47 +08:00
committed by GitHub
parent 2087ac1e89
commit f309e9f80c
8 changed files with 27 additions and 1 deletions

View File

@@ -6,9 +6,11 @@ import (
"strings" "strings"
) )
// ErrInvalidCarrier indicates an error that the carrier is invalid.
var ErrInvalidCarrier = errors.New("invalid carrier") var ErrInvalidCarrier = errors.New("invalid carrier")
type ( type (
// Carrier interface wraps the Get and Set method.
Carrier interface { Carrier interface {
Get(key string) string Get(key string) string
Set(key, value string) Set(key, value string)

View File

@@ -7,7 +7,9 @@ import (
) )
const ( const (
// HttpFormat means http carrier format.
HttpFormat = iota HttpFormat = iota
// GrpcFormat means grpc carrier format.
GrpcFormat GrpcFormat
) )
@@ -17,6 +19,7 @@ var (
) )
type ( type (
// Propagator interface wraps the Extract and Inject methods.
Propagator interface { Propagator interface {
Extract(carrier interface{}) (Carrier, error) Extract(carrier interface{}) (Carrier, error)
Inject(carrier interface{}) (Carrier, error) Inject(carrier interface{}) (Carrier, error)
@@ -58,6 +61,7 @@ func (g grpcPropagator) Inject(carrier interface{}) (Carrier, error) {
return nil, ErrInvalidCarrier return nil, ErrInvalidCarrier
} }
// Extract extracts tracing information from carrier with given format.
func Extract(format, carrier interface{}) (Carrier, error) { func Extract(format, carrier interface{}) (Carrier, error) {
switch v := format.(type) { switch v := format.(type) {
case int: case int:
@@ -71,6 +75,7 @@ func Extract(format, carrier interface{}) (Carrier, error) {
return nil, ErrInvalidCarrier return nil, ErrInvalidCarrier
} }
// Inject injects tracing information into carrier with given format.
func Inject(format, carrier interface{}) (Carrier, error) { func Inject(format, carrier interface{}) (Carrier, error) {
switch v := format.(type) { switch v := format.(type) {
case int: case int:

View File

@@ -21,6 +21,7 @@ const (
var spanSep = string([]byte{spanSepRune}) var spanSep = string([]byte{spanSepRune})
// A Span is a calling span that connects caller and callee.
type Span struct { type Span struct {
ctx spanContext ctx spanContext
serviceName string serviceName string
@@ -58,9 +59,11 @@ func newServerSpan(carrier Carrier, serviceName, operationName string) tracespec
} }
} }
// Finish finishes the calling span.
func (s *Span) Finish() { func (s *Span) Finish() {
} }
// Follow follows the tracing service and operation names in context.
func (s *Span) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) { func (s *Span) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
span := &Span{ span := &Span{
ctx: spanContext{ ctx: spanContext{
@@ -75,6 +78,7 @@ func (s *Span) Follow(ctx context.Context, serviceName, operationName string) (c
return context.WithValue(ctx, tracespec.TracingKey, span), span return context.WithValue(ctx, tracespec.TracingKey, span), span
} }
// Fork forks the tracing service and operation names in context.
func (s *Span) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) { func (s *Span) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
span := &Span{ span := &Span{
ctx: spanContext{ ctx: spanContext{
@@ -89,14 +93,17 @@ func (s *Span) Fork(ctx context.Context, serviceName, operationName string) (con
return context.WithValue(ctx, tracespec.TracingKey, span), span return context.WithValue(ctx, tracespec.TracingKey, span), span
} }
// SpanId returns the span id.
func (s *Span) SpanId() string { func (s *Span) SpanId() string {
return s.ctx.SpanId() return s.ctx.SpanId()
} }
// TraceId returns the trace id.
func (s *Span) TraceId() string { func (s *Span) TraceId() string {
return s.ctx.TraceId() return s.ctx.TraceId()
} }
// Visit visits the span using fn.
func (s *Span) Visit(fn func(key, val string) bool) { func (s *Span) Visit(fn func(key, val string) bool) {
s.ctx.Visit(fn) s.ctx.Visit(fn)
} }
@@ -126,6 +133,7 @@ func (s *Span) followSpanId() string {
return strings.Join(fields, spanSep) return strings.Join(fields, spanSep)
} }
// StartClientSpan starts the client span with given context, service and operation names.
func StartClientSpan(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) { func StartClientSpan(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
if span, ok := ctx.Value(tracespec.TracingKey).(*Span); ok { if span, ok := ctx.Value(tracespec.TracingKey).(*Span); ok {
return span.Fork(ctx, serviceName, operationName) return span.Fork(ctx, serviceName, operationName)
@@ -134,6 +142,7 @@ func StartClientSpan(ctx context.Context, serviceName, operationName string) (co
return ctx, emptyNoopSpan return ctx, emptyNoopSpan
} }
// StartServerSpan starts the server span with given context, carrier, service and operation names.
func StartServerSpan(ctx context.Context, carrier Carrier, serviceName, operationName string) ( func StartServerSpan(ctx context.Context, carrier Carrier, serviceName, operationName string) (
context.Context, tracespec.Trace) { context.Context, tracespec.Trace) {
span := newServerSpan(carrier, serviceName, operationName) span := newServerSpan(carrier, serviceName, operationName)

View File

@@ -1,5 +1,6 @@
package tracespec package tracespec
// SpanContext interface that represents a span context.
type SpanContext interface { type SpanContext interface {
TraceId() string TraceId() string
SpanId() string SpanId() string

View File

@@ -2,6 +2,7 @@ package tracespec
import "context" import "context"
// Trace interface represents a tracing.
type Trace interface { type Trace interface {
SpanContext SpanContext
Finish() Finish()

View File

@@ -7,32 +7,39 @@ import (
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
) )
// A ElapsedTimer is a timer to track the elapsed time.
type ElapsedTimer struct { type ElapsedTimer struct {
start time.Duration start time.Duration
} }
// NewElapsedTimer returns a ElapsedTimer.
func NewElapsedTimer() *ElapsedTimer { func NewElapsedTimer() *ElapsedTimer {
return &ElapsedTimer{ return &ElapsedTimer{
start: timex.Now(), start: timex.Now(),
} }
} }
// Duration returns the elapsed time.
func (et *ElapsedTimer) Duration() time.Duration { func (et *ElapsedTimer) Duration() time.Duration {
return timex.Since(et.start) return timex.Since(et.start)
} }
// Elapsed returns the string representation of elapsed time.
func (et *ElapsedTimer) Elapsed() string { func (et *ElapsedTimer) Elapsed() string {
return timex.Since(et.start).String() return timex.Since(et.start).String()
} }
// ElapsedMs returns the elapsed time of string on milliseconds.
func (et *ElapsedTimer) ElapsedMs() string { func (et *ElapsedTimer) ElapsedMs() string {
return fmt.Sprintf("%.1fms", float32(timex.Since(et.start))/float32(time.Millisecond)) return fmt.Sprintf("%.1fms", float32(timex.Since(et.start))/float32(time.Millisecond))
} }
// CurrentMicros returns the current microseconds.
func CurrentMicros() int64 { func CurrentMicros() int64 {
return time.Now().UnixNano() / int64(time.Microsecond) return time.Now().UnixNano() / int64(time.Microsecond)
} }
// CurrentMillis returns the current milliseconds.
func CurrentMillis() int64 { func CurrentMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond) return time.Now().UnixNano() / int64(time.Millisecond)
} }

View File

@@ -2,6 +2,7 @@ package utils
import "github.com/google/uuid" import "github.com/google/uuid"
// NewUuid returns a uuid string.
func NewUuid() string { func NewUuid() string {
return uuid.New().String() return uuid.New().String()
} }

View File

@@ -14,7 +14,7 @@ var replacer = stringx.NewReplacer(map[string]string{
"-": ".", "-": ".",
}) })
// operator compare returns true if the first field and the third field equation holds else false // CompareVersions returns true if the first field and the third field are equal, otherwise false.
func CompareVersions(v1, op, v2 string) bool { func CompareVersions(v1, op, v2 string) bool {
result := compare(v1, v2) result := compare(v1, v2)
switch op { switch op {