Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Sep 15, 2022
1 parent 609a9cd commit 8ed4dfe
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ template <class SpanType, class TracerType>
SpanType *new_span(TracerType *objPtr,
nostd::string_view name,
const opentelemetry::trace::StartSpanOptions &options,
const opentelemetry::trace::SpanContext &span_context)
std::unique_ptr<opentelemetry::trace::SpanContext> span_context)
{
return new (std::nothrow) SpanType{*objPtr, name, options, span_context};
return new (std::nothrow) SpanType{*objPtr, name, options, std::move(span_context)};
}

/**
Expand Down Expand Up @@ -419,9 +419,13 @@ class Tracer : public opentelemetry::trace::Tracer,
auto traceId = parentContext.IsValid() ? parentContext.trace_id() : traceId_;

// Sampling based on attributes is not supported for now, so passing empty below.
std::map<std::string, int> empty_attributes = {{}};
opentelemetry::sdk::trace::SamplingResult sampling_result =
GetSampler(tracerProvider_)
.ShouldSample(parentContext, traceId, name, options.kind, {}, links);
.ShouldSample(parentContext, traceId, name, options.kind,
opentelemetry::common::KeyValueIterableView<std::map<std::string, int>>(
empty_attributes),
links);

opentelemetry::trace::TraceFlags trace_flags =
sampling_result.decision == opentelemetry::sdk::trace::Decision::DROP
Expand All @@ -433,8 +437,8 @@ class Tracer : public opentelemetry::trace::Tracer,
traceId, GetIdGenerator(tracerProvider_).GenerateSpanId(), trace_flags, false,
sampling_result.trace_state
? sampling_result.trace_state
: parentContext.IsValid ? parentContext.trace_state()
: opentelemetry::trace::TraceState::GetDefault()));
: parentContext.IsValid() ? parentContext.trace_state()
: opentelemetry::trace::TraceState::GetDefault()));

if (sampling_result.decision == sdk::trace::Decision::DROP)
{
Expand All @@ -459,11 +463,9 @@ class Tracer : public opentelemetry::trace::Tracer,

// This template pattern allows us to forward-declare the etw::Span,
// create an instance of it, then assign it to tracer::Span result.
auto currentSpan = new_span<Span, Tracer>(this, name, options, spanContext);
auto currentSpan = new_span<Span, Tracer>(this, name, options, std::move(spanContext));
nostd::shared_ptr<opentelemetry::trace::Span> result = to_span_ptr<Span>(currentSpan);

// auto spanContext = result->GetContext();

// Decorate with additional standard fields
std::string eventName = name.data();

Expand All @@ -478,13 +480,13 @@ class Tracer : public opentelemetry::trace::Tracer,
{
evt[ETW_FIELD_SPAN_PARENTID] = ToLowerBase16(parentContext.span_id());
}
evt[ETW_FIELD_SPAN_ID] = ToLowerBase16(spanContext.span_id());
evt[ETW_FIELD_SPAN_ID] = ToLowerBase16(result.get()->GetContext().span_id());
}

// Populate Etw.Payload["TraceId"] attribute
if (cfg.enableTraceId)
{
evt[ETW_FIELD_TRACE_ID] = ToLowerBase16(spanContext.trace_id());
evt[ETW_FIELD_TRACE_ID] = ToLowerBase16(result.get()->GetContext().trace_id());
}

// Populate Etw.ActivityId at envelope level if enabled
Expand Down Expand Up @@ -708,7 +710,7 @@ class Span : public opentelemetry::trace::Span
*/
Span *GetParent() const { return parent_; }

opentelemetry::trace::SpanContext context_;
std::unique_ptr<opentelemetry::trace::SpanContext> context_;

public:
/**
Expand Down Expand Up @@ -762,12 +764,12 @@ class Span : public opentelemetry::trace::Span
Span(Tracer &owner,
nostd::string_view name,
const opentelemetry::trace::StartSpanOptions &options,
const opentelemetry::trace::SpanContext &span_context,
std::unique_ptr<opentelemetry::trace::SpanContext> span_context,
Span *parent = nullptr) noexcept
: opentelemetry::trace::Span(),
start_time_(std::chrono::system_clock::now()),
owner_(owner),
context_(span_context),
context_(std::move(span_context)),
parent_(parent)
{
name_ = name;
Expand Down Expand Up @@ -886,7 +888,7 @@ class Span : public opentelemetry::trace::Span
* @brief Obtain SpanContext
* @return
*/
opentelemetry::trace::SpanContext GetContext() const noexcept override { return context_; }
opentelemetry::trace::SpanContext GetContext() const noexcept override { return *context_.get(); }

/**
* @brief Check if Span is recording data.
Expand Down

0 comments on commit 8ed4dfe

Please sign in to comment.