Skip to content

Commit

Permalink
Merge pull request #43 from lightstep/jmacd/envoy_carrier_v2
Browse files Browse the repository at this point in the history
EnvoyCarrier v2
  • Loading branch information
jmacd authored Feb 27, 2017
2 parents bd0f18d + 0973e2d commit f1dc8f3
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ config.h
.dirstamp
stamp-h1
tracer_test
carrier_test
cppclient
flushproto_test
*.pb.h
Expand Down
5 changes: 4 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
EXTRA_DIST = LICENSE lightstep-tracer-common/collector.proto
EXTRA_DIST = \
LICENSE \
lightstep-tracer-common/collector.proto \
lightstep-tracer-common/lightstep_carrier.proto

SUBDIRS = src test
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# by homebrew, see the commands to set PKG_CONFIG_PATH in build.sh.

AC_PREREQ([2.69])
AC_INIT([lightstep-tracer-cpp], [0.35],
AC_INIT([lightstep-tracer-cpp], [0.36],
[https://github.com/lightstep/lightstep-tracer-cpp/issues])
LT_INIT([disable-shared])
AM_INIT_AUTOMAKE([1.14 foreign subdir-objects])
Expand Down
2 changes: 1 addition & 1 deletion lightstep-tracer-common
29 changes: 14 additions & 15 deletions src/c++11/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ PROTOC = @PROTOC@
.PHONY: $(PROTOC)

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

PROTO_SRC = \
$(collector_srcdir)/collector.proto \
$(srcdir)/envoy_carrier.proto
$(proto_srcdir)/collector.proto \
$(proto_srcdir)/lightstep_carrier.proto

PROTO_GEN = \
collector.pb.h \
collector.pb.cc \
envoy_carrier.pb.h \
envoy_carrier.pb.cc
lightstep_carrier.pb.h \
lightstep_carrier.pb.cc

proto.stamp: $(PROTO_SRC)
$(PROTOC) --proto_path=$(collector_srcdir) --cpp_out=$(builddir) $(collector_srcdir)/collector.proto
$(PROTOC) --proto_path=$(srcdir) --cpp_out=$(builddir) $(srcdir)/envoy_carrier.proto
$(PROTOC) --proto_path=$(proto_srcdir) --cpp_out=$(builddir) $(proto_srcdir)/collector.proto
$(PROTOC) --proto_path=$(proto_srcdir) --cpp_out=$(builddir) $(proto_srcdir)/lightstep_carrier.proto
touch proto.stamp

$(PROTO_GEN): proto.stamp
Expand All @@ -34,7 +34,7 @@ CLEANFILES = $(PROTO_GEN) proto.stamp
lib_LTLIBRARIES = liblightstep_core_cxx11.la

nobase_include_HEADERS = \
lightstep/envoy.h \
lightstep/carrier.h \
lightstep/impl.h \
lightstep/options.h \
lightstep/propagation.h \
Expand All @@ -54,11 +54,11 @@ liblightstep_core_cxx11_la_SOURCES = \

nobase_nodist_include_HEADERS = \
collector.pb.h \
envoy_carrier.pb.h
lightstep_carrier.pb.h

nodist_liblightstep_core_cxx11_la_SOURCES = \
collector.pb.cc \
envoy_carrier.pb.cc
lightstep_carrier.pb.cc

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

EXTRA_DIST = \
mapbox_variant/LICENSE \
envoy_carrier.proto
mapbox_variant/LICENSE

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

GRPC_PROTO_SRC = \
$(collector_srcdir)/collector.proto
$(proto_srcdir)/collector.proto

GRPC_PROTO_GEN = \
collector.grpc.pb.h \
Expand All @@ -93,9 +92,9 @@ CLEANFILES += $(GRPC_PROTO_GEN) proto.grpc.stamp
# TODO make grpc_cpp_plugin a configure option
#
proto.grpc.stamp: $(GRPC_PROTO_SRC)
$(PROTOC) --proto_path=$(collector_srcdir) --grpc_out=$(builddir) \
$(PROTOC) --proto_path=$(proto_srcdir) --grpc_out=$(builddir) \
--plugin=protoc-gen-grpc=`which grpc_cpp_plugin` \
$(collector_srcdir)/collector.proto
$(proto_srcdir)/collector.proto
touch proto.grpc.stamp

$(GRPC_PROTO_GEN): proto.grpc.stamp
Expand Down
15 changes: 0 additions & 15 deletions src/c++11/envoy_carrier.proto

This file was deleted.

69 changes: 59 additions & 10 deletions src/c++11/impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "lightstep/envoy.h"
#include "lightstep/carrier.h"
#include "lightstep/impl.h"
#include "lightstep/options.h"
#include "lightstep/tracer.h"
Expand Down Expand Up @@ -134,15 +134,15 @@ bool TracerImpl::inject(SpanContext sc, CarrierFormat format, const CarrierWrite
switch (format.type()) {
case CarrierFormat::HTTPHeaders:
case CarrierFormat::TextMap:
case CarrierFormat::EnvoyProto:
return inject_basic_carrier(sc, opaque);
case CarrierFormat::Binary:
break;
return inject_text_carrier(sc, opaque);
case CarrierFormat::LightStepBinary:
return inject_lightstep_carrier(sc, opaque);
default:
return false;
}
return false;
}

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

bool TracerImpl::inject_lightstep_carrier(SpanContext sc, const CarrierWriter& opaque) {
const ProtoWriter* carrier = dynamic_cast<const ProtoWriter*>(&opaque);
if (carrier == nullptr) {
return false;
}
BinaryCarrier *const output = carrier->output_;
BasicTracerCarrier *const basic = output->mutable_basic_ctx();
basic->set_span_id(sc.span_id());
basic->set_trace_id(sc.trace_id());
basic->set_sampled(true);

auto baggage = basic->mutable_baggage_items();

sc.ForeachBaggageItem([baggage](const std::string& key,
const std::string& value) {
(*baggage)[key] = value;
return true;
});

// TODO Remove the text encoding after [...] has upgraded globally.
TextProtoWriter legacy_writer(output);
inject_text_carrier(sc, legacy_writer);
return true;
}

SpanContext TracerImpl::extract(CarrierFormat format, const CarrierReader& opaque) {
switch (format.type()) {
case CarrierFormat::HTTPHeaders:
case CarrierFormat::TextMap:
case CarrierFormat::EnvoyProto:
return extract_basic_carrier(opaque);
return extract_text_carrier(opaque);
case CarrierFormat::LightStepBinary:
return extract_lightstep_carrier(opaque);
default:
return SpanContext();
}
}

SpanContext TracerImpl::extract_basic_carrier(const CarrierReader& opaque) {
SpanContext TracerImpl::extract_text_carrier(const CarrierReader& opaque) {
const BasicCarrierReader* carrier = dynamic_cast<const BasicCarrierReader*>(&opaque);
if (carrier == nullptr) {
return SpanContext();
Expand Down Expand Up @@ -200,6 +226,29 @@ SpanContext TracerImpl::extract_basic_carrier(const CarrierReader& opaque) {
return SpanContext(ctx);
}

SpanContext TracerImpl::extract_lightstep_carrier(const CarrierReader& opaque) {
const ProtoReader* carrier = dynamic_cast<const ProtoReader*>(&opaque);
if (carrier == nullptr) {
return SpanContext();
}

const BinaryCarrier& proto = carrier->data_;
const BasicTracerCarrier& basic = proto.basic_ctx();
if (basic.trace_id() == 0 && basic.span_id() == 0) {
return extract_text_carrier(TextProtoReader(proto));
}

std::shared_ptr<ContextImpl> ctx(new ContextImpl);
ctx->trace_id = basic.trace_id();
ctx->span_id = basic.span_id();

for (const auto& entry : basic.baggage_items()) {
ctx->setBaggageItem(entry);
}

return SpanContext(ctx);
}

void StartTimestamp::Apply(SpanImpl *span) const {
// Note: no locking, only called from StartSpan
span->start_timestamp_ = when_;
Expand Down
69 changes: 69 additions & 0 deletions src/c++11/lightstep/carrier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// -*- Mode: C++ -*-
//
// Note! Most LightStep client libraries support injecting/extracting
// LightStepCarrier to/from a string type and handle base64
// encoding/decoding as part of the library.
//
// This library DOES NOT PERFORM BASE64 encoding/decoding.
#ifndef __LIGHTSTEP_CARRIER_H__
#define __LIGHTSTEP_CARRIER_H__

#include "lightstep_carrier.pb.h"
#include "lightstep/propagation.h"

namespace lightstep {

// ProtoReader reads a LightStepCarrier.
class ProtoReader : public CarrierReader {
public:
explicit ProtoReader(const BinaryCarrier& data) : data_(data) { }

private:
friend class lightstep::TracerImpl;
const BinaryCarrier& data_;
};

// ProtoReader writes a BinaryCarrier.
class ProtoWriter : public CarrierWriter {
public:
explicit ProtoWriter(BinaryCarrier *output) : output_(output) { }

private:
friend class lightstep::TracerImpl;
BinaryCarrier *const output_;
};

// TextProtoReader/Writer handle reading and writing the opentracing
// text carrier representation.
class TextProtoReader : public BasicCarrierReader {
public:
explicit TextProtoReader(const BinaryCarrier& data) : data_(data) { }

void ForeachKey(std::function<void(const std::string& key,
const std::string& value)> f) const override {
for (const auto& c : data_.text_ctx()) {
f(c.key(), c.value());
}
}

private:
const BinaryCarrier& data_;
};

class TextProtoWriter : public BasicCarrierWriter {
public:
explicit TextProtoWriter(BinaryCarrier *output) : output_(output) { }

void Set(const std::string &key, const std::string &value) const override {
TextCarrierPair *kv = output_->add_text_ctx();
kv->set_key(key);
kv->set_value(value);
}

private:
BinaryCarrier *const output_;
};

} // namespace lightstep

#endif // __LIGHTSTEP_CARRIER_H__
45 changes: 0 additions & 45 deletions src/c++11/lightstep/envoy.h

This file was deleted.

6 changes: 4 additions & 2 deletions src/c++11/lightstep/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ class TracerImpl {
friend class SpanReference;

bool inject(SpanContext sc, CarrierFormat format, const CarrierWriter &writer);
bool inject_basic_carrier(SpanContext sc, const CarrierWriter& opaque);
bool inject_text_carrier(SpanContext sc, const CarrierWriter& opaque);
bool inject_lightstep_carrier(SpanContext sc, const CarrierWriter& opaque);

SpanContext extract(CarrierFormat format, const CarrierReader& reader);
SpanContext extract_basic_carrier(const CarrierReader& reader);
SpanContext extract_text_carrier(const CarrierReader& reader);
SpanContext extract_lightstep_carrier(const CarrierReader& reader);

void GetTwoIds(uint64_t *a, uint64_t *b);
uint64_t GetOneId();
Expand Down
12 changes: 7 additions & 5 deletions src/c++11/lightstep/propagation.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ SpanReference FollowsFrom(const SpanContext& ctx);
class CarrierFormat {
public:
enum FormatType {
// Binary encodes the SpanContext for propagation as opaque binary data.
Binary = 1, // RESERVED, NOT IMPLEMENTED
// OpenTracingBinary encodes the SpanContext for propagation as opaque
// binary data.
OpenTracingBinary = 1, // RESERVED, NOT IMPLEMENTED

// HTTPHeaders represents SpanContexts as HTTP header string pairs.
//
Expand Down Expand Up @@ -170,13 +171,14 @@ class CarrierFormat {
// See the HTTPHeaders examples.
TextMap = 3,

// EnvoyProto carriers use a serialized protocol message.
EnvoyProto = 4,
// LightStepBinary carriers read/write a lightstep::BinaryCarrier
// protobuf message.
LightStepBinary = 4,
};

static CarrierFormat TextMapCarrier;
static CarrierFormat HTTPHeadersCarrier;
static CarrierFormat EnvoyProtoCarrier;
static CarrierFormat LightStepBinaryCarrier;

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

Expand Down
Loading

0 comments on commit f1dc8f3

Please sign in to comment.