Skip to content

Commit

Permalink
Merge branch 'main' into context-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Jul 7, 2021
2 parents ddd3cae + 6ffa848 commit 4db4349
Show file tree
Hide file tree
Showing 30 changed files with 176 additions and 42 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ on each other), the owner should try to get people aligned by:
split it up.

If none of the above worked and the PR has been stuck for more than 2 weeks, the
owner should bring it to the [OpenTelemetry C++ SIG
meeting](https://zoom.us/j/8203130519). The meeting passcode is _77777_.
owner should bring it to the OpenTelemetry C++ SIG meeting. See
[README.md](README.md#contributing) for the meeting link.

## Useful Resources

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contributors' availability. Check the [OpenTelemetry community
calendar](https://calendar.google.com/calendar/embed?src=google.com_b79e3e90j7bbsa2n2p5an5lf60%40group.calendar.google.com)
for specific dates.

Meetings take place via [Zoom video conference](https://zoom.us/j/8203130519).
Meetings take place via [Zoom video conference](https://zoom.us/j/6729396170).
The passcode is _77777_.

Meeting notes are available as a public [Google
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class BaggagePropagator : public context::propagation::TextMapPropagator
auto baggage = Baggage::FromHeader(baggage_str);
return SetBaggage(context, baggage);
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return callback(kBaggageHeader);
}
};
} // namespace propagation
} // namespace baggage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ class CompositePropagator : public TextMapPropagator
return propagators_.size() ? tmp_context : context;
}

/**
* Invoke callback with fields set to carrier by `inject` method for all the
* configured propagators
* Returns true if all invocation return true
*/
bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
bool status = true;
for (auto &p : propagators_)
{
status = status && p->Fields(callback);
}
return status;
}

private:
std::vector<std::unique_ptr<TextMapPropagator>> propagators_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class NoOpPropagator : public TextMapPropagator

/** Noop inject function does nothing */
void Inject(TextMapCarrier & /*carrier*/, const context::Context &context) noexcept override {}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return true;
}
};
} // namespace propagation
} // namespace context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ namespace propagation
class TextMapCarrier
{
public:
/*returns the value associated with the passed key.*/
// returns the value associated with the passed key.
virtual nostd::string_view Get(nostd::string_view key) const noexcept = 0;

/*stores the key-value pair.*/
// stores the key-value pair.
virtual void Set(nostd::string_view key, nostd::string_view value) noexcept = 0;

/* list of all the keys in the carrier.
By default, it returns true without invoking callback */
virtual bool Keys(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept
{
return true;
}
virtual ~TextMapCarrier() = default;
};

// The TextMapPropagator class provides an interface that enables extracting and injecting
Expand All @@ -40,6 +48,11 @@ class TextMapPropagator

// Sets the context for carrier with self defined rules.
virtual void Inject(TextMapCarrier &carrier, const context::Context &context) noexcept = 0;

// Gets the fields set in the carrier by the `inject` method
virtual bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept = 0;

virtual ~TextMapPropagator() = default;
};
} // namespace propagation
} // namespace context
Expand Down
10 changes: 10 additions & 0 deletions api/include/opentelemetry/trace/propagation/b3_propagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ class B3Propagator : public B3PropagatorExtractor

carrier.Set(kB3CombinedHeader, nostd::string_view(trace_identity, sizeof(trace_identity)));
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return callback(kB3CombinedHeader);
}
};

class B3PropagatorMultiHeader : public B3PropagatorExtractor
Expand All @@ -173,6 +178,11 @@ class B3PropagatorMultiHeader : public B3PropagatorExtractor
carrier.Set(kB3SpanIdHeader, nostd::string_view(span_id, sizeof(span_id)));
carrier.Set(kB3SampledHeader, nostd::string_view(trace_flags + 1, 1));
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return callback(kB3TraceIdHeader) && callback(kB3SpanIdHeader) && callback(kB3SampledHeader);
}
};

} // namespace propagation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp

