File: context.h

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (44 lines) | stat: -rw-r--r-- 1,224 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/context/context.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/trace/default_span.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{

// Get Span from explicit context
inline nostd::shared_ptr<Span> GetSpan(const context::Context &context) noexcept
{
  context::ContextValue span = context.GetValue(kSpanKey);
  if (nostd::holds_alternative<nostd::shared_ptr<Span>>(span))
  {
    return nostd::get<nostd::shared_ptr<Span>>(span);
  }
  return nostd::shared_ptr<Span>(new DefaultSpan(SpanContext::GetInvalid()));
}

inline bool IsRootSpan(const context::Context &context) noexcept
{
  context::ContextValue is_root_span = context.GetValue(kIsRootSpanKey);
  if (nostd::holds_alternative<bool>(is_root_span))
  {
    return nostd::get<bool>(is_root_span);
  }
  return false;
}

// Set Span into explicit context
inline context::Context SetSpan(context::Context &context,
                                const nostd::shared_ptr<Span> &span) noexcept
{
  return context.SetValue(kSpanKey, span);
}

}  // namespace trace
OPENTELEMETRY_END_NAMESPACE