Skip to content

Commit

Permalink
Set attributes on span start
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Tax committed Jun 16, 2020
1 parent 235d22a commit e7e2f44
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ struct StartSpanOptions
core::SystemTimestamp start_system_time;
core::SteadyTimestamp start_steady_time;

// Optionally set attributes at Span creation.
nostd::span<std::pair<nostd::string_view, common::AttributeValue>> attributes;

// TODO:
// Span(Context?) parent;
// SpanContext remote_parent;
// Links
// Attributes
nostd::span<std::pair<nostd::string_view, common::AttributeValue>> attributes;
SpanKind kind = SpanKind::kInternal;
};
/**
Expand Down
11 changes: 8 additions & 3 deletions examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include <iostream>

namespace nostd = opentelemetry::nostd;
namespace core = opentelemetry::core;
namespace trace = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;
namespace common = opentelemetry::common;
namespace core = opentelemetry::core;
namespace trace = opentelemetry::trace;

namespace
{
Expand All @@ -22,6 +23,10 @@ class Span final : public trace::Span
~Span() { std::cout << "~Span\n"; }

// opentelemetry::trace::Span
void SetAttribute(nostd::string_view /*name*/,
common::AttributeValue && /*value*/) noexcept override
{}

void AddEvent(nostd::string_view /*name*/) noexcept override {}

void AddEvent(nostd::string_view /*name*/, core::SystemTimestamp /*timestamp*/) noexcept override
Expand Down
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class SpanData final : public Recordable
* Get the attributes for this span
* @return the attributes for this span
*/
const std::unordered_map<std::string, common::AttributeValue>& GetAttributes() const noexcept
const std::unordered_map<std::string, common::AttributeValue> &GetAttributes() const noexcept
{
return attributes_;
return attributes_;
}

void SetIds(opentelemetry::trace::TraceId trace_id,
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
processor_->OnStart(*recordable_);
recordable_->SetName(name);

for (auto& attr : options.attributes) {
auto value = attr.second;
recordable_->SetAttribute(attr.first, std::move(value));
for (auto &attr : options.attributes)
{
auto value = attr.second;
recordable_->SetAttribute(attr.first, std::move(value));
}

recordable_->SetStartTime(NowOr(options.start_system_time));
Expand Down
2 changes: 1 addition & 1 deletion sdk/test/trace/span_data_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/trace_id.h"

Expand Down
18 changes: 8 additions & 10 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using namespace opentelemetry::sdk::trace;
using opentelemetry::core::SteadyTimestamp;
using opentelemetry::core::SystemTimestamp;
namespace nostd = opentelemetry::nostd;
namespace common = opentelemetry::common;

/**
* A mock exporter that switches a flag once a valid recordable was received.
Expand All @@ -23,8 +25,7 @@ class MockSpanExporter final : public SpanExporter
return std::unique_ptr<Recordable>(new SpanData);
}

ExportResult Export(
const opentelemetry::nostd::span<std::unique_ptr<Recordable>> &recordables) noexcept override
ExportResult Export(const nostd::span<std::unique_ptr<Recordable>> &recordables) noexcept override
{
for (auto &recordable : recordables)
{
Expand Down Expand Up @@ -120,11 +121,8 @@ TEST(Tracer, StartSpanWithOptionsAttributes)
auto tracer = initTracer(spans_received);

opentelemetry::trace::StartSpanOptions start;
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attributes[] = {
{"attr1", 314159},
{"attr2", false},
{"attr3", "hi"}
};
std::pair<nostd::string_view, common::AttributeValue> attributes[] = {
{"attr1", 314159}, {"attr2", false}, {"attr3", "string"}};
start.attributes = attributes;

tracer->StartSpan("span 1", start)->End();
Expand All @@ -133,7 +131,7 @@ TEST(Tracer, StartSpanWithOptionsAttributes)

auto &span_data = spans_received->at(0);
ASSERT_EQ(3, span_data->GetAttributes().size());
ASSERT_EQ(314159, opentelemetry::nostd::get<int>(span_data->GetAttributes().at("attr1")));
ASSERT_EQ(false, opentelemetry::nostd::get<bool>(span_data->GetAttributes().at("attr2")));
ASSERT_EQ("hi", opentelemetry::nostd::get<opentelemetry::nostd::string_view>(span_data->GetAttributes().at("attr3")));
ASSERT_EQ(314159, nostd::get<int>(span_data->GetAttributes().at("attr1")));
ASSERT_EQ(false, nostd::get<bool>(span_data->GetAttributes().at("attr2")));
ASSERT_EQ("string", nostd::get<nostd::string_view>(span_data->GetAttributes().at("attr3")));
}

0 comments on commit e7e2f44

Please sign in to comment.