return ExtractContextFromTraceHeaders(trace_parent, trace_state);
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return (callback(kTraceParent) && callback(kTraceState));
}
};
} // namespace propagation
} // namespace trace
Expand Down
11 changes: 8 additions & 3 deletions api/include/opentelemetry/trace/propagation/jaeger.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace trace
namespace propagation
{

static const nostd::string_view kTraceHeader = "uber-trace-id";
static const nostd::string_view kJaegerTraceHeader = "uber-trace-id";

class JaegerPropagator : public context::propagation::TextMapPropagator
{
Expand Down Expand Up @@ -45,7 +45,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator
trace_identity[trace_id_length + span_id_length + 4] = '0';
trace_identity[trace_id_length + span_id_length + 5] = span_context.IsSampled() ? '1' : '0';

carrier.Set(kTraceHeader, nostd::string_view(trace_identity, sizeof(trace_identity)));
carrier.Set(kJaegerTraceHeader, nostd::string_view(trace_identity, sizeof(trace_identity)));
}

context::Context Extract(const context::propagation::TextMapCarrier &carrier,
Expand All @@ -56,6 +56,11 @@ class JaegerPropagator : public context::propagation::TextMapPropagator
return SetSpan(context, sp);
}

bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept override
{
return callback(kJaegerTraceHeader);
}

private:
static constexpr uint8_t kIsSampled = 0x01;

Expand All @@ -67,7 +72,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator

static SpanContext ExtractImpl(const context::propagation::TextMapCarrier &carrier)
{
nostd::string_view trace_identity = carrier.Get(kTraceHeader);
nostd::string_view trace_identity = carrier.Get(kJaegerTraceHeader);

const size_t trace_field_count = 4;
nostd::string_view trace_fields[trace_field_count];
Expand Down
8 changes: 8 additions & 0 deletions api/test/baggage/propagation/baggage_propagator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,13 @@ TEST(BaggagePropagatorTest, ExtractAndInjectBaggage)
BaggageCarrierTest carrier2;
format.Inject(carrier2, ctx2);
EXPECT_EQ(carrier2.headers_[kBaggageHeader.data()], baggage.second);

std::vector<std::string> fields;
format.Fields([&fields](nostd::string_view field) {
fields.push_back(field.data());
return true;
});
EXPECT_EQ(fields.size(), 1);
EXPECT_EQ(fields[0], kBaggageHeader.data());
}
}
10 changes: 10 additions & 0 deletions api/test/context/propagation/composite_propagator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,14 @@ TEST_F(CompositePropagatorTest, Inject)
EXPECT_EQ(carrier.headers_["traceparent"],
"00-0102030405060708090a0b0c0d0e0f10-0102030405060708-01");
EXPECT_EQ(carrier.headers_["b3"], "0102030405060708090a0b0c0d0e0f10-0102030405060708-1");

std::vector<std::string> fields;
composite_propagator_->Fields([&fields](nostd::string_view field) {
fields.push_back(field.data());
return true;
});
EXPECT_EQ(fields.size(), 3);
EXPECT_EQ(fields[0], opentelemetry::trace::propagation::kTraceParent);
EXPECT_EQ(fields[1], opentelemetry::trace::propagation::kTraceState);
EXPECT_EQ(fields[2], opentelemetry::trace::propagation::kB3CombinedHeader);
}
9 changes: 9 additions & 0 deletions api/test/trace/propagation/http_text_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,13 @@ TEST(GlobalPropagator, SetAndGet)
EXPECT_TRUE(carrier.headers_.count("traceparent") > 0);
EXPECT_TRUE(carrier.headers_.count("tracestate") > 0);
EXPECT_EQ(carrier.headers_["tracestate"], trace_state_value);

std::vector<std::string> fields;
propagator->Fields([&fields](nostd::string_view field) {
fields.push_back(field.data());
return true;
});
EXPECT_EQ(fields.size(), 2);
EXPECT_EQ(fields[0], opentelemetry::trace::propagation::kTraceParent);
EXPECT_EQ(fields[1], opentelemetry::trace::propagation::kTraceState);
}
8 changes: 8 additions & 0 deletions api/test/trace/propagation/jaeger_propagation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ TEST(JaegerPropagatorTest, InjectsContext)
format.Inject(carrier, context::RuntimeContext::GetCurrent());
EXPECT_EQ(carrier.headers_["uber-trace-id"],
"0102030405060708090a0b0c0d0e0f10:0102030405060708:0:01");

std::vector<std::string> fields;
format.Fields([&fields](nostd::string_view field) {
fields.push_back(field.data());
return true;
});
EXPECT_EQ(fields.size(), 1);
EXPECT_EQ(fields[0], opentelemetry::trace::propagation::kJaegerTraceHeader);
}

TEST(JaegerPropagatorTest, DoNotInjectInvalidContext)
Expand Down
27 changes: 23 additions & 4 deletions bazel/curl.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ package(features = ["no_copts_tokenization"])

