1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-20 03:30:02 +02:00

fix zipkin without local endpoint with service name (#644)

This commit is contained in:
Oleg Shatnyuk 2020-04-19 09:02:29 +03:00 committed by GitHub
parent 669d4b3a6c
commit cf7c4e5328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 118 additions and 86 deletions

View File

@ -35,6 +35,7 @@ func initTracer() {
// Create Zipkin Exporter // Create Zipkin Exporter
exporter, err := zipkin.NewExporter( exporter, err := zipkin.NewExporter(
"http://localhost:9411/api/v2/spans", "http://localhost:9411/api/v2/spans",
"zipkin-example",
zipkin.WithLogger(logger), zipkin.WithLogger(logger),
) )
if err != nil { if err != nil {

View File

@ -182,12 +182,14 @@ func TestExportSpans(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "SERVER", Kind: "SERVER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "exporter-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: nil, Annotations: nil,
Tags: map[string]string{ Tags: map[string]string{
@ -208,12 +210,14 @@ func TestExportSpans(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "bar", Name: "bar",
Kind: "SERVER", Kind: "SERVER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 15, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 15, 0, time.UTC),
Duration: 30 * time.Second, Duration: 30 * time.Second,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "exporter-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: nil, Annotations: nil,
Tags: map[string]string{ Tags: map[string]string{
@ -227,7 +231,9 @@ func TestExportSpans(t *testing.T) {
defer collector.Close() defer collector.Close()
ls := &logStore{T: t} ls := &logStore{T: t}
logger := logStoreLogger(ls) logger := logStoreLogger(ls)
exporter, err := NewExporter(collector.url, WithLogger(logger)) _, err := NewExporter(collector.url, "", WithLogger(logger))
require.Error(t, err, "service name must be non-empty string")
exporter, err := NewExporter(collector.url, "exporter-test", WithLogger(logger))
require.NoError(t, err) require.NoError(t, err)
ctx := context.Background() ctx := context.Background()
require.Len(t, ls.Messages, 0) require.Len(t, ls.Messages, 0)

View File

@ -26,23 +26,25 @@ import (
export "go.opentelemetry.io/otel/sdk/export/trace" export "go.opentelemetry.io/otel/sdk/export/trace"
) )
func toZipkinSpanModels(batch []*export.SpanData) []zkmodel.SpanModel { func toZipkinSpanModels(batch []*export.SpanData, serviceName string) []zkmodel.SpanModel {
models := make([]zkmodel.SpanModel, 0, len(batch)) models := make([]zkmodel.SpanModel, 0, len(batch))
for _, data := range batch { for _, data := range batch {
models = append(models, toZipkinSpanModel(data)) models = append(models, toZipkinSpanModel(data, serviceName))
} }
return models return models
} }
func toZipkinSpanModel(data *export.SpanData) zkmodel.SpanModel { func toZipkinSpanModel(data *export.SpanData, serviceName string) zkmodel.SpanModel {
return zkmodel.SpanModel{ return zkmodel.SpanModel{
SpanContext: toZipkinSpanContext(data), SpanContext: toZipkinSpanContext(data),
Name: data.Name, Name: data.Name,
Kind: toZipkinKind(data.SpanKind), Kind: toZipkinKind(data.SpanKind),
Timestamp: data.StartTime, Timestamp: data.StartTime,
Duration: data.EndTime.Sub(data.StartTime), Duration: data.EndTime.Sub(data.StartTime),
Shared: false, Shared: false,
LocalEndpoint: nil, // *Endpoint LocalEndpoint: &zkmodel.Endpoint{
ServiceName: serviceName,
},
RemoteEndpoint: nil, // *Endpoint RemoteEndpoint: nil, // *Endpoint
Annotations: toZipkinAnnotations(data.MessageEvents), Annotations: toZipkinAnnotations(data.MessageEvents),
Tags: toZipkinTags(data), Tags: toZipkinTags(data),

View File

@ -321,12 +321,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "SERVER", Kind: "SERVER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -358,12 +360,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "SERVER", Kind: "SERVER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -395,12 +399,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "", Kind: "",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -432,12 +438,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "", Kind: "",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -469,12 +477,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "CLIENT", Kind: "CLIENT",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -506,12 +516,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "PRODUCER", Kind: "PRODUCER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -543,12 +555,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "CONSUMER", Kind: "CONSUMER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -580,12 +594,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "SERVER", Kind: "SERVER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: nil, Annotations: nil,
Tags: map[string]string{ Tags: map[string]string{
@ -608,12 +624,14 @@ func TestModelConversion(t *testing.T) {
Sampled: nil, Sampled: nil,
Err: nil, Err: nil,
}, },
Name: "foo", Name: "foo",
Kind: "SERVER", Kind: "SERVER",
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC), Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
Duration: time.Minute, Duration: time.Minute,
Shared: false, Shared: false,
LocalEndpoint: nil, LocalEndpoint: &zkmodel.Endpoint{
ServiceName: "model-test",
},
RemoteEndpoint: nil, RemoteEndpoint: nil,
Annotations: []zkmodel.Annotation{ Annotations: []zkmodel.Annotation{
{ {
@ -631,7 +649,7 @@ func TestModelConversion(t *testing.T) {
}, },
}, },
} }
gottenOutputBatch := toZipkinSpanModels(inputBatch) gottenOutputBatch := toZipkinSpanModels(inputBatch, "model-test")
require.Equal(t, expectedOutputBatch, gottenOutputBatch) require.Equal(t, expectedOutputBatch, gottenOutputBatch)
} }

View File

@ -30,9 +30,10 @@ import (
// the SpanBatcher interface, so it needs to be used together with the // the SpanBatcher interface, so it needs to be used together with the
// WithBatcher option when setting up the exporter pipeline. // WithBatcher option when setting up the exporter pipeline.
type Exporter struct { type Exporter struct {
url string url string
client *http.Client serviceName string
logger *log.Logger client *http.Client
logger *log.Logger
} }
var ( var (
@ -63,10 +64,13 @@ func WithClient(client *http.Client) Option {
} }
// NewExporter creates a new zipkin exporter. // NewExporter creates a new zipkin exporter.
func NewExporter(collectorURL string, os ...Option) (*Exporter, error) { func NewExporter(collectorURL string, serviceName string, os ...Option) (*Exporter, error) {
if _, err := url.Parse(collectorURL); err != nil { if _, err := url.Parse(collectorURL); err != nil {
return nil, fmt.Errorf("invalid collector URL: %v", err) return nil, fmt.Errorf("invalid collector URL: %v", err)
} }
if serviceName == "" {
return nil, fmt.Errorf("service name must be non-empty string")
}
opts := Options{} opts := Options{}
for _, o := range os { for _, o := range os {
o(&opts) o(&opts)
@ -75,9 +79,10 @@ func NewExporter(collectorURL string, os ...Option) (*Exporter, error) {
opts.client = http.DefaultClient opts.client = http.DefaultClient
} }
return &Exporter{ return &Exporter{
url: collectorURL, url: collectorURL,
client: opts.client, client: opts.client,
logger: opts.logger, logger: opts.logger,
serviceName: serviceName,
}, nil }, nil
} }
@ -88,7 +93,7 @@ func (e *Exporter) ExportSpans(ctx context.Context, batch []*export.SpanData) {
e.logf("no spans to export") e.logf("no spans to export")
return return
} }
models := toZipkinSpanModels(batch) models := toZipkinSpanModels(batch, e.serviceName)
body, err := json.Marshal(models) body, err := json.Marshal(models)
if err != nil { if err != nil {
e.logf("failed to serialize zipkin models to JSON: %v", err) e.logf("failed to serialize zipkin models to JSON: %v", err)