Skip to content

Commit b954649

Browse files
authored
Merge branch 'main' into api-getting-started-update
2 parents 9c59720 + b1ff408 commit b954649

File tree

11 files changed

+82
-62
lines changed

11 files changed

+82
-62
lines changed

CODE_OF_CONDUCT.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# OpenTelemetry Community Code of Conduct
2+
3+
OpenTelemetry follows the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md).

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Slack](https://img.shields.io/badge/slack-@cncf/otel/cpp-brightgreen.svg?logo=slack)](https://cloud-native.slack.com/archives/C01N3AT62SJ)
44
[![codecov.io](https://codecov.io/gh/open-telemetry/opentelemetry-cpp/branch/main/graphs/badge.svg?)](https://codecov.io/gh/open-telemetry/opentelemetry-cpp/)
55
[![Build
6-
Status](https://action-badges.now.sh/open-telemetry/opentelemetry-cpp)](https://github.com/open-telemetry/opentelemetry-cpp/actions)
6+
Status](https://github.com/open-telemetry/opentelemetry-cpp/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/open-telemetry/opentelemetry-cpp/actions)
77
[![Release](https://img.shields.io/github/v/release/open-telemetry/opentelemetry-cpp?include_prereleases&style=)](https://github.com/open-telemetry/opentelemetry-cpp/releases/)
88

99
The C++ [OpenTelemetry](https://opentelemetry.io/) client.

api/include/opentelemetry/trace/propagation/http_trace_context.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ static const size_t kTraceParentSize = 55;
3636
// The HttpTraceContext provides methods to extract and inject
3737
// context into headers of HTTP requests with traces.
3838
// Example:
39-
// HttpTraceContext().inject(setter, carrier, context);
40-
// HttpTraceContext().extract(getter, carrier, context);
39+
// HttpTraceContext().Inject(carrier, context);
40+
// HttpTraceContext().Extract(carrier, context);
4141

4242
class HttpTraceContext : public opentelemetry::context::propagation::TextMapPropagator
4343
{

ci/setup_grpc.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ fi
1010
export BUILD_DIR=/tmp/
1111
export INSTALL_DIR=/usr/local/
1212
pushd $BUILD_DIR
13-
git clone --recurse-submodules -b v1.34.0 https://github.com/grpc/grpc
13+
git clone --depth=1 -b v1.34.0 https://github.com/grpc/grpc
1414
cd grpc
15+
git submodule init
16+
git submodule update --depth 1
1517
mkdir -p cmake/build
1618
pushd cmake/build
1719
cmake -DgRPC_INSTALL=ON \

examples/http/server.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RequestHandler : public HTTP_SERVER_NS::HttpRequestCallback
2525
auto prop = opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator();
2626
auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent();
2727
auto new_context = prop->Extract(carrier, current_ctx);
28-
options.parent = GetSpanFromContext(new_context)->GetContext();
28+
options.parent = opentelemetry::trace::propagation::GetSpan(new_context)->GetContext();
2929

3030
// start span with parent context extracted from http header
3131
auto span = get_tracer("http-server")

examples/http/tracer_common.h

-13
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@
1616

1717
namespace
1818
{
19-
// TBD - This function be removed once #723 is merged
20-
inline nostd::shared_ptr<opentelemetry::trace::Span> GetSpanFromContext(
21-
const opentelemetry::context::Context &context)
22-
{
23-
opentelemetry::context::ContextValue span = context.GetValue(opentelemetry::trace::kSpanKey);
24-
if (nostd::holds_alternative<nostd::shared_ptr<opentelemetry::trace::Span>>(span))
25-
{
26-
return nostd::get<nostd::shared_ptr<opentelemetry::trace::Span>>(span);
27-
}
28-
static nostd::shared_ptr<opentelemetry::trace::Span> invalid_span{
29-
new opentelemetry::trace::DefaultSpan(opentelemetry::trace::SpanContext::GetInvalid())};
30-
return invalid_span;
31-
}
3219

3320
template <typename T>
3421
class HttpTextMapCarrier : public opentelemetry::context::propagation::TextMapCarrier

ext/src/http/client/curl/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ if(CURL_FOUND)
55
set_target_properties(http_client_curl PROPERTIES EXPORT_NAME
66
http_client_curl)
77

8-
target_link_libraries(http_client_curl PUBLIC CURL::libcurl)
8+
if(TARGET CURL::libcurl)
9+
target_link_libraries(http_client_curl PUBLIC CURL::libcurl)
10+
else()
11+
include_directories(${CURL_INCLUDE_DIRS})
12+
target_link_libraries(http_client_curl PUBLIC ${CURL_LIBRARIES})
13+
endif()
914

1015
install(
1116
TARGETS http_client_curl

sdk/src/trace/span.cc

+5-28
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
4747
const trace_api::SpanContextKeyValueIterable &links,
4848
const trace_api::StartSpanOptions &options,
4949
const trace_api::SpanContext &parent_span_context,
50-
const nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state,
51-
const bool sampled) noexcept
50+
std::unique_ptr<trace_api::SpanContext> span_context) noexcept
5251
: tracer_{std::move(tracer)},
5352
recordable_{tracer_->GetProcessor().MakeRecordable()},
5453
start_steady_time{options.start_steady_time},
54+
span_context_(std::move(span_context)),
5555
has_ended_{false}
5656
{
5757
if (recordable_ == nullptr)
@@ -60,32 +60,9 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
6060
}
6161
recordable_->SetName(name);
6262
recordable_->SetInstrumentationLibrary(tracer_->GetInstrumentationLibrary());
63-
64-
trace_api::TraceId trace_id;
65-
trace_api::SpanId span_id = tracer_->GetIdGenerator().GenerateSpanId();
66-
trace_api::SpanId parent_span_id;
67-
bool is_parent_span_valid = false;
68-
69-
if (parent_span_context.IsValid())
70-
{
71-
trace_id = parent_span_context.trace_id();
72-
parent_span_id = parent_span_context.span_id();
73-
is_parent_span_valid = true;
74-
}
75-
else
76-
{
77-
trace_id = tracer_->GetIdGenerator().GenerateTraceId();
78-
}
79-
80-
span_context_ = std::unique_ptr<trace_api::SpanContext>(new trace_api::SpanContext(
81-
trace_id, span_id,
82-
sampled ? trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled} : trace_api::TraceFlags{},
83-
false,
84-
trace_state ? trace_state
85-
: is_parent_span_valid ? parent_span_context.trace_state()
86-
: trace_api::TraceState::GetDefault()));
87-
88-
recordable_->SetIdentity(*span_context_, parent_span_id);
63+
recordable_->SetIdentity(*span_context_, parent_span_context.IsValid()
64+
? parent_span_context.span_id()
65+
: trace_api::SpanId());
8966

9067
attributes.ForEachKeyValue(
9168
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {

sdk/src/trace/span.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class Span final : public trace_api::Span
2121
const trace_api::SpanContextKeyValueIterable &links,
2222
const trace_api::StartSpanOptions &options,
2323
const trace_api::SpanContext &parent_span_context,
24-
const nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state =
25-
trace_api::TraceState::GetDefault(),
26-
const bool sampled = false) noexcept;
24+
std::unique_ptr<trace_api::SpanContext> span_context) noexcept;
2725

2826
~Span() override;
2927

sdk/src/trace/tracer.cc

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "opentelemetry/version.h"
66
#include "src/trace/span.h"
77

8+
#include <memory>
9+
810
OPENTELEMETRY_BEGIN_NAMESPACE
911
namespace sdk
1012
{
@@ -22,11 +24,26 @@ nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
2224
const trace_api::SpanContextKeyValueIterable &links,
2325
const trace_api::StartSpanOptions &options) noexcept
2426
{
25-
trace_api::SpanContext parent =
27+
trace_api::SpanContext parent_context =
2628
options.parent.IsValid() ? options.parent : GetCurrentSpan()->GetContext();
2729

28-
auto sampling_result = context_->GetSampler().ShouldSample(parent, parent.trace_id(), name,
30+
trace_api::TraceId trace_id;
31+
trace_api::SpanId span_id = GetIdGenerator().GenerateSpanId();
32+
bool is_parent_span_valid = false;
33+
34+
if (parent_context.IsValid())
35+
{
36+
trace_id = parent_context.trace_id();
37+
is_parent_span_valid = true;
38+
}
39+
else
40+
{
41+
trace_id = GetIdGenerator().GenerateTraceId();
42+
}
43+
44+
auto sampling_result = context_->GetSampler().ShouldSample(parent_context, trace_id, name,
2945
options.kind, attributes, links);
46+
3047
if (sampling_result.decision == Decision::DROP)
3148
{
3249
// Don't allocate a no-op span for every DROP decision, but use a static
@@ -38,9 +55,16 @@ nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
3855
}
3956
else
4057
{
58+
59+
auto span_context = std::unique_ptr<trace_api::SpanContext>(new trace_api::SpanContext(
60+
trace_id, span_id, trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled}, false,
61+
sampling_result.trace_state ? sampling_result.trace_state
62+
: is_parent_span_valid ? parent_context.trace_state()
63+
: trace_api::TraceState::GetDefault()));
64+
4165
auto span = nostd::shared_ptr<trace_api::Span>{
42-
new (std::nothrow) Span{this->shared_from_this(), name, attributes, links, options, parent,
43-
sampling_result.trace_state, true}};
66+
new (std::nothrow) Span{this->shared_from_this(), name, attributes, links, options,
67+
parent_context, std::move(span_context)}};
4468

4569
// if the attributes is not nullptr, add attributes to the span.
4670
if (sampling_result.attributes)

sdk/test/trace/tracer_test.cc

+32-8
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,37 @@ using opentelemetry::exporter::memory::InMemorySpanExporter;
2121
using opentelemetry::trace::SpanContext;
2222

2323
/**
24-
* A mock sampler that returns non-empty sampling results attributes.
24+
* A mock sampler with ShouldSample returning:
25+
* Decision::RECORD_AND_SAMPLE if trace_id is valid
26+
* Decision::DROP otherwise.
2527
*/
2628
class MockSampler final : public Sampler
2729
{
2830
public:
2931
SamplingResult ShouldSample(
3032
const SpanContext & /*parent_context*/,
31-
trace_api::TraceId /*trace_id*/,
33+
trace_api::TraceId trace_id,
3234
nostd::string_view /*name*/,
3335
trace_api::SpanKind /*span_kind*/,
3436
const opentelemetry::common::KeyValueIterable & /*attributes*/,
3537
const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/) noexcept override
3638
{
37-
// Return two pairs of attributes. These attributes should be added to the
38-
// span attributes
39-
return {Decision::RECORD_AND_SAMPLE,
40-
nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
41-
new const std::map<std::string, opentelemetry::common::AttributeValue>(
42-
{{"sampling_attr1", 123}, {"sampling_attr2", "string"}}))};
39+
// Sample only if valid trace_id ( This is to test Sampler get's valid trace id)
40+
if (trace_id.IsValid())
41+
{
42+
// Return two pairs of attributes. These attributes should be added to the
43+
// span attributes
44+
return {Decision::RECORD_AND_SAMPLE,
45+
nostd::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>>(
46+
new const std::map<std::string, opentelemetry::common::AttributeValue>(
47+
{{"sampling_attr1", 123}, {"sampling_attr2", "string"}}))};
48+
}
49+
else
50+
{
51+
// we should never reach here
52+
assert(false);
53+
return {Decision::DROP};
54+
}
4355
}
4456

4557
nostd::string_view GetDescription() const noexcept override { return "MockSampler"; }
@@ -599,3 +611,15 @@ TEST(Tracer, ExpectParent)
599611
EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
600612
EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
601613
}
614+
615+
TEST(Tracer, ValidTraceIdToSampler)
616+
{
617+
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());
618+
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
619+
auto tracer = initTracer(std::move(exporter), new MockSampler());
620+
621+
auto span = tracer->StartSpan("span 1");
622+
// sampler was fed with valid trace_id, so span shouldn't be NoOp Span.
623+
EXPECT_TRUE(span->IsRecording());
624+
EXPECT_TRUE(span->GetContext().IsValid());
625+
}

0 commit comments

Comments
 (0)