config_setting(
name = "windows",
values = {"cpu": "x64_windows"},
constraint_values = [
"@bazel_tools//platforms:windows",
],
visibility = ["//visibility:private"],
)

config_setting(
name = "osx",
values = {"cpu": "darwin"},
constraint_values = [
"@bazel_tools//platforms:osx",
],
visibility = ["//visibility:private"],
)

Expand All @@ -27,11 +31,26 @@ cc_library(
]),
copts = CURL_COPTS + [
"-DOS=\"os\"",
"-DCURL_EXTERN_SYMBOL=__attribute__((__visibility__(\"default\")))",
],
defines = ["CURL_STATICLIB"],
includes = [
"include/",
"lib/",
],
linkopts = select({
"//:windows": [
"-DEFAULTLIB:ws2_32.lib",
"-DEFAULTLIB:advapi32.lib",
"-DEFAULTLIB:crypt32.lib",
"-DEFAULTLIB:Normaliz.lib",
],
"//:osx": [
"-framework SystemConfiguration",
"-lpthread",
],
"//conditions:default": [
"-lpthread",
],
}),
visibility = ["//visibility:public"],
)
)
6 changes: 3 additions & 3 deletions docs/public/GettingStarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Mark a span as active
auto scope = tracer->WithActiveSpan(span);
This marks a span as active and returns a ``Scope`` object bound to the
lifetime of the span. When the ``Scope`` object is destroyed, the
related span is ended.
This marks a span as active and returns a ``Scope`` object. The scope object
controls how long a span is active. The span remains active for the lifetime
of the scope object.

The concept of an active span is important, as any span that is created
without explicitly specifying a parent is parented to the currently
Expand Down
2 changes: 0 additions & 2 deletions examples/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ cc_binary(
"client.cc",
"tracer_common.h",
],
# TODO: Move copts/linkopts for static CURL usage into shared bzl file.
copts = [
"-DCURL_STATICLIB",
"-DWITH_CURL",
],
linkopts = select({
Expand Down
3 changes: 0 additions & 3 deletions exporters/elasticsearch/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ cc_library(
"include/opentelemetry/exporters/elasticsearch/es_log_exporter.h",
"include/opentelemetry/exporters/elasticsearch/es_log_recordable.h",
],
copts = [
"-DCURL_STATICLIB",
],
linkopts = select({
"//bazel:windows": [
"-DEFAULTLIB:advapi32.lib",
Expand Down
2 changes: 1 addition & 1 deletion exporters/jaeger/test/jaeger_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ TEST(JaegerSpanRecordable, SetInstrumentationLibrary)

std::string library_name = "opentelemetry-cpp";
std::string library_version = "0.1.0";
auto instrumentation_library = InstrumentationLibrary::create(library_name, library_version);
auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version);

rec.SetInstrumentationLibrary(*instrumentation_library);

Expand Down
6 changes: 4 additions & 2 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ if(WITH_OTLP_HTTP)
set_target_properties(opentelemetry_exporter_otlp_http
PROPERTIES EXPORT_NAME otlp_http_exporter)

target_link_libraries(opentelemetry_exporter_otlp_http
PUBLIC opentelemetry_otlp_recordable http_client_curl)
target_link_libraries(
opentelemetry_exporter_otlp_http
PUBLIC opentelemetry_otlp_recordable http_client_curl
nlohmann_json::nlohmann_json)

list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_http)
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class OtlpRecordable final : public sdk::trace::Recordable
/** Dynamically converts the resource of this span into a proto. */
proto::resource::v1::Resource ProtoResource() const noexcept;

proto::common::v1::InstrumentationLibrary GetProtoInstrumentationLibrary() const noexcept;

void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept override;

Expand Down Expand Up @@ -57,6 +59,8 @@ class OtlpRecordable final : public sdk::trace::Recordable
private:
proto::trace::v1::Span span_;
const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
};
} // namespace otlp
} // namespace exporter
Expand Down
3 changes: 2 additions & 1 deletion exporters/otlp/src/otlp_grpc_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>>
for (auto &recordable : spans)
{
auto rec = std::unique_ptr<OtlpRecordable>(static_cast<OtlpRecordable *>(recordable.release()));
*instrumentation_lib->add_spans() = std::move(rec->span());
*instrumentation_lib->add_spans() = std::move(rec->span());
*instrumentation_lib->mutable_instrumentation_library() = rec->GetProtoInstrumentationLibrary();

if (!has_resource)
{
Expand Down
Loading

0 comments on commit 4db4349

Please sign in to comment.