Skip to content

Commit

Permalink
feat(json-format): lowercase span name and service name as per swagge…
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs authored Aug 10, 2020
1 parent 6989a82 commit 6b3cdea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
29 changes: 24 additions & 5 deletions model/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,33 @@

package model

import "net"
import (
"encoding/json"
"net"
"strings"
)

// Endpoint holds the network context of a node in the service graph.
type Endpoint struct {
ServiceName string `json:"serviceName,omitempty"`
IPv4 net.IP `json:"ipv4,omitempty"`
IPv6 net.IP `json:"ipv6,omitempty"`
Port uint16 `json:"port,omitempty"`
ServiceName string
IPv4 net.IP
IPv6 net.IP
Port uint16
}

// MarshalJSON exports our Endpoint into the correct format for the Zipkin V2 API.
func (e Endpoint) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ServiceName string `json:"serviceName,omitempty"`
IPv4 net.IP `json:"ipv4,omitempty"`
IPv6 net.IP `json:"ipv6,omitempty"`
Port uint16 `json:"port,omitempty"`
}{
strings.ToLower(e.ServiceName),
e.IPv4,
e.IPv6,
e.Port,
})
}

// Empty returns if all Endpoint properties are empty / unspecified.
Expand Down
3 changes: 3 additions & 0 deletions model/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package model
import (
"encoding/json"
"errors"
"strings"
"time"
)

Expand Down Expand Up @@ -87,6 +88,8 @@ func (s SpanModel) MarshalJSON() ([]byte, error) {
s.Duration += 500 * time.Nanosecond
}

s.Name = strings.ToLower(s.Name)

if s.LocalEndpoint.Empty() {
s.LocalEndpoint = nil
}
Expand Down
4 changes: 4 additions & 0 deletions model/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net"
"reflect"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -84,6 +85,9 @@ func TestSpanJSON(t *testing.T) {
span1.Annotations[idx].Timestamp = span1.Annotations[idx].Timestamp.Round(time.Microsecond)
}

span1.Name = strings.ToLower(span1.Name)
span1.LocalEndpoint.ServiceName = strings.ToLower(span1.LocalEndpoint.ServiceName)

if !reflect.DeepEqual(span1, span2) {
t.Errorf("want SpanModel: %+v, have: %+v", span1, span2)
}
Expand Down

0 comments on commit 6b3cdea

Please sign in to comment.