Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject span #55

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions exporter/trace/cloudtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"log"
"sync"
"time"

traceapi "cloud.google.com/go/trace/apiv2"
Expand All @@ -29,6 +30,11 @@ import (
export "go.opentelemetry.io/otel/sdk/export/trace"
)

var (
once sync.Once
resLabelMap = make(map[string]string)
)

// Option is function type that is passed to the exporter initialization function.
type Option func(*options)

Expand Down Expand Up @@ -206,11 +212,30 @@ func newContextWithTimeout(ctx context.Context, timeout time.Duration) (context.
return context.WithTimeout(ctx, timeout)
}

// SetResLabelMapForOnce intializes ResLabelMap for once since
// monitored resources are static
func (e * Exporter) SetResLabelMapForOnce(input map[string]string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's true that monitored resources are static, OT doesn't preclude the possibility of specifying different resource info for different telemetry data.

For example, see this proposal which would allow different resource scopes: open-telemetry/oteps#78

As such, we shouldn't make the assumption of static resources in the exporter. Instead, you should just translate from the Otel resource in the trace data to the expected attribute values, similar to what you did for the metrics resource mapping code in the other PR.


Having said that, this does come at the cost of performance. We may end up looking to optimize this somewhat in the future, but I don't think we should do that now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I should have read your other PR first sorry as that makes it more clear what you're trying to achieve here.

Getting the resource from the controller config might actually be a reasonable thing to do (perhaps it would be better to get it from the top-level tracer/meter config). This is a somewhat dependent on how the SDK changes over time, but we could probably fix this if the SDK changed significantly.

Interesting idea - let me think about it some more and get back to you tomorrow.

FYI @nilebox

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. In fact, the PR(https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/pull/53/files) needs to be changed because I plan to export labels from the merged exporter instead of from cloudmonitoring.go or metric.go. But I may still use Config to extract resources from push.Option. More details are in my design doc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 - commented on design doc

once.Do(func() {
for k,v := range input {
resLabelMap[k] = v
}
})
}

// injectLabelsIntoSpan injects Monitored Resources labels into a span
func injectLabelsIntoSpan(sd *export.SpanData) *export.SpanData {
for k, v := range resLabelMap {
sd.Attributes = append(sd.Attributes, kv.Key(k).String(v))
}
return sd
}

// ExportSpan exports a SpanData to Stackdriver Trace.
func (e *Exporter) ExportSpan(ctx context.Context, sd *export.SpanData) {
if len(e.traceExporter.o.DefaultTraceAttributes) > 0 {
sd = e.sdWithDefaultTraceAttributes(sd)
}
injectLabelsIntoSpan(sd)
e.traceExporter.ExportSpan(ctx, sd)
}

Expand Down
2 changes: 1 addition & 1 deletion exporter/trace/trace_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func protoFromSpanData(s *export.SpanData, projectID string) *tracepb.Span {
SameProcessAsParentSpan: &wrapperspb.BoolValue{Value: !s.HasRemoteParent},
}
if s.ParentSpanID != s.SpanContext.SpanID && s.ParentSpanID.IsValid() {
sp.ParentSpanId = fmt.Sprintf("%.16x", s.ParentSpanID)
sp.ParentSpanId = s.ParentSpanID.String()
}
if s.StatusCode != codes.OK {
sp.Status = &statuspb.Status{Code: int32(s.StatusCode)}
Expand Down