diff --git a/exporter/lightstepexporter/lightstep.go b/exporter/lightstepexporter/lightstep.go index b9016e986149..c21f2cbbf092 100644 --- a/exporter/lightstepexporter/lightstep.go +++ b/exporter/lightstepexporter/lightstep.go @@ -62,9 +62,15 @@ func (e *LightStepExporter) pushTraceData(ctx context.Context, td consumerdata.T for _, span := range td.Spans { sd, err := lightstep.OCProtoSpanToOTelSpanData(span) if err == nil { - if td.Node != nil && td.Node.ServiceInfo != nil { - lightStepServiceName := kv.Key("lightstep.component_name") - sd.Attributes = append(sd.Attributes, lightStepServiceName.String(td.Node.ServiceInfo.Name)) + if td.Node != nil { + if td.Node.ServiceInfo != nil { + lightstepServiceName := kv.Key("lightstep.component_name") + sd.Attributes = append(sd.Attributes, lightstepServiceName.String(td.Node.ServiceInfo.Name)) + } + if td.Node.Identifier != nil { + lightstepHostName := kv.Key("lightstep.hostname") + sd.Attributes = append(sd.Attributes, lightstepHostName.String(td.Node.Identifier.HostName)) + } } e.exporter.ExportSpan(ctx, sd) goodSpans++ diff --git a/exporter/lightstepexporter/lightstep_test.go b/exporter/lightstepexporter/lightstep_test.go index 6ceb36bbcede..96a2f5478433 100644 --- a/exporter/lightstepexporter/lightstep_test.go +++ b/exporter/lightstepexporter/lightstep_test.go @@ -16,27 +16,42 @@ package lightstepexporter import ( "context" + "io/ioutil" "net/http" "net/http/httptest" "net/url" "strconv" "testing" + commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1" tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1" "github.com/golang/protobuf/ptypes/wrappers" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/consumer/consumerdata" "go.uber.org/zap" ) -func testingServer() *httptest.Server { +func testingServer(callback func(data []byte)) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + + defer req.Body.Close() + b, err := ioutil.ReadAll(req.Body) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } + + callback(b) rw.Write([]byte(`OK`)) })) } -func testTraceExporter(td consumerdata.TraceData, t *testing.T) { - server := testingServer() +func testTraceExporter(td consumerdata.TraceData, t *testing.T) []byte { + response := []byte{} + server := testingServer(func(data []byte) { + response = append(response, data...) + }) u, _ := url.Parse(server.URL) port, _ := strconv.Atoi(u.Port()) @@ -59,7 +74,7 @@ func testTraceExporter(td consumerdata.TraceData, t *testing.T) { err = exporter.ConsumeTraceData(ctx, td) require.NoError(t, err) exporter.Shutdown(ctx) - + return response } func TestEmptyNode(t *testing.T) { @@ -78,3 +93,29 @@ func TestEmptyNode(t *testing.T) { testTraceExporter(td, t) } + +func TestPushTraceData(t *testing.T) { + td := consumerdata.TraceData{ + Node: &commonpb.Node{ + ServiceInfo: &commonpb.ServiceInfo{ + Name: "serviceABC", + }, + Identifier: &commonpb.ProcessIdentifier{ + HostName: "hostname123", + }, + }, + Spans: []*tracepb.Span{ + { + TraceId: []byte{0x01}, + SpanId: []byte{0x02}, + Name: &tracepb.TruncatableString{Value: "rootSpan"}, + Kind: tracepb.Span_SERVER, + SameProcessAsParentSpan: &wrappers.BoolValue{Value: true}, + }, + }, + } + response := testTraceExporter(td, t) + assert.Contains(t, string(response), "serviceABC") + assert.Contains(t, string(response), "hostname123") + assert.Contains(t, string(response), "rootSpan") +}