Skip to content

Commit f1dc8f3

Browse files
authored
Merge pull request #43 from lightstep/jmacd/envoy_carrier_v2
EnvoyCarrier v2
2 parents bd0f18d + 0973e2d commit f1dc8f3

16 files changed

+388
-135
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ config.h
2828
.dirstamp
2929
stamp-h1
3030
tracer_test
31+
carrier_test
3132
cppclient
3233
flushproto_test
3334
*.pb.h

Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
EXTRA_DIST = LICENSE lightstep-tracer-common/collector.proto
1+
EXTRA_DIST = \
2+
LICENSE \
3+
lightstep-tracer-common/collector.proto \
4+
lightstep-tracer-common/lightstep_carrier.proto
25

36
SUBDIRS = src test

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# by homebrew, see the commands to set PKG_CONFIG_PATH in build.sh.
55

66
AC_PREREQ([2.69])
7-
AC_INIT([lightstep-tracer-cpp], [0.35],
7+
AC_INIT([lightstep-tracer-cpp], [0.36],
88
[https://github.com/lightstep/lightstep-tracer-cpp/issues])
99
LT_INIT([disable-shared])
1010
AM_INIT_AUTOMAKE([1.14 foreign subdir-objects])

src/c++11/Makefile.am

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ PROTOC = @PROTOC@
77
.PHONY: $(PROTOC)
88

99
# Lightstep Collector reporting:
10-
collector_srcdir = $(top_srcdir)/lightstep-tracer-common
10+
proto_srcdir = $(top_srcdir)/lightstep-tracer-common
1111

1212
PROTO_SRC = \
13-
$(collector_srcdir)/collector.proto \
14-
$(srcdir)/envoy_carrier.proto
13+
$(proto_srcdir)/collector.proto \
14+
$(proto_srcdir)/lightstep_carrier.proto
1515

1616
PROTO_GEN = \
1717
collector.pb.h \
1818
collector.pb.cc \
19-
envoy_carrier.pb.h \
20-
envoy_carrier.pb.cc
19+
lightstep_carrier.pb.h \
20+
lightstep_carrier.pb.cc
2121

2222
proto.stamp: $(PROTO_SRC)
23-
$(PROTOC) --proto_path=$(collector_srcdir) --cpp_out=$(builddir) $(collector_srcdir)/collector.proto
24-
$(PROTOC) --proto_path=$(srcdir) --cpp_out=$(builddir) $(srcdir)/envoy_carrier.proto
23+
$(PROTOC) --proto_path=$(proto_srcdir) --cpp_out=$(builddir) $(proto_srcdir)/collector.proto
24+
$(PROTOC) --proto_path=$(proto_srcdir) --cpp_out=$(builddir) $(proto_srcdir)/lightstep_carrier.proto
2525
touch proto.stamp
2626

2727
$(PROTO_GEN): proto.stamp
@@ -34,7 +34,7 @@ CLEANFILES = $(PROTO_GEN) proto.stamp
3434
lib_LTLIBRARIES = liblightstep_core_cxx11.la
3535

3636
nobase_include_HEADERS = \
37-
lightstep/envoy.h \
37+
lightstep/carrier.h \
3838
lightstep/impl.h \
3939
lightstep/options.h \
4040
lightstep/propagation.h \
@@ -54,11 +54,11 @@ liblightstep_core_cxx11_la_SOURCES = \
5454

5555
nobase_nodist_include_HEADERS = \
5656
collector.pb.h \
57-
envoy_carrier.pb.h
57+
lightstep_carrier.pb.h
5858

5959
nodist_liblightstep_core_cxx11_la_SOURCES = \
6060
collector.pb.cc \
61-
envoy_carrier.pb.cc
61+
lightstep_carrier.pb.cc
6262

6363
# Note: To set the protobuf library include and library flags, e.g.,
6464
#
@@ -71,8 +71,7 @@ liblightstep_core_cxx11_la_LDFLAGS = $(protobuf_LIBS)
7171
AM_CXXFLAGS = -Wno-deprecated-declarations
7272

7373
EXTRA_DIST = \
74-
mapbox_variant/LICENSE \
75-
envoy_carrier.proto
74+
mapbox_variant/LICENSE
7675

7776
# The builtin gRPC transport is built by default. When supplying your
7877
# own gRPC transport, you can disable the built-in recorder:
@@ -81,7 +80,7 @@ EXTRA_DIST = \
8180
if ENABLE_GRPC
8281

8382
GRPC_PROTO_SRC = \
84-
$(collector_srcdir)/collector.proto
83+
$(proto_srcdir)/collector.proto
8584

8685
GRPC_PROTO_GEN = \
8786
collector.grpc.pb.h \
@@ -93,9 +92,9 @@ CLEANFILES += $(GRPC_PROTO_GEN) proto.grpc.stamp
9392
# TODO make grpc_cpp_plugin a configure option
9493
#
9594
proto.grpc.stamp: $(GRPC_PROTO_SRC)
96-
$(PROTOC) --proto_path=$(collector_srcdir) --grpc_out=$(builddir) \
95+
$(PROTOC) --proto_path=$(proto_srcdir) --grpc_out=$(builddir) \
9796
--plugin=protoc-gen-grpc=`which grpc_cpp_plugin` \
98-
$(collector_srcdir)/collector.proto
97+
$(proto_srcdir)/collector.proto
9998
touch proto.grpc.stamp
10099

101100
$(GRPC_PROTO_GEN): proto.grpc.stamp

src/c++11/envoy_carrier.proto

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/c++11/impl.cc

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifdef HAVE_CONFIG_H
88
#include "config.h"
99
#endif
10-
#include "lightstep/envoy.h"
10+
#include "lightstep/carrier.h"
1111
#include "lightstep/impl.h"
1212
#include "lightstep/options.h"
1313
#include "lightstep/tracer.h"
@@ -134,15 +134,15 @@ bool TracerImpl::inject(SpanContext sc, CarrierFormat format, const CarrierWrite
134134
switch (format.type()) {
135135
case CarrierFormat::HTTPHeaders:
136136
case CarrierFormat::TextMap:
137-
case CarrierFormat::EnvoyProto:
138-
return inject_basic_carrier(sc, opaque);
139-
case CarrierFormat::Binary:
140-
break;
137+
return inject_text_carrier(sc, opaque);
138+
case CarrierFormat::LightStepBinary:
139+
return inject_lightstep_carrier(sc, opaque);
140+
default:
141+
return false;
141142
}
142-
return false;
143143
}
144144

145-
bool TracerImpl::inject_basic_carrier(SpanContext sc, const CarrierWriter& opaque) {
145+
bool TracerImpl::inject_text_carrier(SpanContext sc, const CarrierWriter& opaque) {
146146
const BasicCarrierWriter* carrier = dynamic_cast<const BasicCarrierWriter*>(&opaque);
147147
if (carrier == nullptr) {
148148
return false;
@@ -159,18 +159,44 @@ bool TracerImpl::inject_basic_carrier(SpanContext sc, const CarrierWriter& opaqu
159159
return true;
160160
}
161161

162+
bool TracerImpl::inject_lightstep_carrier(SpanContext sc, const CarrierWriter& opaque) {
163+
const ProtoWriter* carrier = dynamic_cast<const ProtoWriter*>(&opaque);
164+
if (carrier == nullptr) {
165+
return false;
166+
}
167+
BinaryCarrier *const output = carrier->output_;
168+
BasicTracerCarrier *const basic = output->mutable_basic_ctx();
169+
basic->set_span_id(sc.span_id());
170+
basic->set_trace_id(sc.trace_id());
171+
basic->set_sampled(true);
172+
173+
auto baggage = basic->mutable_baggage_items();
174+
175+
sc.ForeachBaggageItem([baggage](const std::string& key,
176+
const std::string& value) {
177+
(*baggage)[key] = value;
178+
return true;
179+
});
180+
181+
// TODO Remove the text encoding after [...] has upgraded globally.
182+
TextProtoWriter legacy_writer(output);
183+
inject_text_carrier(sc, legacy_writer);
184+
return true;
185+
}
186+
162187
SpanContext TracerImpl::extract(CarrierFormat format, const CarrierReader& opaque) {
163188
switch (format.type()) {
164189
case CarrierFormat::HTTPHeaders:
165190
case CarrierFormat::TextMap:
166-
case CarrierFormat::EnvoyProto:
167-
return extract_basic_carrier(opaque);
191+
return extract_text_carrier(opaque);
192+
case CarrierFormat::LightStepBinary:
193+
return extract_lightstep_carrier(opaque);
168194
default:
169195
return SpanContext();
170196
}
171197
}
172198

173-
SpanContext TracerImpl::extract_basic_carrier(const CarrierReader& opaque) {
199+
SpanContext TracerImpl::extract_text_carrier(const CarrierReader& opaque) {
174200
const BasicCarrierReader* carrier = dynamic_cast<const BasicCarrierReader*>(&opaque);
175201
if (carrier == nullptr) {
176202
return SpanContext();
@@ -200,6 +226,29 @@ SpanContext TracerImpl::extract_basic_carrier(const CarrierReader& opaque) {
200226
return SpanContext(ctx);
201227
}
202228

229+
SpanContext TracerImpl::extract_lightstep_carrier(const CarrierReader& opaque) {
230+
const ProtoReader* carrier = dynamic_cast<const ProtoReader*>(&opaque);
231+
if (carrier == nullptr) {
232+
return SpanContext();
233+
}
234+
235+
const BinaryCarrier& proto = carrier->data_;
236+
const BasicTracerCarrier& basic = proto.basic_ctx();
237+
if (basic.trace_id() == 0 && basic.span_id() == 0) {
238+
return extract_text_carrier(TextProtoReader(proto));
239+
}
240+
241+
std::shared_ptr<ContextImpl> ctx(new ContextImpl);
242+
ctx->trace_id = basic.trace_id();
243+
ctx->span_id = basic.span_id();
244+
245+
for (const auto& entry : basic.baggage_items()) {
246+
ctx->setBaggageItem(entry);
247+
}
248+
249+
return SpanContext(ctx);
250+
}
251+
203252
void StartTimestamp::Apply(SpanImpl *span) const {
204253
// Note: no locking, only called from StartSpan
205254
span->start_timestamp_ = when_;

src/c++11/lightstep/carrier.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// -*- Mode: C++ -*-
2+
//
3+
// Note! Most LightStep client libraries support injecting/extracting
4+
// LightStepCarrier to/from a string type and handle base64
5+
// encoding/decoding as part of the library.
6+
//
7+
// This library DOES NOT PERFORM BASE64 encoding/decoding.
8+
#ifndef __LIGHTSTEP_CARRIER_H__
9+
#define __LIGHTSTEP_CARRIER_H__
10+
11+
#include "lightstep_carrier.pb.h"
12+
#include "lightstep/propagation.h"
13+
14+
namespace lightstep {
15+
16+
// ProtoReader reads a LightStepCarrier.
17+
class ProtoReader : public CarrierReader {
18+
public:
19+
explicit ProtoReader(const BinaryCarrier& data) : data_(data) { }
20+
21+
private:
22+
friend class lightstep::TracerImpl;
23+
const BinaryCarrier& data_;
24+
};
25+
26+
// ProtoReader writes a BinaryCarrier.
27+
class ProtoWriter : public CarrierWriter {
28+
public:
29+
explicit ProtoWriter(BinaryCarrier *output) : output_(output) { }
30+
31+
private:
32+
friend class lightstep::TracerImpl;
33+
BinaryCarrier *const output_;
34+
};
35+
36+
// TextProtoReader/Writer handle reading and writing the opentracing
37+
// text carrier representation.
38+
class TextProtoReader : public BasicCarrierReader {
39+
public:
40+
explicit TextProtoReader(const BinaryCarrier& data) : data_(data) { }
41+
42+
void ForeachKey(std::function<void(const std::string& key,
43+
const std::string& value)> f) const override {
44+
for (const auto& c : data_.text_ctx()) {
45+
f(c.key(), c.value());
46+
}
47+
}
48+
49+
private:
50+
const BinaryCarrier& data_;
51+
};
52+
53+
class TextProtoWriter : public BasicCarrierWriter {
54+
public:
55+
explicit TextProtoWriter(BinaryCarrier *output) : output_(output) { }
56+
57+
void Set(const std::string &key, const std::string &value) const override {
58+
TextCarrierPair *kv = output_->add_text_ctx();
59+
kv->set_key(key);
60+
kv->set_value(value);
61+
}
62+
63+
private:
64+
BinaryCarrier *const output_;
65+
};
66+
67+
} // namespace lightstep
68+
69+
#endif // __LIGHTSTEP_CARRIER_H__

src/c++11/lightstep/envoy.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/c++11/lightstep/impl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ class TracerImpl {
102102
friend class SpanReference;
103103

104104
bool inject(SpanContext sc, CarrierFormat format, const CarrierWriter &writer);
105-
bool inject_basic_carrier(SpanContext sc, const CarrierWriter& opaque);
105+
bool inject_text_carrier(SpanContext sc, const CarrierWriter& opaque);
106+
bool inject_lightstep_carrier(SpanContext sc, const CarrierWriter& opaque);
106107

107108
SpanContext extract(CarrierFormat format, const CarrierReader& reader);
108-
SpanContext extract_basic_carrier(const CarrierReader& reader);
109+
SpanContext extract_text_carrier(const CarrierReader& reader);
110+
SpanContext extract_lightstep_carrier(const CarrierReader& reader);
109111

110112
void GetTwoIds(uint64_t *a, uint64_t *b);
111113
uint64_t GetOneId();

src/c++11/lightstep/propagation.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ SpanReference FollowsFrom(const SpanContext& ctx);
127127
class CarrierFormat {
128128
public:
129129
enum FormatType {
130-
// Binary encodes the SpanContext for propagation as opaque binary data.
131-
Binary = 1, // RESERVED, NOT IMPLEMENTED
130+
// OpenTracingBinary encodes the SpanContext for propagation as opaque
131+
// binary data.
132+
OpenTracingBinary = 1, // RESERVED, NOT IMPLEMENTED
132133

133134
// HTTPHeaders represents SpanContexts as HTTP header string pairs.
134135
//
@@ -170,13 +171,14 @@ class CarrierFormat {
170171
// See the HTTPHeaders examples.
171172
TextMap = 3,
172173

173-
// EnvoyProto carriers use a serialized protocol message.
174-
EnvoyProto = 4,
174+
// LightStepBinary carriers read/write a lightstep::BinaryCarrier
175+
// protobuf message.
176+
LightStepBinary = 4,
175177
};
176178

177179
static CarrierFormat TextMapCarrier;
178180
static CarrierFormat HTTPHeadersCarrier;
179-
static CarrierFormat EnvoyProtoCarrier;
181+
static CarrierFormat LightStepBinaryCarrier;
180182

181183
CarrierFormat(FormatType type) : type_(type) { }
182184

0 commit comments

Comments
 (0)