Skip to content

Commit c41715d

Browse files
author
Johannes Tax
committed
Set attributes on span start
1 parent b59af35 commit c41715d

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

api/include/opentelemetry/trace/span.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ struct StartSpanOptions
3737
core::SystemTimestamp start_system_time;
3838
core::SteadyTimestamp start_steady_time;
3939

40+
// Optionally set attributes at Span creation.
41+
nostd::span<std::pair<nostd::string_view, common::AttributeValue>> attributes;
42+
4043
// TODO:
4144
// Span(Context?) parent;
4245
// SpanContext remote_parent;
4346
// Links
44-
// Attributes
45-
nostd::span<std::pair<nostd::string_view, common::AttributeValue>> attributes;
4647
SpanKind kind = SpanKind::kInternal;
4748
};
4849
/**

examples/plugin/plugin/tracer.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#include <iostream>
44

5-
namespace nostd = opentelemetry::nostd;
6-
namespace core = opentelemetry::core;
7-
namespace trace = opentelemetry::trace;
5+
namespace nostd = opentelemetry::nostd;
6+
namespace common = opentelemetry::common;
7+
namespace core = opentelemetry::core;
8+
namespace trace = opentelemetry::trace;
89

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

2425
// opentelemetry::trace::Span
26+
void SetAttribute(nostd::string_view /*name*/,
27+
common::AttributeValue && /*value*/) noexcept override
28+
{}
29+
2530
void AddEvent(nostd::string_view /*name*/) noexcept override {}
2631

2732
void AddEvent(nostd::string_view /*name*/, core::SystemTimestamp /*timestamp*/) noexcept override

sdk/include/opentelemetry/sdk/trace/span_data.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ class SpanData final : public Recordable
7272
* Get the attributes for this span
7373
* @return the attributes for this span
7474
*/
75-
const std::unordered_map<std::string, common::AttributeValue>& GetAttributes() const noexcept
75+
const std::unordered_map<std::string, common::AttributeValue> &GetAttributes() const noexcept
7676
{
77-
return attributes_;
77+
return attributes_;
7878
}
7979

8080
void SetIds(opentelemetry::trace::TraceId trace_id,

sdk/src/trace/span.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
5555
processor_->OnStart(*recordable_);
5656
recordable_->SetName(name);
5757

58-
for (auto& attr : options.attributes) {
59-
auto value = attr.second;
60-
recordable_->SetAttribute(attr.first, std::move(value));
58+
for (auto &attr : options.attributes)
59+
{
60+
auto value = attr.second;
61+
recordable_->SetAttribute(attr.first, std::move(value));
6162
}
6263

6364
recordable_->SetStartTime(NowOr(options.start_system_time));

sdk/test/trace/span_data_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "opentelemetry/nostd/variant.h"
21
#include "opentelemetry/sdk/trace/span_data.h"
2+
#include "opentelemetry/nostd/variant.h"
33
#include "opentelemetry/trace/span_id.h"
44
#include "opentelemetry/trace/trace_id.h"
55

sdk/test/trace/tracer_test.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using namespace opentelemetry::sdk::trace;
88
using opentelemetry::core::SteadyTimestamp;
99
using opentelemetry::core::SystemTimestamp;
10+
namespace nostd = opentelemetry::nostd;
11+
namespace common = opentelemetry::common;
1012

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

26-
ExportResult Export(
27-
const opentelemetry::nostd::span<std::unique_ptr<Recordable>> &recordables) noexcept override
28+
ExportResult Export(const nostd::span<std::unique_ptr<Recordable>> &recordables) noexcept override
2829
{
2930
for (auto &recordable : recordables)
3031
{
@@ -120,11 +121,8 @@ TEST(Tracer, StartSpanWithOptionsAttributes)
120121
auto tracer = initTracer(spans_received);
121122

122123
opentelemetry::trace::StartSpanOptions start;
123-
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attributes[] = {
124-
{"attr1", 314159},
125-
{"attr2", false},
126-
{"attr3", "hi"}
127-
};
124+
std::pair<nostd::string_view, common::AttributeValue> attributes[] = {
125+
{"attr1", 314159}, {"attr2", false}, {"attr3", "string"}};
128126
start.attributes = attributes;
129127

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

134132
auto &span_data = spans_received->at(0);
135133
ASSERT_EQ(3, span_data->GetAttributes().size());
136-
ASSERT_EQ(314159, opentelemetry::nostd::get<int>(span_data->GetAttributes().at("attr1")));
137-
ASSERT_EQ(false, opentelemetry::nostd::get<bool>(span_data->GetAttributes().at("attr2")));
138-
ASSERT_EQ("hi", opentelemetry::nostd::get<opentelemetry::nostd::string_view>(span_data->GetAttributes().at("attr3")));
134+
ASSERT_EQ(314159, nostd::get<int>(span_data->GetAttributes().at("attr1")));
135+
ASSERT_EQ(false, nostd::get<bool>(span_data->GetAttributes().at("attr2")));
136+
ASSERT_EQ("string", nostd::get<nostd::string_view>(span_data->GetAttributes().at("attr3")));
139137
}

0 commit comments

Comments
 (0)