endpoint.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2022 The OpenZipkin Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package model
  15. import (
  16. "encoding/json"
  17. "net"
  18. "strings"
  19. )
  20. // Endpoint holds the network context of a node in the service graph.
  21. type Endpoint struct {
  22. ServiceName string
  23. IPv4 net.IP
  24. IPv6 net.IP
  25. Port uint16
  26. }
  27. // MarshalJSON exports our Endpoint into the correct format for the Zipkin V2 API.
  28. func (e Endpoint) MarshalJSON() ([]byte, error) {
  29. return json.Marshal(&struct {
  30. ServiceName string `json:"serviceName,omitempty"`
  31. IPv4 net.IP `json:"ipv4,omitempty"`
  32. IPv6 net.IP `json:"ipv6,omitempty"`
  33. Port uint16 `json:"port,omitempty"`
  34. }{
  35. strings.ToLower(e.ServiceName),
  36. e.IPv4,
  37. e.IPv6,
  38. e.Port,
  39. })
  40. }
  41. // Empty returns if all Endpoint properties are empty / unspecified.
  42. func (e *Endpoint) Empty() bool {
  43. return e == nil ||
  44. (e.ServiceName == "" && e.Port == 0 && len(e.IPv4) == 0 && len(e.IPv6) == 0)
  45. }