Skip to content

Commit

Permalink
Merge pull request open-telemetry#6 from Tianlin-Zhao/origin/propagators
Browse files Browse the repository at this point in the history
Origin/propagators
  • Loading branch information
Tianlin-Zhao authored Jul 29, 2020
2 parents d826bb5 + f035678 commit 591e76e
Show file tree
Hide file tree
Showing 126 changed files with 6,226 additions and 418 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# bazel configurations for running tests under sanitizers.
# Based on https://github.com/bazelment/trunk/blob/master/tools/bazel.rc

# Needed by gRPC to build on some platforms.
build --copt -DGRPC_BAZEL_BUILD

# --config=asan : Address Sanitizer.
common:asan --copt -fsanitize=address
common:asan --copt -DADDRESS_SANITIZER
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.4.1
2 changes: 1 addition & 1 deletion .github/.codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ comment:

# Relative file path fixing.
# CI file paths must match Git file paths.
# This fix removes the "/home/runner/" prefix
# This fix removes the "/home/runner/" prefix
# to coverage report file paths.
fixes:
- "/home/runner/::"
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ jobs:
sudo ./ci/install_gcc48.sh
- name: run tests
run: ./ci/do_ci.sh bazel.legacy.test
env:
CC: /usr/bin/gcc-4.8

bazel_test:
name: Bazel
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ add_subdirectory(sdk)
include_directories(.)
add_subdirectory(exporters)
add_subdirectory(examples)
add_subdirectory(ext)
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# OpenTelemetry C++

[![Gitter chat][gitter-image]][gitter-url]

