Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit df7c373

Browse files
authored
Add lock to SpanContext to protect baggage (#83)
Signed-off-by: Isaac Hier <[email protected]>
1 parent 9521a8e commit df7c373

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/jaegertracing/SpanContext.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <iomanip>
2121
#include <iostream>
22+
#include <mutex>
2223
#include <string>
2324
#include <unordered_map>
2425

@@ -41,6 +42,7 @@ class SpanContext : public opentracing::SpanContext {
4142
, _spanID(0)
4243
, _parentID(0)
4344
, _flags(0)
45+
, _mutex()
4446
{
4547
}
4648

@@ -56,6 +58,7 @@ class SpanContext : public opentracing::SpanContext {
5658
, _flags(flags)
5759
, _baggage(baggage)
5860
, _debugID(debugID)
61+
, _mutex()
5962
{
6063
}
6164

@@ -98,13 +101,15 @@ class SpanContext : public opentracing::SpanContext {
98101

99102
SpanContext withBaggage(const StrMap& baggage) const
100103
{
104+
std::lock_guard<std::mutex> lock(_mutex);
101105
return SpanContext(
102106
_traceID, _spanID, _parentID, _flags, baggage, _debugID);
103107
}
104108

105109
template <typename Function>
106110
void forEachBaggageItem(Function f) const
107111
{
112+
std::lock_guard<std::mutex> lock(_mutex);
108113
for (auto&& pair : _baggage) {
109114
if (!f(pair.first, pair.second)) {
110115
break;
@@ -115,6 +120,7 @@ class SpanContext : public opentracing::SpanContext {
115120
template <typename Function>
116121
void forEachBaggageItem(Function f)
117122
{
123+
std::lock_guard<std::mutex> lock(_mutex);
118124
for (auto&& pair : _baggage) {
119125
if (!f(pair.first, pair.second)) {
120126
break;
@@ -160,9 +166,17 @@ class SpanContext : public opentracing::SpanContext {
160166

161167
bool operator==(const SpanContext& rhs) const
162168
{
169+
{
170+
std::lock(_mutex, rhs._mutex);
171+
std::lock_guard<std::mutex> lock(_mutex, std::adopt_lock);
172+
std::lock_guard<std::mutex> rhsLock(rhs._mutex, std::adopt_lock);
173+
if (_baggage != rhs._baggage) {
174+
return false;
175+
}
176+
}
163177
return _traceID == rhs._traceID && _spanID == rhs._spanID &&
164178
_parentID == rhs._parentID && _flags == rhs._flags &&
165-
_baggage == rhs._baggage && _debugID == rhs._debugID;
179+
_debugID == rhs._debugID;
166180
}
167181

168182
private:
@@ -172,6 +186,7 @@ class SpanContext : public opentracing::SpanContext {
172186
unsigned char _flags;
173187
StrMap _baggage;
174188
std::string _debugID;
189+
mutable std::mutex _mutex; // Protects _baggage.
175190
};
176191

177192
} // namespace jaegertracing

0 commit comments

Comments
 (0)