Skip to content

Commit

Permalink
update stdout trace with resource. (#558)
Browse files Browse the repository at this point in the history
* update stdout trace with resource.

* convert resource to span attributes.

* remove resource reference after converting to Span Attributes.
  • Loading branch information
rghetia authored Mar 16, 2020
1 parent 435c39a commit 80b720a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion example/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions exporters/trace/stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions exporters/trace/stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand All @@ -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)

Expand All @@ -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":[` +
Expand Down

0 comments on commit 80b720a

Please sign in to comment.