A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/doxyhtml/opentelemetry__tracer_8cpp_source.html below:

NCBI C++ ToolKit: src/corelib/opentelemetry/opentelemetry_tracer.cpp Source File

40 #include <opentelemetry/sdk/trace/id_generator.h> 41 #include <opentelemetry/sdk/trace/exporter.h> 42 #include <opentelemetry/sdk/resource/resource.h> 43 #include <opentelemetry/sdk/trace/samplers/always_on.h> 44 #include <opentelemetry/sdk/trace/processor.h> 45 #include <opentelemetry/sdk/trace/simple_processor_factory.h> 46 #include <opentelemetry/sdk/trace/tracer_provider_factory.h> 47 #include <opentelemetry/trace/provider.h> 48 #include <opentelemetry/trace/semantic_conventions.h> 49 #include <opentelemetry/exporters/otlp/otlp_http_exporter_factory.h> 50 #include <opentelemetry/exporters/otlp/otlp_http_exporter_options.h> 51 #include <opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h> 52 #include <opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h> 53 #include <opentelemetry/exporters/ostream/span_exporter_factory.h> 56 #define NCBI_USE_ERRCODE_X Corelib_Diag 60 using namespace

opentelemetry;

65 using

TIdBuf = vector<uint8_t>;

67 static

constexpr

int

kTraceIdSize = trace::TraceId::kSize;

68 using

TTraceIdBuf = nostd::span<const uint8_t, kTraceIdSize>;

70 #define GET_BYTE(i64, b) ((i64 >> (56 - b*8)) & 0xff) 72

TIdBuf trace_id_hash(

const string

&s)

77

h1 = c + (h1 << 5) + h1;

78

h2 = c + (h2 << 6) + (h2 << 16) - h2;

80

TIdBuf

buf

(kTraceIdSize, 0);

81  for

