update stdout trace with resource. (#558)
* update stdout trace with resource. * convert resource to span attributes. * remove resource reference after converting to Span Attributes.
This commit is contained in:
		
							parent
							
								
									435c39aab4
								
							
						
					
					
						commit
						80b720a771
					
				| 
						 | 
				
			
			@ -18,6 +18,7 @@ import (
 | 
			
		|||
	"context"
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"go.opentelemetry.io/otel/api/core"
 | 
			
		||||
	"go.opentelemetry.io/otel/api/correlation"
 | 
			
		||||
	"go.opentelemetry.io/otel/api/global"
 | 
			
		||||
	"go.opentelemetry.io/otel/api/key"
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +46,8 @@ func initTracer() {
 | 
			
		|||
		return
 | 
			
		||||
	}
 | 
			
		||||
	tp, err := sdktrace.NewProvider(sdktrace.WithSyncer(exp),
 | 
			
		||||
		sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
 | 
			
		||||
		sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
 | 
			
		||||
		sdktrace.WithResourceAttributes(core.Key("rk1").String("rv11"), core.Key("rk2").Int64(5)))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Panicf("failed to initialize trace provider %v", err)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -51,6 +51,18 @@ func NewExporter(o Options) (*Exporter, error) {
 | 
			
		|||
 | 
			
		||||
// ExportSpan writes a SpanData in json format to stdout.
 | 
			
		||||
func (e *Exporter) ExportSpan(ctx context.Context, data *export.SpanData) {
 | 
			
		||||
	if data.Resource != nil {
 | 
			
		||||
		dataCopy := *data
 | 
			
		||||
		dataCopy.Attributes = append(data.Attributes, data.Resource.Attributes()...)
 | 
			
		||||
		dataCopy.Resource = nil
 | 
			
		||||
		e.exportSpan(ctx, &dataCopy)
 | 
			
		||||
	} else {
 | 
			
		||||
		e.exportSpan(ctx, data)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ExportSpan writes a SpanData in json format to stdout.
 | 
			
		||||
func (e *Exporter) exportSpan(ctx context.Context, data *export.SpanData) {
 | 
			
		||||
	var jsonSpan []byte
 | 
			
		||||
	var err error
 | 
			
		||||
	if e.pretty {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,6 +27,7 @@ import (
 | 
			
		|||
	"go.opentelemetry.io/otel/api/key"
 | 
			
		||||
	"go.opentelemetry.io/otel/api/trace"
 | 
			
		||||
	export "go.opentelemetry.io/otel/sdk/export/trace"
 | 
			
		||||
	"go.opentelemetry.io/otel/sdk/resource"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestExporter_ExportSpan(t *testing.T) {
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +44,7 @@ func TestExporter_ExportSpan(t *testing.T) {
 | 
			
		|||
	spanID, _ := core.SpanIDFromHex("0102030405060708")
 | 
			
		||||
	keyValue := "value"
 | 
			
		||||
	doubleValue := 123.456
 | 
			
		||||
	resource := resource.New(core.Key("rk1").String("rv11"))
 | 
			
		||||
 | 
			
		||||
	testSpan := &export.SpanData{
 | 
			
		||||
		SpanContext: core.SpanContext{
 | 
			
		||||
| 
						 | 
				
			
			@ -63,6 +65,7 @@ func TestExporter_ExportSpan(t *testing.T) {
 | 
			
		|||
		SpanKind:      trace.SpanKindInternal,
 | 
			
		||||
		StatusCode:    codes.Unknown,
 | 
			
		||||
		StatusMessage: "interesting",
 | 
			
		||||
		Resource:      resource,
 | 
			
		||||
	}
 | 
			
		||||
	ex.ExportSpan(context.Background(), testSpan)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -85,6 +88,10 @@ func TestExporter_ExportSpan(t *testing.T) {
 | 
			
		|||
		`{` +
 | 
			
		||||
		`"Key":"double",` +
 | 
			
		||||
		`"Value":{"Type":"FLOAT64","Value":123.456}` +
 | 
			
		||||
		`},` +
 | 
			
		||||
		`{` +
 | 
			
		||||
		`"Key":"rk1",` +
 | 
			
		||||
		`"Value":{"Type":"STRING","Value":"rv11"}` +
 | 
			
		||||
		`}` +
 | 
			
		||||
		`],` +
 | 
			
		||||
		`"MessageEvents":[` +
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue