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

Add ForceFlush to TracerProvider in SDK #588

Merged
merged 2 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Increment the:

## [Unreleased]

* Added `ForceFlush` to `TracerProvider` in SDK. ([#588](https://github.com/open-telemetry/opentelemetry-cpp/pull/588)).

## [0.0.1] 2020-12-16

### Added
Expand Down
5 changes: 5 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider
*/
bool Shutdown() noexcept;

/**
* Force flush the span processor associated with this tracer provider.
*/
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;

private:
opentelemetry::sdk::AtomicSharedPtr<SpanProcessor> processor_;
std::shared_ptr<opentelemetry::trace::Tracer> tracer_;
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/logs/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void Logger::Log(opentelemetry::logs::Severity severity,
// Leave these fields in the recordable empty if neither the passed in values
// nor the context values are valid (e.g. the application is not using traces)

// Traceid
// TraceId
if (trace_id.IsValid())
{
recordable->SetTraceId(trace_id);
Expand All @@ -99,7 +99,7 @@ void Logger::Log(opentelemetry::logs::Severity severity,
recordable->SetTraceId(span_context.trace_id());
}

// Spanid
// SpanId
if (span_id.IsValid())
{
recordable->SetSpanId(span_id);
Expand All @@ -109,7 +109,7 @@ void Logger::Log(opentelemetry::logs::Severity severity,
recordable->SetSpanId(span_id);
}

// Traceflags
// TraceFlags
if (trace_flags.IsSampled())
{
recordable->SetTraceFlags(trace_flags);
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/trace/tracer_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ bool TracerProvider::Shutdown() noexcept
{
return GetProcessor()->Shutdown();
}

bool TracerProvider::ForceFlush(std::chrono::microseconds timeout) noexcept
{
return GetProcessor()->Shutdown(timeout);
Copy link
Member

Choose a reason for hiding this comment

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

Curious - do we want to call ForceFlush or Shutdown? (I thought the latter)

Copy link
Member

@lalitb lalitb Feb 26, 2021

Choose a reason for hiding this comment

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

As per the specs (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#forceflush):
ForceFlush MUST invoke ForceFlush on all registered SpanProcessors.

So ForceFlush() method needs to be defined both for SpanProcessor and Exporter. This can be done as separate PR and in that case Shutdown() call can be removed for now from above.

Note: PR to update specs for adding ForceFlush in Exporter is still open ( open-telemetry/opentelemetry-specification#1467 )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch, @reyang

@lalitb I think we only support one SpanProcessor now. I'd prefer to add ForceFlush to SpanExporter in a separate PR.

Copy link
Member

@reyang reyang Feb 26, 2021

Choose a reason for hiding this comment

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

I think we only support one SpanProcessor now.

That's fine, I think we could support multiple processors by using a de-mux processor (so from the provider perspective, there is only one processor).

Here are some examples:

}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
9 changes: 9 additions & 0 deletions sdk/test/trace/tracer_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ TEST(TracerProvider, Shutdown)

EXPECT_TRUE(tp1.Shutdown());
}

TEST(TracerProvider, ForceFlush)
{
std::shared_ptr<SpanProcessor> processor1(new SimpleSpanProcessor(nullptr));

TracerProvider tp1(processor1);

EXPECT_TRUE(tp1.ForceFlush());
}