(

size_t i

= 0;

i

< kTraceIdSize / 2; ++

i

) {

90 static

constexpr

int

kSpanIdSize = trace::SpanId::kSize;

91 using

TSpanIdBuf = nostd::span<const uint8_t, kSpanIdSize>;

93

TIdBuf span_id_hash(

const string

&s)

97

h = c + (h << 6) + (h << 16) - h;

99

TIdBuf

buf

(kSpanIdSize, 0);

100  for

(

size_t i

= 0;

i

< kSpanIdSize; ++

i

) {

120  return

s_GenerateSpanId();

125  return

s_GenerateTraceId();

131  return

phid.substr(0, phid.find_first_of(

'.'

));

137  size_t

dot = phid.find_last_of(

'.'

);

138  if

(dot != string::npos && phid.size() > dot) {

139  return

phid.substr(0, dot);

147

TIdBuf span_id_buf = span_id_hash(phid);

148  return

trace::SpanId(TSpanIdBuf(span_id_buf.data(), span_id_buf.size()));

153  string

base_phid = s_GetBaseHitId();

154

TIdBuf trace_id_buf = trace_id_hash(base_phid);

155  return

trace::TraceId(TTraceIdBuf(trace_id_buf.data(), trace_id_buf.size()));

160  auto

parent_phid = s_GetParentHitId();

161  if

(parent_phid.empty())

return

trace::SpanId();

162

TIdBuf span_id_buf = span_id_hash(parent_phid);

163  return

trace::SpanId(TSpanIdBuf(span_id_buf.data(), span_id_buf.size()));

168 using TTracer

= nostd::shared_ptr<trace::Tracer>;

174  string

app_name =

"ncbi-app"

;

175  string

app_ver =

"0.0"

;

179

app_name = guard->GetProgramDisplayName();

180

app_ver = guard->GetVersion().Print();

184  return

trace::Provider::GetTracerProvider()->GetTracer(app_name, app_ver);

194  auto

processor = sdk::trace::SimpleSpanProcessorFactory::Create(std::move(exporter));

195

vector<unique_ptr<sdk::trace::SpanProcessor>> processors;

196

processors.push_back(std::move(processor));

198

opentelemetry::sdk::resource::Resource

200

sdk::resource::Resource::Create({});

201

unique_ptr<sdk::trace::Sampler> sampler(

new

sdk::trace::AlwaysOnSampler());

205

shared_ptr<trace::TracerProvider> provider =

206

sdk::trace::TracerProviderFactory::Create(std::move(processors),

207

resource, std::move(sampler), std::move(id_generator));

208

trace::Provider::SetTracerProvider(provider);

216

shared_ptr<trace::TracerProvider> none;

217

trace::Provider::SetTracerProvider(none);

235

x_InitOtlp(endpoint);

294

x_InitOtlp(TParam_OtlpEndpoint::GetDefault());

300  ERR_POST

(

"Unknown OTEL exporter"

);

310

exporter::otlp::OtlpHttpExporterOptions http_opt;

312

http_opt.content_type = exporter::otlp::HttpRequestContentType::kBinary;

317

http_opt.url = !endpoint.empty() ? endpoint :

"http://localhost:4318/v1/traces"

;

318

m_Tracer =

s_InitTracer

(exporter::otlp::OtlpHttpExporterFactory::Create(http_opt));

321

exporter::otlp::OtlpGrpcExporterOptions grpc_opt;

322

grpc_opt.endpoint = !endpoint.empty() ? endpoint :

"http://localhost:4317/v1/traces"

;

323

m_Tracer =

s_InitTracer

(exporter::otlp::OtlpGrpcExporterFactory::Create(grpc_opt));

326  ERR_POST

(

"Unknown OTLP protocol"

);

333

m_Tracer =

s_InitTracer

(exporter::trace::OStreamSpanExporterFactory::Create(ostr));

339

TIdBuf

buf

(

id

.

size

() / 2, 0);

340  for

(

size_t b

= 0;

b

<

id

.size() / 2; ++

b

) {

341  buf

[

b

] = NStr::StringToNumeric<uint8_t>(

id

.substr(

b

*2, 2), 0, 16);

349

constexpr

size_t

kVersion_Digits = 2;

350

constexpr

size_t

kTraceId_Digits = 32;

351

constexpr

size_t

kParentId_Digits = 16;

352

constexpr

size_t

kFlags_Digits = 2;

353

constexpr

size_t

kSeparator1_Pos = kVersion_Digits;

354

constexpr

size_t

kSeparator2_Pos = kSeparator1_Pos + 1 + kTraceId_Digits;

355

constexpr

size_t

kSeparator3_Pos = kSeparator2_Pos + 1 + kParentId_Digits;

356

constexpr

size_t

kTraceParent_Size = kVersion_Digits + 1 + kTraceId_Digits + 1 + kParentId_Digits + 1 + kFlags_Digits;

357  _ASSERT

(kTraceIdSize*2 == kTraceId_Digits);

358  _ASSERT

(kSpanIdSize*2 == kParentId_Digits);

360  const string

& traceparent =

ctx

.GetSrcTraceParent();

362  if

(traceparent.size() >= kTraceParent_Size &&

363

traceparent[kSeparator1_Pos] ==

'-'

&&

364

traceparent[kSeparator2_Pos] ==

'-'

&&

365

traceparent[kSeparator3_Pos] ==

'-'

) {

368

TIdBuf tid_buf =

string_to_id

(traceparent.substr(kSeparator1_Pos + 1, kTraceId_Digits));

369

TIdBuf pid_buf =

string_to_id

(traceparent.substr(kSeparator2_Pos + 1, kParentId_Digits));

370

TIdBuf flags_buf =

string_to_id

(traceparent.substr(kSeparator3_Pos + 1, kFlags_Digits));

371

trace_id = trace::TraceId(TTraceIdBuf(tid_buf.data(), tid_buf.size()));

372

parent_id = trace::SpanId(TSpanIdBuf(pid_buf.data(), pid_buf.size()));

373  flags

= (flags_buf[0] << 8) | flags_buf[1];

388  string

ts =

ctx

.GetSrcTraceState();

389  size_t

ncbi_pos = ts.find(

"ncbi"

);

390  if

(ncbi_pos ==

NPOS

)

return

ts;

392

vector<string> states;

395  for

(

const auto

&

state

: states) {

401  if

(

key

==

"ncbi"

) {

403  if

(!new_ts.empty())

val

+=

","

;

404

new_ts =

key

+

"="

+

val

+ new_ts;

408  if

(!new_ts.empty()) new_ts +=

","

;

418  if

(!tracer)

return

;

422  if

(&current_ctx != &

context

) {

426  string

phid_cur = current_ctx.IsSetHitID() ? current_ctx.GetHitID() :

"<empty>"

;

427  string

phid_arg =

context

.IsSetHitID() ?

context

.GetHitID() :

"<empty>"

;

429  "Request context is not current, trace and span ids may be incorrect. Current context phid="

<<

430

phid_cur <<

"; tracer context phid="

<< phid_arg);

433

trace::TraceId context_tid;

434

trace::SpanId context_pid;

438

trace::SpanContext parent_ctx(context_tid, context_pid, trace::TraceFlags(0),

false

);

439

trace::StartSpanOptions options;

440

options.parent = parent_ctx;

442  switch

(

context

.GetTracerSpanKind()) {

444

options.kind = trace::SpanKind::kInternal;

447

options.kind = trace::SpanKind::kServer;

450

options.kind = trace::SpanKind::kClient;

453

options.kind = m_SpanKind;

462

span->SetStatus(trace_api::StatusCode::kOk);

470

span->SetAttribute(

"ncbi.guid"

,

buf

);

471

span->SetAttribute(

"ncbi.pid"

, diag_context.

GetPID

());

473

span->SetAttribute(

"ncbi.host"

, diag_context.

GetHost

());

474

span->SetAttribute(

"ncbi.request_id"

,

context

.GetRequestID());

475

span->SetAttribute(

"ncbi.phid"

,

context

.GetHitID());

476  string

s =

context

.GetClientIP();

477  if

( !s.empty() ) span->SetAttribute(

"ncbi.client_ip"

, s);

479  if

( !s.empty() ) span->SetAttribute(

"ncbi.session_id"

, s);

481  char

trace_id_buf[2*trace::TraceId::kSize + 1];

482

trace_id_buf[2*trace::TraceId::kSize] = 0;

483

span->GetContext().trace_id().ToLowerBase16(

484

nostd::span<char, 2*trace::TraceId::kSize>(trace_id_buf, 2*trace::TraceId::kSize));

485  string

trace_id(trace_id_buf);

487  char

span_id_buf[2*trace::SpanId::kSize + 1];

488

span_id_buf[2*trace::SpanId::kSize] = 0;

489

span->GetContext().span_id().ToLowerBase16(

490

nostd::span<char, 2*trace::SpanId::kSize>(span_id_buf, 2*trace::SpanId::kSize));

491  string

span_id(span_id_buf);

494

.

Print

(

"opentelemetry_trace_id"

, trace_id)

495

.

Print

(

"opentelemetry_span_id"

, span_id);

497

shared_ptr<COpentelemetryTracerSpan> ctx_span = make_shared<COpentelemetryTracerSpan>(span);

500

ctx_span->m_TraceParent =

"00-"

+

string

(trace_id) +

"-"

+ span_id +

"-01"

;

502  context

.SetTracerSpan(ctx_span);

508  auto

ctx_span =

context

.GetTracerSpan();

509  if

(!ctx_span)

return

;

510  auto

ot_span = dynamic_pointer_cast<COpentelemetryTracerSpan>(ctx_span);

511  if

(!ot_span)

return

;

512

ot_span->GetSpan()->End();

526  if

(!m_Span)

return

;

527

m_Span->UpdateName(name);

533  if

( !m_Span )

return

;

539

m_Span->SetAttribute(trace::SemanticConventions::kClientAddress,

value

);

542

m_Span->SetAttribute(trace::SemanticConventions::kClientPort,

value

);

545

m_Span->SetAttribute(trace::SemanticConventions::kServerAddress,

value

);

548

m_Span->SetAttribute(trace::SemanticConventions::kServerPort,

value

);

551

m_Span->SetAttribute(trace::SemanticConventions::kUrlFull,

value

);

554

m_Span->SetAttribute(trace::SemanticConventions::kHttpRequestMethod,

value

);

557

m_Span->SetAttribute(trace::SemanticConventions::kUrlScheme,

value

);

560

m_Span->SetAttribute(trace::SemanticConventions::kHttpScheme,

value

);

563

m_Span->SetAttribute(trace::SemanticConventions::kHttpResponseStatusCode,

value

);

566

m_Span->SetAttribute(trace::SemanticConventions::kErrorType,

value

);

576  if

( !m_Span )

return

;

577

m_Span->SetAttribute(attr,

value

);

583  if

( !m_Span )

return

;

584  switch

(header_type) {

586

SetCustomAttribute(

"http.request.header."

+ name,

value

);

589

SetCustomAttribute(

"http.response.header."

+ name,

value

);

599  if

( !m_Span )

return

;

600

m_Span->SetStatus(status ==

eSuccess

? trace::StatusCode::kOk : trace::StatusCode::kError);

612  return

m_TraceParent;

618  if

( !m_Span )

return

;

636

m_Span->AddEvent(

msg

, attr);

642  if

( m_Span ) m_Span->End();

sdk::trace::IdGenerator TParent

static string s_GetBaseHitId(void)

static string s_GetParentHitId(void)

static trace::TraceId s_GenerateTraceId() noexcept

static trace::SpanId s_GenerateParentSpanId() noexcept

trace::SpanId GenerateSpanId() noexcept override

static trace::SpanId s_GenerateSpanId() noexcept

trace::TraceId GenerateTraceId() noexcept override

static CNcbiApplicationGuard InstanceGuard(void)

Singleton method.

int m_ErrCode

Error code.

int m_ErrSubCode

Sub Error code.

CDiagContext_Extra & Print(const string &name, const string &value)

The method does not print the argument, but adds it to the string.

void SetAttribute(ESpanAttribute attr, const string &value) override

void EndSpan(void) override

const char * m_File

File name.

const char * m_Buffer

Not guaranteed to be '\0'-terminated!

const string & GetHost(void) const

Get host name.

string GetStringUID(TUID uid=0) const

Return string representation of UID.

CDiagContext & GetDiagContext(void)

Get diag context instance.

const string & GetTraceParent(void) const override

const char * m_Module

Module name.

void SetName(const string &name) override

void OnRequestStop(CRequestContext &context) override

CDiagContext_Extra Extra(void) const

Create a temporary CDiagContext_Extra object.

TCount m_ProcPost

Number of the post in the process.

void SetSpanStatus(ESpanStatus status) override

size_t m_Line

Line number in file.

TUID GetUID(void) const

Return (create if not created yet) unique diagnostic ID.

EDiagSev m_Severity

Severity level.

void SetHttpHeader(EHttpHeaderType header_type, const string &name, const string &value) override

const char * m_Prefix

Prefix string.

TDiagPostFlags m_Flags

Bitwise OR of "EDiagPostFlag".

static CRequestContext & GetRequestContext(void)

Shortcut to CDiagContextThreadData::GetThreadData().GetRequestContext()

~COpentelemetryTracer(void)

const string & GetTraceState(void) const override

COpentelemetryTracer(void)

Export using exporter settings from the environment/INI file.

void PostEvent(const SDiagMessage &message) override

static TPID GetPID(void)

Get cached PID (read real PID if not cached yet).

TCount m_ThrPost

Number of the post in the thread.

size_t m_BufferLen

Length of m_Buffer.

static const char * SeverityName(EDiagSev sev)

Get a common symbolic name for the severity levels.

void SetCustomAttribute(const string &attr, const string &value) override

const char * m_Class

Class name.

static void CleanupTracer(void)

Uninstall current tracer, if any.

~COpentelemetryTracerSpan(void)

void OnRequestStart(CRequestContext &context) override

string GetHitID(void) const

Get explicit hit id or the default one (from HTTP_NCBI_PHID etc).

const char * m_Function

Function name.

#define ERR_POST(message)

Error posting with file, line number information but without error codes.

const char * m_ErrText

Sometimes 'error' has no numeric code, but can be represented as text.

void x_InitOtlp(const string &endpoint)

void x_InitStream(ostream &ostr)

@ eDPF_AppLog

Post message to application log.

void Warning(CExceptionArgs_Base &args)

@ eParam_NoThread

Do not use per-thread values.

uint64_t Uint8

8-byte (64-bit) unsigned integer

#define END_NCBI_SCOPE

End previously defined NCBI scope.

#define BEGIN_NCBI_SCOPE

Define ncbi namespace.

static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)

Split a string using specified delimiters.

static bool SplitInTwo(const CTempString str, const CTempString delim, string &str1, string &str2, TSplitFlags flags=0)

Split a string into two pieces using the specified delimiters.

static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)

Convert numeric value to string.

static string TruncateSpaces(const string &str, ETrunc where=eTrunc_Both)

Truncate whitespace in a string.

static TThreadSystemID GetCurrentThreadSystemID(void)

Get the current thread ID.

const struct ncbi::grid::netcache::search::fields::SIZE size

const struct ncbi::grid::netcache::search::fields::KEY key

const GenericPointer< typename T::ValueType > T2 value

Defines the CNcbiApplication and CAppException classes for creating NCBI applications.

Multi-threading – mutexes; rw-locks; semaphore.

static string s_UpdateTraceState(CRequestContext &ctx, const string &traceparent)

static void s_ParseIds(CRequestContext &ctx, trace::TraceId &trace_id, trace::SpanId &parent_id, int &flags)

NCBI_PARAM_ENUM_DEF_EX(EOTEL_Exporter, OTEL, TRACES_EXPORTER, EOTEL_Exporter::e_Otlp, eParam_NoThread, OTEL_TRACES_EXPORTER)

DEFINE_STATIC_FAST_MUTEX(s_InitTracerMutex)

nostd::shared_ptr< trace::Tracer > TTracer

NCBI_PARAM_ENUM_DECL(EOTEL_Exporter, OTEL, TRACES_EXPORTER)

typedef NCBI_PARAM_TYPE(OTEL, TRACES_EXPORTER) TParam_Exporter

static string s_DefaultSpanNamePrefix

TIdBuf string_to_id(const string &id)

NCBI_PARAM_ENUM_ARRAY(EOTEL_Exporter, OTEL, TRACES_EXPORTER)

static TTracer s_GetTracer(void)

static TTracer s_InitTracer(unique_ptr< sdk::trace::SpanExporter > exporter)

NCBI_PARAM_DECL(string, OTEL, EXPORTER_OTLP_ENDPOINT)

NCBI_PARAM_DEF_EX(string, OTEL, EXPORTER_OTLP_ENDPOINT, "", eParam_NoThread, OTEL_EXPORTER_OTLP_ENDPOINT)

Defines COpentelemetryTracer class for NCBI C++ diagnostic API.

@ eSuccess

Successfully retrieved.

static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)

static CS_CONTEXT * context


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4