Skip to content

Commit cf7c4e5

Browse files
authored
fix zipkin without local endpoint with service name (#644)
1 parent 669d4b3 commit cf7c4e5

File tree

5 files changed

+118
-86
lines changed

5 files changed

+118
-86
lines changed

example/zipkin/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func initTracer() {
3535
// Create Zipkin Exporter
3636
exporter, err := zipkin.NewExporter(
3737
"http://localhost:9411/api/v2/spans",
38+
"zipkin-example",
3839
zipkin.WithLogger(logger),
3940
)
4041
if err != nil {

exporters/trace/zipkin/exporter_test.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,14 @@ func TestExportSpans(t *testing.T) {
182182
Sampled: nil,
183183
Err: nil,
184184
},
185-
Name: "foo",
186-
Kind: "SERVER",
187-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
188-
Duration: time.Minute,
189-
Shared: false,
190-
LocalEndpoint: nil,
185+
Name: "foo",
186+
Kind: "SERVER",
187+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
188+
Duration: time.Minute,
189+
Shared: false,
190+
LocalEndpoint: &zkmodel.Endpoint{
191+
ServiceName: "exporter-test",
192+
},
191193
RemoteEndpoint: nil,
192194
Annotations: nil,
193195
Tags: map[string]string{
@@ -208,12 +210,14 @@ func TestExportSpans(t *testing.T) {
208210
Sampled: nil,
209211
Err: nil,
210212
},
211-
Name: "bar",
212-
Kind: "SERVER",
213-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 15, 0, time.UTC),
214-
Duration: 30 * time.Second,
215-
Shared: false,
216-
LocalEndpoint: nil,
213+
Name: "bar",
214+
Kind: "SERVER",
215+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 15, 0, time.UTC),
216+
Duration: 30 * time.Second,
217+
Shared: false,
218+
LocalEndpoint: &zkmodel.Endpoint{
219+
ServiceName: "exporter-test",
220+
},
217221
RemoteEndpoint: nil,
218222
Annotations: nil,
219223
Tags: map[string]string{
@@ -227,7 +231,9 @@ func TestExportSpans(t *testing.T) {
227231
defer collector.Close()
228232
ls := &logStore{T: t}
229233
logger := logStoreLogger(ls)
230-
exporter, err := NewExporter(collector.url, WithLogger(logger))
234+
_, err := NewExporter(collector.url, "", WithLogger(logger))
235+
require.Error(t, err, "service name must be non-empty string")
236+
exporter, err := NewExporter(collector.url, "exporter-test", WithLogger(logger))
231237
require.NoError(t, err)
232238
ctx := context.Background()
233239
require.Len(t, ls.Messages, 0)

exporters/trace/zipkin/model.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,25 @@ import (
2626
export "go.opentelemetry.io/otel/sdk/export/trace"
2727
)
2828

29-
func toZipkinSpanModels(batch []*export.SpanData) []zkmodel.SpanModel {
29+
func toZipkinSpanModels(batch []*export.SpanData, serviceName string) []zkmodel.SpanModel {
3030
models := make([]zkmodel.SpanModel, 0, len(batch))
3131
for _, data := range batch {
32-
models = append(models, toZipkinSpanModel(data))
32+
models = append(models, toZipkinSpanModel(data, serviceName))
3333
}
3434
return models
3535
}
3636

37-
func toZipkinSpanModel(data *export.SpanData) zkmodel.SpanModel {
37+
func toZipkinSpanModel(data *export.SpanData, serviceName string) zkmodel.SpanModel {
3838
return zkmodel.SpanModel{
39-
SpanContext: toZipkinSpanContext(data),
40-
Name: data.Name,
41-
Kind: toZipkinKind(data.SpanKind),
42-
Timestamp: data.StartTime,
43-
Duration: data.EndTime.Sub(data.StartTime),
44-
Shared: false,
45-
LocalEndpoint: nil, // *Endpoint
39+
SpanContext: toZipkinSpanContext(data),
40+
Name: data.Name,
41+
Kind: toZipkinKind(data.SpanKind),
42+
Timestamp: data.StartTime,
43+
Duration: data.EndTime.Sub(data.StartTime),
44+
Shared: false,
45+
LocalEndpoint: &zkmodel.Endpoint{
46+
ServiceName: serviceName,
47+
},
4648
RemoteEndpoint: nil, // *Endpoint
4749
Annotations: toZipkinAnnotations(data.MessageEvents),
4850
Tags: toZipkinTags(data),

exporters/trace/zipkin/model_test.go

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,14 @@ func TestModelConversion(t *testing.T) {
321321
Sampled: nil,
322322
Err: nil,
323323
},
324-
Name: "foo",
325-
Kind: "SERVER",
326-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
327-
Duration: time.Minute,
328-
Shared: false,
329-
LocalEndpoint: nil,
324+
Name: "foo",
325+
Kind: "SERVER",
326+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
327+
Duration: time.Minute,
328+
Shared: false,
329+
LocalEndpoint: &zkmodel.Endpoint{
330+
ServiceName: "model-test",
331+
},
330332
RemoteEndpoint: nil,
331333
Annotations: []zkmodel.Annotation{
332334
{
@@ -358,12 +360,14 @@ func TestModelConversion(t *testing.T) {
358360
Sampled: nil,
359361
Err: nil,
360362
},
361-
Name: "foo",
362-
Kind: "SERVER",
363-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
364-
Duration: time.Minute,
365-
Shared: false,
366-
LocalEndpoint: nil,
363+
Name: "foo",
364+
Kind: "SERVER",
365+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
366+
Duration: time.Minute,
367+
Shared: false,
368+
LocalEndpoint: &zkmodel.Endpoint{
369+
ServiceName: "model-test",
370+
},
367371
RemoteEndpoint: nil,
368372
Annotations: []zkmodel.Annotation{
369373
{
@@ -395,12 +399,14 @@ func TestModelConversion(t *testing.T) {
395399
Sampled: nil,
396400
Err: nil,
397401
},
398-
Name: "foo",
399-
Kind: "",
400-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
401-
Duration: time.Minute,
402-
Shared: false,
403-
LocalEndpoint: nil,
402+
Name: "foo",
403+
Kind: "",
404+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
405+
Duration: time.Minute,
406+
Shared: false,
407+
LocalEndpoint: &zkmodel.Endpoint{
408+
ServiceName: "model-test",
409+
},
404410
RemoteEndpoint: nil,
405411
Annotations: []zkmodel.Annotation{
406412
{
@@ -432,12 +438,14 @@ func TestModelConversion(t *testing.T) {
432438
Sampled: nil,
433439
Err: nil,
434440
},
435-
Name: "foo",
436-
Kind: "",
437-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
438-
Duration: time.Minute,
439-
Shared: false,
440-
LocalEndpoint: nil,
441+
Name: "foo",
442+
Kind: "",
443+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
444+
Duration: time.Minute,
445+
Shared: false,
446+
LocalEndpoint: &zkmodel.Endpoint{
447+
ServiceName: "model-test",
448+
},
441449
RemoteEndpoint: nil,
442450
Annotations: []zkmodel.Annotation{
443451
{
@@ -469,12 +477,14 @@ func TestModelConversion(t *testing.T) {
469477
Sampled: nil,
470478
Err: nil,
471479
},
472-
Name: "foo",
473-
Kind: "CLIENT",
474-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
475-
Duration: time.Minute,
476-
Shared: false,
477-
LocalEndpoint: nil,
480+
Name: "foo",
481+
Kind: "CLIENT",
482+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
483+
Duration: time.Minute,
484+
Shared: false,
485+
LocalEndpoint: &zkmodel.Endpoint{
486+
ServiceName: "model-test",
487+
},
478488
RemoteEndpoint: nil,
479489
Annotations: []zkmodel.Annotation{
480490
{
@@ -506,12 +516,14 @@ func TestModelConversion(t *testing.T) {
506516
Sampled: nil,
507517
Err: nil,
508518
},
509-
Name: "foo",
510-
Kind: "PRODUCER",
511-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
512-
Duration: time.Minute,
513-
Shared: false,
514-
LocalEndpoint: nil,
519+
Name: "foo",
520+
Kind: "PRODUCER",
521+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
522+
Duration: time.Minute,
523+
Shared: false,
524+
LocalEndpoint: &zkmodel.Endpoint{
525+
ServiceName: "model-test",
526+
},
515527
RemoteEndpoint: nil,
516528
Annotations: []zkmodel.Annotation{
517529
{
@@ -543,12 +555,14 @@ func TestModelConversion(t *testing.T) {
543555
Sampled: nil,
544556
Err: nil,
545557
},
546-
Name: "foo",
547-
Kind: "CONSUMER",
548-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
549-
Duration: time.Minute,
550-
Shared: false,
551-
LocalEndpoint: nil,
558+
Name: "foo",
559+
Kind: "CONSUMER",
560+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
561+
Duration: time.Minute,
562+
Shared: false,
563+
LocalEndpoint: &zkmodel.Endpoint{
564+
ServiceName: "model-test",
565+
},
552566
RemoteEndpoint: nil,
553567
Annotations: []zkmodel.Annotation{
554568
{
@@ -580,12 +594,14 @@ func TestModelConversion(t *testing.T) {
580594
Sampled: nil,
581595
Err: nil,
582596
},
583-
Name: "foo",
584-
Kind: "SERVER",
585-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
586-
Duration: time.Minute,
587-
Shared: false,
588-
LocalEndpoint: nil,
597+
Name: "foo",
598+
Kind: "SERVER",
599+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
600+
Duration: time.Minute,
601+
Shared: false,
602+
LocalEndpoint: &zkmodel.Endpoint{
603+
ServiceName: "model-test",
604+
},
589605
RemoteEndpoint: nil,
590606
Annotations: nil,
591607
Tags: map[string]string{
@@ -608,12 +624,14 @@ func TestModelConversion(t *testing.T) {
608624
Sampled: nil,
609625
Err: nil,
610626
},
611-
Name: "foo",
612-
Kind: "SERVER",
613-
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
614-
Duration: time.Minute,
615-
Shared: false,
616-
LocalEndpoint: nil,
627+
Name: "foo",
628+
Kind: "SERVER",
629+
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
630+
Duration: time.Minute,
631+
Shared: false,
632+
LocalEndpoint: &zkmodel.Endpoint{
633+
ServiceName: "model-test",
634+
},
617635
RemoteEndpoint: nil,
618636
Annotations: []zkmodel.Annotation{
619637
{
@@ -631,7 +649,7 @@ func TestModelConversion(t *testing.T) {
631649
},
632650
},
633651
}
634-
gottenOutputBatch := toZipkinSpanModels(inputBatch)
652+
gottenOutputBatch := toZipkinSpanModels(inputBatch, "model-test")
635653
require.Equal(t, expectedOutputBatch, gottenOutputBatch)
636654
}
637655

exporters/trace/zipkin/zipkin.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030
// the SpanBatcher interface, so it needs to be used together with the
3131
// WithBatcher option when setting up the exporter pipeline.
3232
type Exporter struct {
33-
url string
34-
client *http.Client
35-
logger *log.Logger
33+
url string
34+
serviceName string
35+
client *http.Client
36+
logger *log.Logger
3637
}
3738

3839
var (
@@ -63,10 +64,13 @@ func WithClient(client *http.Client) Option {
6364
}
6465

6566
// NewExporter creates a new zipkin exporter.
66-
func NewExporter(collectorURL string, os ...Option) (*Exporter, error) {
67+
func NewExporter(collectorURL string, serviceName string, os ...Option) (*Exporter, error) {
6768
if _, err := url.Parse(collectorURL); err != nil {
6869
return nil, fmt.Errorf("invalid collector URL: %v", err)
6970
}
71+
if serviceName == "" {
72+
return nil, fmt.Errorf("service name must be non-empty string")
73+
}
7074
opts := Options{}
7175
for _, o := range os {
7276
o(&opts)
@@ -75,9 +79,10 @@ func NewExporter(collectorURL string, os ...Option) (*Exporter, error) {
7579
opts.client = http.DefaultClient
7680
}
7781
return &Exporter{
78-
url: collectorURL,
79-
client: opts.client,
80-
logger: opts.logger,
82+
url: collectorURL,
83+
client: opts.client,
84+
logger: opts.logger,
85+
serviceName: serviceName,
8186
}, nil
8287
}
8388

@@ -88,7 +93,7 @@ func (e *Exporter) ExportSpans(ctx context.Context, batch []*export.SpanData) {
8893
e.logf("no spans to export")
8994
return
9095
}
91-
models := toZipkinSpanModels(batch)
96+
models := toZipkinSpanModels(batch, e.serviceName)
9297
body, err := json.Marshal(models)
9398
if err != nil {
9499
e.logf("failed to serialize zipkin models to JSON: %v", err)

0 commit comments

Comments
 (0)