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

Redefine model/ and api_v2/ types as aliases to jaeger-idl/ types #6602

Merged
merged 11 commits into from
Jan 24, 2025
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jaegertracing/jaeger-idl v0.0.0-20250122172554-3e525d710892
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/ionos-cloud/sdk-go/v6 v6.1.11 h1:J/uRN4UWO3wCyGOeDdMKv8LWRzKu6UIkLEaes38Kzh8=
github.com/ionos-cloud/sdk-go/v6 v6.1.11/go.mod h1:EzEgRIDxBELvfoa/uBN0kOQaqovLjUWEB7iW4/Q+t4k=
github.com/jaegertracing/jaeger-idl v0.0.0-20250122172554-3e525d710892 h1:wDzNPLnnHOGCvGpLGn78gPo6Cqxpdil5TkS9dKfT2cE=
github.com/jaegertracing/jaeger-idl v0.0.0-20250122172554-3e525d710892/go.mod h1:TimiEKGlMAcMZsKSHJkXNo0I0Q39iaENPswLXA1I2XY=
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo=
Expand Down
9 changes: 0 additions & 9 deletions model/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ const (
// JaegerDependencyLinkSource describes a dependency diagram that was generated from Jaeger traces.
JaegerDependencyLinkSource = "jaeger"
)

// ApplyDefaults applies defaults to the DependencyLink.
func (d DependencyLink) ApplyDefaults() DependencyLink {
dd := d
if dd.Source == "" {
dd.Source = JaegerDependencyLinkSource
}
return dd
}
48 changes: 5 additions & 43 deletions model/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

package model

import (
jaegerIdlModel "github.com/jaegertracing/jaeger-idl/model/v1"
)

