|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef OPENCENSUS_CONTEXT_CONTEXT_H_ |
| 16 | +#define OPENCENSUS_CONTEXT_CONTEXT_H_ |
| 17 | + |
| 18 | +#include <functional> |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "opencensus/tags/tag_map.h" |
| 22 | +#include "opencensus/trace/span.h" |
| 23 | + |
| 24 | +namespace opencensus { |
| 25 | +namespace context { |
| 26 | + |
| 27 | +// Context holds information specific to an operation, such as a TagMap and |
| 28 | +// Span. Each thread has a currently active Context. Contexts are conceptually |
| 29 | +// immutable: the contents of a Context cannot be modified in-place. |
| 30 | +// |
| 31 | +// This is a draft implementation of Context, and we chose to depend on TagMap |
| 32 | +// and Span directly. In future, the implementation will change, so only rely |
| 33 | +// on the public API for manipulating Contexts. In future we may support |
| 34 | +// arbitrary keys and values. |
| 35 | +class Context { |
| 36 | + public: |
| 37 | + // Returns a const reference to the current (thread local) Context. |
| 38 | + static const Context& Current(); |
| 39 | + |
| 40 | + // Context is copiable and movable. |
| 41 | + Context(const Context&) = default; |
| 42 | + Context(Context&&) = default; |
| 43 | + Context& operator=(const Context&) = default; |
| 44 | + Context& operator=(Context&&) = default; |
| 45 | + |
| 46 | + // Returns an std::function wrapped to run with a copy of this Context. |
| 47 | + std::function<void()> Wrap(std::function<void()> fn) const; |
| 48 | + |
| 49 | + // Returns a human-readable string for debugging. Do not rely on its format or |
| 50 | + // try to parse it. Do not use the DebugString to retrieve Spans or Tags. |
| 51 | + std::string DebugString() const; |
| 52 | + |
| 53 | + private: |
| 54 | + // Creates a default Context. |
| 55 | + Context(); |
| 56 | + |
| 57 | + static Context* InternalMutableCurrent(); |
| 58 | + friend void swap(Context& a, Context& b); |
| 59 | + |
| 60 | + friend class WithContext; |
| 61 | + |
| 62 | + opencensus::tags::TagMap tags_; |
| 63 | + opencensus::trace::Span span_; |
| 64 | +}; |
| 65 | + |
| 66 | +} // namespace context |
| 67 | +} // namespace opencensus |
| 68 | + |
| 69 | +#endif // OPENCENSUS_CONTEXT_CONTEXT_H_ |
0 commit comments