mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-05 22:54:18 +02:00
fix zipkin without local endpoint with service name (#644)
This commit is contained in:
parent
669d4b3a6c
commit
cf7c4e5328
@ -35,6 +35,7 @@ func initTracer() {
|
||||
// Create Zipkin Exporter
|
||||
exporter, err := zipkin.NewExporter(
|
||||
"http://localhost:9411/api/v2/spans",
|
||||
"zipkin-example",
|
||||
zipkin.WithLogger(logger),
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -187,7 +187,9 @@ func TestExportSpans(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "exporter-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: nil,
|
||||
Tags: map[string]string{
|
||||
@ -213,7 +215,9 @@ func TestExportSpans(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 15, 0, time.UTC),
|
||||
Duration: 30 * time.Second,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "exporter-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: nil,
|
||||
Tags: map[string]string{
|
||||
@ -227,7 +231,9 @@ func TestExportSpans(t *testing.T) {
|
||||
defer collector.Close()
|
||||
ls := &logStore{T: t}
|
||||
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)
|
||||
ctx := context.Background()
|
||||
require.Len(t, ls.Messages, 0)
|
||||
|
@ -26,15 +26,15 @@ import (
|
||||
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))
|
||||
for _, data := range batch {
|
||||
models = append(models, toZipkinSpanModel(data))
|
||||
models = append(models, toZipkinSpanModel(data, serviceName))
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
func toZipkinSpanModel(data *export.SpanData) zkmodel.SpanModel {
|
||||
func toZipkinSpanModel(data *export.SpanData, serviceName string) zkmodel.SpanModel {
|
||||
return zkmodel.SpanModel{
|
||||
SpanContext: toZipkinSpanContext(data),
|
||||
Name: data.Name,
|
||||
@ -42,7 +42,9 @@ func toZipkinSpanModel(data *export.SpanData) zkmodel.SpanModel {
|
||||
Timestamp: data.StartTime,
|
||||
Duration: data.EndTime.Sub(data.StartTime),
|
||||
Shared: false,
|
||||
LocalEndpoint: nil, // *Endpoint
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: serviceName,
|
||||
},
|
||||
RemoteEndpoint: nil, // *Endpoint
|
||||
Annotations: toZipkinAnnotations(data.MessageEvents),
|
||||
Tags: toZipkinTags(data),
|
||||
|
@ -326,7 +326,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -363,7 +365,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -400,7 +404,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -437,7 +443,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -474,7 +482,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -511,7 +521,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -548,7 +560,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: []zkmodel.Annotation{
|
||||
{
|
||||
@ -585,7 +599,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
Annotations: nil,
|
||||
Tags: map[string]string{
|
||||
@ -613,7 +629,9 @@ func TestModelConversion(t *testing.T) {
|
||||
Timestamp: time.Date(2020, time.March, 11, 19, 24, 0, 0, time.UTC),
|
||||
Duration: time.Minute,
|
||||
Shared: false,
|
||||
LocalEndpoint: nil,
|
||||
LocalEndpoint: &zkmodel.Endpoint{
|
||||
ServiceName: "model-test",
|
||||
},
|
||||
RemoteEndpoint: nil,
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@ import (
|
||||
// WithBatcher option when setting up the exporter pipeline.
|
||||
type Exporter struct {
|
||||
url string
|
||||
serviceName string
|
||||
client *http.Client
|
||||
logger *log.Logger
|
||||
}
|
||||
@ -63,10 +64,13 @@ func WithClient(client *http.Client) Option {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, fmt.Errorf("invalid collector URL: %v", err)
|
||||
}
|
||||
if serviceName == "" {
|
||||
return nil, fmt.Errorf("service name must be non-empty string")
|
||||
}
|
||||
opts := Options{}
|
||||
for _, o := range os {
|
||||
o(&opts)
|
||||
@ -78,6 +82,7 @@ func NewExporter(collectorURL string, os ...Option) (*Exporter, error) {
|
||||
url: collectorURL,
|
||||
client: opts.client,
|
||||
logger: opts.logger,
|
||||
serviceName: serviceName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -88,7 +93,7 @@ func (e *Exporter) ExportSpans(ctx context.Context, batch []*export.SpanData) {
|
||||
e.logf("no spans to export")
|
||||
return
|
||||
}
|
||||
models := toZipkinSpanModels(batch)
|
||||
models := toZipkinSpanModels(batch, e.serviceName)
|
||||
body, err := json.Marshal(models)
|
||||
if err != nil {
|
||||
e.logf("failed to serialize zipkin models to JSON: %v", err)
|
||||
|
Loading…
Reference in New Issue
Block a user