Skip to content

Commit

Permalink
Merge branch 'main' into zipkin-kind
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Apr 28, 2021
2 parents 7e114fc + 7f5252f commit 6e9ca0d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Recordable final : public sdk::trace::Recordable

void SetDuration(std::chrono::nanoseconds duration) noexcept override;

void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept override;

private:
proto::trace::v1::Span span_;
};
Expand Down
8 changes: 8 additions & 0 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept
const uint64_t unix_end_time = span_.start_time_unix_nano() + duration.count();
span_.set_end_time_unix_nano(unix_end_time);
}

void Recordable::SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept
{
// TODO: add instrumentation library to OTLP exporter.
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class Recordable final : public sdk::trace::Recordable

void SetDuration(std::chrono::nanoseconds duration) noexcept override;

void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept override;

private:
ZipkinSpan span_;
};
Expand Down
9 changes: 9 additions & 0 deletions exporters/zipkin/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ void Recordable::SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept
span_["kind"] = span_iter->second;
}
}

void Recordable::SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept
{
span_["tags"]["otel.library.name"] = instrumentation_library.GetName();
span_["tags"]["otel.library.version"] = instrumentation_library.GetVersion();
}

} // namespace zipkin
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
15 changes: 15 additions & 0 deletions exporters/zipkin/test/zipkin_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ TEST(ZipkinSpanRecordable, SetDuration)
EXPECT_EQ(rec.span(), j_span);
}

TEST(ZipkinSpanRecordable, SetInstrumentationLibrary)
{
using InstrumentationLibrary = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary;

const char *library_name = "otel-cpp";
const char *library_version = "0.5.0";
json j_span = {
{"tags", {{"otel.library.name", library_name}, {"otel.library.version", library_version}}}};
opentelemetry::exporter::zipkin::Recordable rec;

rec.SetInstrumentationLibrary(*InstrumentationLibrary::create(library_name, library_version));

EXPECT_EQ(rec.span(), j_span);
}

TEST(ZipkinSpanRecordable, SetStatus)
{
std::string description = "Error description";
Expand Down
13 changes: 12 additions & 1 deletion ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
duration_ = duration;
}

void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept override
{
std::lock_guard<std::mutex> lock(mutex_);
instrumentation_library_ = &instrumentation_library;
}

void AddLink(const opentelemetry::trace::SpanContext &span_context,
const opentelemetry::common::KeyValueIterable &attributes =
opentelemetry::common::KeyValueIterableView<std::map<std::string, int>>(
Expand Down Expand Up @@ -212,7 +220,8 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
status_desc_(threadsafe_span_data.status_desc_),
attributes_(threadsafe_span_data.attributes_),
events_(threadsafe_span_data.events_),
converter_(threadsafe_span_data.converter_)
converter_(threadsafe_span_data.converter_),
instrumentation_library_(threadsafe_span_data.instrumentation_library_)
{}

mutable std::mutex mutex_;
Expand All @@ -227,6 +236,8 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> attributes_;
std::vector<opentelemetry::sdk::trace::SpanDataEvent> events_;
opentelemetry::sdk::common::AttributeConverter converter_;
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_;
};
} // namespace zpages
} // namespace ext
Expand Down
11 changes: 11 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/recordable.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/empty_attributes.h"
#include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
#include "opentelemetry/trace/canonical_code.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_context.h"
Expand All @@ -21,6 +22,9 @@ namespace sdk
{
namespace trace
{

using namespace opentelemetry::sdk::instrumentationlibrary;

/**
* Maintains a representation of a span in a format that can be processed by a recorder.
*
Expand Down Expand Up @@ -125,6 +129,13 @@ class Recordable
* @param duration the duration to set
*/
virtual void SetDuration(std::chrono::nanoseconds duration) noexcept = 0;

/**
* Set the instrumentation library of the span.
* @param instrumentation_library the instrumentation library to set
*/
virtual void SetInstrumentationLibrary(
const InstrumentationLibrary &instrumentation_library) noexcept = 0;
};
} // namespace trace
} // namespace sdk
Expand Down
7 changes: 7 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ class SpanData final : public Recordable

void SetDuration(std::chrono::nanoseconds duration) noexcept override { duration_ = duration; }

void SetInstrumentationLibrary(
const InstrumentationLibrary &instrumentation_library) noexcept override
{
instrumentation_library_ = &instrumentation_library;
}

private:
opentelemetry::trace::SpanContext span_context_{false, false};
opentelemetry::trace::SpanId parent_span_id_;
Expand All @@ -244,6 +250,7 @@ class SpanData final : public Recordable
std::vector<SpanDataEvent> events_;
std::vector<SpanDataLink> links_;
opentelemetry::trace::SpanKind span_kind_{opentelemetry::trace::SpanKind::kInternal};
const InstrumentationLibrary *instrumentation_library_;
};
} // namespace trace
} // namespace sdk
Expand Down
1 change: 1 addition & 0 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
return;
}
recordable_->SetName(name);
recordable_->SetInstrumentationLibrary(tracer_->GetInstrumentationLibrary());

trace_api::TraceId trace_id;
trace_api::SpanId span_id = tracer_->GetIdGenerator().GenerateSpanId();
Expand Down

0 comments on commit 6e9ca0d

Please sign in to comment.