const (
// SampledFlag is the bit set in Flags in order to define a span as a sampled span
SampledFlag = Flags(1)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -14,46 +18,4 @@ const (
)

// Flags is a bit map of flags for a span
type Flags uint32

// ------- Flags -------

// SetSampled sets the Flags as sampled
func (f *Flags) SetSampled() {
f.setFlags(SampledFlag)
}

// SetDebug set the Flags as sampled
func (f *Flags) SetDebug() {
f.setFlags(DebugFlag)
}

// SetFirehose set the Flags as firehose enabled
func (f *Flags) SetFirehose() {
f.setFlags(FirehoseFlag)
}

func (f *Flags) setFlags(bit Flags) {
*f |= bit
}

// IsSampled returns true if the Flags denote sampling
func (f Flags) IsSampled() bool {
return f.checkFlags(SampledFlag)
}

// IsDebug returns true if the Flags denote debugging
// Debugging can be useful in testing tracing availability or correctness
func (f Flags) IsDebug() bool {
return f.checkFlags(DebugFlag)
}

// IsFirehoseEnabled returns true if firehose is enabled
// Firehose is used to decide whether to index a span or not
func (f Flags) IsFirehoseEnabled() bool {
return f.checkFlags(FirehoseFlag)
}

func (f Flags) checkFlags(bit Flags) bool {
return f&bit == bit
}
type Flags = jaegerIdlModel.Flags
7 changes: 3 additions & 4 deletions model/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ package model

import (
"hash/fnv"
"io"

jaegerIdlModel "github.com/jaegertracing/jaeger-idl/model/v1"
)

// Hashable interface is for type that can participate in a hash computation
// by writing their data into io.Writer, which is usually an instance of hash.Hash.
type Hashable interface {
Hash(w io.Writer) error
}
type Hashable = jaegerIdlModel.Hashable

// HashCode calculates a FNV-1a hash code for a Hashable object.
func HashCode(o Hashable) (uint64, error) {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
152 changes: 3 additions & 149 deletions model/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
package model

import (
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"strconv"

"github.com/gogo/protobuf/jsonpb"
jaegerIdlModel "github.com/jaegertracing/jaeger-idl/model/v1"
)

const (
Expand All @@ -22,13 +21,10 @@ const (
)

// TraceID is a random 128bit identifier for a trace
type TraceID struct {
Low uint64 `json:"lo"`
High uint64 `json:"hi"`
}
type TraceID = jaegerIdlModel.TraceID

// SpanID is a random 64bit identifier for a span
type SpanID uint64
type SpanID = jaegerIdlModel.SpanID

// ------- TraceID -------

Expand All @@ -37,13 +33,6 @@ func NewTraceID(high, low uint64) TraceID {
return TraceID{High: high, Low: low}
}

func (t TraceID) String() string {
if t.High == 0 {
return fmt.Sprintf("%016x", t.Low)
}
return fmt.Sprintf("%016x%016x", t.High, t.Low)
}

// TraceIDFromString creates a TraceID from a hexadecimal string
func TraceIDFromString(s string) (TraceID, error) {
var hi, lo uint64
Expand Down Expand Up @@ -82,80 +71,13 @@ func TraceIDFromBytes(data []byte) (TraceID, error) {
return t, nil
}

// MarshalText is called by encoding/json, which we do not want people to use.
func (TraceID) MarshalText() ([]byte, error) {
return nil, errors.New("unsupported method TraceID.MarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
}

// UnmarshalText is called by encoding/json, which we do not want people to use.
func (*TraceID) UnmarshalText([]byte /* text */) error {
return errors.New("unsupported method TraceID.UnmarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
}

// Size returns the size of this datum in protobuf. It is always 16 bytes.
func (*TraceID) Size() int {
return 16
}

// MarshalTo converts trace ID into a binary representation. Called by protobuf serialization.
func (t *TraceID) MarshalTo(data []byte) (n int, err error) {
var b [16]byte
binary.BigEndian.PutUint64(b[:8], uint64(t.High))
binary.BigEndian.PutUint64(b[8:], uint64(t.Low))
return marshalBytes(data, b[:])
}

// Unmarshal inflates this trace ID from binary representation. Called by protobuf serialization.
func (t *TraceID) Unmarshal(data []byte) error {
var err error
*t, err = TraceIDFromBytes(data)
return err
}

func marshalBytes(dst []byte, src []byte) (n int, err error) {
if len(dst) < len(src) {
return 0, errors.New("buffer is too short")
}
return copy(dst, src), nil
}

// MarshalJSON converts trace id into a base64 string enclosed in quotes.
// Used by protobuf JSON serialization.
// Example: {high:2, low:1} => "AAAAAAAAAAIAAAAAAAAAAQ==".
func (t TraceID) MarshalJSON() ([]byte, error) {
var b [16]byte
t.MarshalTo(b[:]) // can only error on incorrect buffer size
s := make([]byte, 24+2)
base64.StdEncoding.Encode(s[1:25], b[:])
s[0], s[25] = '"', '"'
return s, nil
}

// UnmarshalJSON inflates trace id from base64 string, possibly enclosed in quotes.
// Used by protobuf JSON serialization.
func (t *TraceID) UnmarshalJSON(data []byte) error {
s := string(data)
if l := len(s); l > 2 && s[0] == '"' && s[l-1] == '"' {
s = s[1 : l-1]
}
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return fmt.Errorf("cannot unmarshal TraceID from string '%s': %w", string(data), err)
}
return t.Unmarshal(b)
}

// ------- SpanID -------

// NewSpanID creates a new SpanID from a 64bit unsigned int.
func NewSpanID(v uint64) SpanID {
return SpanID(v)
}

func (s SpanID) String() string {
return fmt.Sprintf("%016x", uint64(s))
}

// SpanIDFromString creates a SpanID from a hexadecimal string
func SpanIDFromString(s string) (SpanID, error) {
if len(s) > 16 {
Expand All @@ -175,71 +97,3 @@ func SpanIDFromBytes(data []byte) (SpanID, error) {
}
return NewSpanID(binary.BigEndian.Uint64(data)), nil
}

// MarshalText is called by encoding/json, which we do not want people to use.
func (SpanID) MarshalText() ([]byte, error) {
return nil, errors.New("unsupported method SpanID.MarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
}

// UnmarshalText is called by encoding/json, which we do not want people to use.
func (*SpanID) UnmarshalText([]byte /* text */) error {
return errors.New("unsupported method SpanID.UnmarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
}

// Size returns the size of this datum in protobuf. It is always 8 bytes.
func (*SpanID) Size() int {
return 8
}

// MarshalTo converts span ID into a binary representation. Called by protobuf serialization.
func (s *SpanID) MarshalTo(data []byte) (n int, err error) {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(*s))
return marshalBytes(data, b[:])
}

// Unmarshal inflates span ID from a binary representation. Called by protobuf serialization.
func (s *SpanID) Unmarshal(data []byte) error {
var err error
*s, err = SpanIDFromBytes(data)
return err
}

// MarshalJSON converts span id into a base64 string enclosed in quotes.
// Used by protobuf JSON serialization.
// Example: {1} => "AAAAAAAAAAE=".
func (s SpanID) MarshalJSON() ([]byte, error) {
var b [8]byte
s.MarshalTo(b[:]) // can only error on incorrect buffer size
v := make([]byte, 12+2)
base64.StdEncoding.Encode(v[1:13], b[:])
v[0], v[13] = '"', '"'
return v, nil
}

// UnmarshalJSON inflates span id from base64 string, possibly enclosed in quotes.
// User by protobuf JSON serialization.
//
// There appears to be a bug in gogoproto, as this function is only called for numeric values.
// https://github.com/gogo/protobuf/issues/411#issuecomment-393856837
func (s *SpanID) UnmarshalJSON(data []byte) error {
str := string(data)
if l := len(str); l > 2 && str[0] == '"' && str[l-1] == '"' {
str = str[1 : l-1]
}
b, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return fmt.Errorf("cannot unmarshal SpanID from string '%s': %w", string(data), err)
}
return s.Unmarshal(b)
}

// UnmarshalJSONPB inflates span id from base64 string, possibly enclosed in quotes.
// User by protobuf JSON serialization.
//
// TODO: can be removed once this ticket is fixed:
//
// https://github.com/gogo/protobuf/issues/411#issuecomment-393856837
func (s *SpanID) UnmarshalJSONPB(_ *jsonpb.Unmarshaler, b []byte) error {
return s.UnmarshalJSON(b)
}
Loading
Loading