[gitter-image]: https://badges.gitter.im/open-telemetry/opentelemetry-cpp.svg
[gitter-url]: https://gitter.im/open-telemetry/opentelemetry-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
[![Gitter chat](https://badges.gitter.im/open-telemetry/opentelemetry-cpp.svg)](https://gitter.im/open-telemetry/opentelemetry-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![codecov.io](https://codecov.io/gh/open-telemetry/opentelemetry-cpp/branch/master/graphs/badge.svg?)](https://codecov.io/gh/open-telemetry/opentelemetry-cpp/)
[![Build Status](https://action-badges.now.sh/open-telemetry/opentelemetry-cpp)](https://github.com/open-telemetry/opentelemetry-cpp/actions)
[![Release](https://img.shields.io/github/v/release/open-telemetry/opentelemetry-cpp?include_prereleases&style=)](https://github.com/open-telemetry/opentelemetry-cpp/releases/)

The C++ [OpenTelemetry](https://opentelemetry.io/) client.

Expand Down
33 changes: 33 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ workspace(name = "io_opentelemetry_cpp")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Load gRPC dependency
# Note that this dependency needs to be loaded first due to
# https://github.com/bazelbuild/bazel/issues/6664
http_archive(
name = "com_github_grpc_grpc",
strip_prefix = "grpc-1.28.0",
urls = [
"https://github.com/grpc/grpc/archive/v1.28.0.tar.gz",
],
)

load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

# Load extra gRPC dependencies due to https://github.com/grpc/grpc/issues/20511
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")

grpc_extra_deps()

load("@upb//bazel:repository_defs.bzl", "bazel_version_repository")

bazel_version_repository(name = "upb_bazel_version")

# Uses older protobuf version because of
# https://github.com/protocolbuffers/protobuf/issues/7179
http_archive(
Expand Down Expand Up @@ -54,3 +78,12 @@ http_archive(
strip_prefix = "benchmark-1.5.0",
urls = ["https://github.com/google/benchmark/archive/v1.5.0.tar.gz"],
)

http_archive(
name = "github_nlohmann_json",
build_file = "//third_party:nlohmann_json.BUILD",
sha256 = "69cc88207ce91347ea530b227ff0776db82dcb8de6704e1a3d74f4841bc651cf",
urls = [
"https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip",
],
)
7 changes: 4 additions & 3 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Context
{

public:
Context() = default;
// Creates a context object from a map of keys and identifiers, this will
// hold a shared_ptr to the head of the DataList linked list
template <class T>
Expand Down Expand Up @@ -89,9 +90,9 @@ class Context
return false;
}

private:
Context() = default;
bool operator==(const Context &other) { return (head_ == other.head_); }

private:
// A linked list to contain the keys and values of this context node
class DataList
{
Expand Down Expand Up @@ -163,4 +164,4 @@ class Context
nostd::shared_ptr<DataList> head_;
};
} // namespace context
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
61 changes: 61 additions & 0 deletions api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include "opentelemetry/context/context.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{
// Provides a wrapper for propagating the context object globally. In order
// to use either the threadlocal_context.h file must be included or another
// implementation which must be derived from the RuntimeContext can be
// provided.
class RuntimeContext
{
public:
class Token
{
public:
bool operator==(const Context &other) noexcept { return context_ == other; }

~Token() noexcept { Detach(*this); }

private:
friend class RuntimeContext;

// A constructor that sets the token's Context object to the
// one that was passed in.
Token(Context context) noexcept : context_(context){};

Token() noexcept = default;

Context context_;
};

// Return the current context.
static Context GetCurrent() noexcept { return context_handler_->InternalGetCurrent(); }

// Sets the current 'Context' object. Returns a token
// that can be used to reset to the previous Context.
static Token Attach(Context context) noexcept
{
return context_handler_->InternalAttach(context);
}

// Resets the context to a previous value stored in the
// passed in token. Returns true if successful, false otherwise
static bool Detach(Token &token) noexcept { return context_handler_->InternalDetach(token); }

static RuntimeContext *context_handler_;

protected:
// Provides a token with the passed in context
Token CreateToken(Context context) noexcept { return Token(context); }

virtual Context InternalGetCurrent() noexcept = 0;

virtual Token InternalAttach(Context context) noexcept = 0;

virtual bool InternalDetach(Token &token) noexcept = 0;
};
} // namespace context
OPENTELEMETRY_END_NAMESPACE
116 changes: 116 additions & 0 deletions api/include/opentelemetry/context/threadlocal_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#pragma once

#include "opentelemetry/context/context.h"
#include "opentelemetry/context/runtime_context.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{

// The ThreadLocalContext class is a derived class from RuntimeContext and
// provides a wrapper for propogating context through cpp thread locally.
// This file must be included to use the RuntimeContext class if another
// implementation has not been registered.
class ThreadLocalContext : public RuntimeContext
{
public:
ThreadLocalContext() noexcept = default;

// Return the current context.
Context InternalGetCurrent() noexcept override { return stack_.Top(); }

// Resets the context to a previous value stored in the
// passed in token. Returns true if successful, false otherwise
bool InternalDetach(Token &token) noexcept override
{
if (!(token == stack_.Top()))
{
return false;
}
stack_.Pop();
return true;
}

// Sets the current 'Context' object. Returns a token
// that can be used to reset to the previous Context.
Token InternalAttach(Context context) noexcept override
{
stack_.Push(context);
Token old_context = CreateToken(context);
return old_context;
}

private:
// A nested class to store the attached contexts in a stack.
class Stack
{
friend class ThreadLocalContext;

Stack() noexcept : size_(0), capacity_(0), base_(nullptr){};

// Pops the top Context off the stack and returns it.
Context Pop() noexcept
{
if (size_ <= 0)
{
return Context();
}
int index = size_ - 1;
size_--;
return base_[index];
}

// Returns the Context at the top of the stack.
Context Top() const noexcept
{
if (size_ <= 0)
{
return Context();
}
return base_[size_ - 1];
}

// Pushes the passed in context pointer to the top of the stack
// and resizes if necessary.
void Push(Context context) noexcept
{
size_++;
if (size_ > capacity_)
{
Resize(size_ * 2);
}
base_[size_ - 1] = context;
}

// Reallocates the storage array to the pass in new capacity size.
void Resize(int new_capacity) noexcept
{
int old_size = size_ - 1;
if (new_capacity == 0)
{
new_capacity = 2;
}
Context *temp = new Context[new_capacity];
if (base_ != nullptr)
{
std::copy(base_, base_ + old_size, temp);
delete[] base_;
}
base_ = temp;
}

~Stack() noexcept { delete[] base_; }

size_t size_;
size_t capacity_;
Context *base_;
};

static thread_local Stack stack_;
};
thread_local ThreadLocalContext::Stack ThreadLocalContext::stack_ = ThreadLocalContext::Stack();

// Registers the ThreadLocalContext as the context handler for the RuntimeContext
RuntimeContext *RuntimeContext::context_handler_ = new ThreadLocalContext();
} // namespace context
OPENTELEMETRY_END_NAMESPACE
Empty file.
83 changes: 83 additions & 0 deletions api/include/opentelemetry/metrics/async_instruments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include "instrument.h"
#include "observer_result.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{

template <class T>
class ValueObserver : virtual public AsynchronousInstrument<T>
{

public:
ValueObserver() = default;

ValueObserver(nostd::string_view name,
nostd::string_view description,
nostd::string_view unit,
bool enabled,
void (*callback)(ObserverResult<T>))
{}

/*
* Updates the instruments aggregator with the new value. The labels should
* contain the keys and values to be associated with this value.
*
* @param value is the numerical representation of the metric being captured
* @param labels the set of labels, as key-value pairs
*/
virtual void observe(T value, const trace::KeyValueIterable &labels) override = 0;

/**
* Captures data by activating the callback function associated with the
* instrument and storing its return value. Callbacks for asynchronous
* instruments are defined during construction.
*
* @param none
* @return none
*/
virtual void run() override = 0;
};

template <class T>
class SumObserver : virtual public AsynchronousInstrument<T>
{

public:
SumObserver() = default;

SumObserver(nostd::string_view name,
nostd::string_view description,
nostd::string_view unit,
bool enabled,
void (*callback)(ObserverResult<T>))
{}

virtual void observe(T value, const trace::KeyValueIterable &labels) override = 0;

virtual void run() override = 0;
};

template <class T>
class UpDownSumObserver : virtual public AsynchronousInstrument<T>
{

public:
UpDownSumObserver() = default;

UpDownSumObserver(nostd::string_view name,
nostd::string_view description,
nostd::string_view unit,
bool enabled,
void (*callback)(ObserverResult<T>))
{}

virtual void observe(T value, const trace::KeyValueIterable &labels) override = 0;

virtual void run() override = 0;
};

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit 591e76e

Please sign in to comment.