opentelemetry;
65 usingTIdBuf = vector<uint8_t>;
67 staticconstexpr
intkTraceIdSize = trace::TraceId::kSize;
68 usingTTraceIdBuf = nostd::span<const uint8_t, kTraceIdSize>;
70 #define GET_BYTE(i64, b) ((i64 >> (56 - b*8)) & 0xff) 72TIdBuf trace_id_hash(
const string&s)
77h1 = c + (h1 << 5) + h1;
78h2 = c + (h2 << 6) + (h2 << 16) - h2;
80TIdBuf
buf(kTraceIdSize, 0);
81 for(
size_t i= 0;
i< kTraceIdSize / 2; ++
i) {
90 staticconstexpr
intkSpanIdSize = trace::SpanId::kSize;
91 usingTSpanIdBuf = nostd::span<const uint8_t, kSpanIdSize>;
93TIdBuf span_id_hash(
const string&s)
97h = c + (h << 6) + (h << 16) - h;
99TIdBuf
buf(kSpanIdSize, 0);
100 for(
size_t i= 0;
i< kSpanIdSize; ++
i) {
120 returns_GenerateSpanId();
125 returns_GenerateTraceId();
131 returnphid.substr(0, phid.find_first_of(
'.'));
137 size_tdot = phid.find_last_of(
'.');
138 if(dot != string::npos && phid.size() > dot) {
139 returnphid.substr(0, dot);
147TIdBuf span_id_buf = span_id_hash(phid);
148 returntrace::SpanId(TSpanIdBuf(span_id_buf.data(), span_id_buf.size()));
153 stringbase_phid = s_GetBaseHitId();
154TIdBuf trace_id_buf = trace_id_hash(base_phid);
155 returntrace::TraceId(TTraceIdBuf(trace_id_buf.data(), trace_id_buf.size()));
160 autoparent_phid = s_GetParentHitId();
161 if(parent_phid.empty())
returntrace::SpanId();
162TIdBuf span_id_buf = span_id_hash(parent_phid);
163 returntrace::SpanId(TSpanIdBuf(span_id_buf.data(), span_id_buf.size()));
168 using TTracer= nostd::shared_ptr<trace::Tracer>;
174 stringapp_name =
"ncbi-app";
175 stringapp_ver =
"0.0";
179app_name = guard->GetProgramDisplayName();
180app_ver = guard->GetVersion().Print();
184 returntrace::Provider::GetTracerProvider()->GetTracer(app_name, app_ver);
194 autoprocessor = sdk::trace::SimpleSpanProcessorFactory::Create(std::move(exporter));
195vector<unique_ptr<sdk::trace::SpanProcessor>> processors;
196processors.push_back(std::move(processor));
198opentelemetry::sdk::resource::Resource
200sdk::resource::Resource::Create({});
201unique_ptr<sdk::trace::Sampler> sampler(
newsdk::trace::AlwaysOnSampler());
205shared_ptr<trace::TracerProvider> provider =
206sdk::trace::TracerProviderFactory::Create(std::move(processors),
207resource, std::move(sampler), std::move(id_generator));
208trace::Provider::SetTracerProvider(provider);
216shared_ptr<trace::TracerProvider> none;
217trace::Provider::SetTracerProvider(none);
235x_InitOtlp(endpoint);
294x_InitOtlp(TParam_OtlpEndpoint::GetDefault());
300 ERR_POST(
"Unknown OTEL exporter");
310exporter::otlp::OtlpHttpExporterOptions http_opt;
312http_opt.content_type = exporter::otlp::HttpRequestContentType::kBinary;
317http_opt.url = !endpoint.empty() ? endpoint :
"http://localhost:4318/v1/traces";
318m_Tracer =
s_InitTracer(exporter::otlp::OtlpHttpExporterFactory::Create(http_opt));
321exporter::otlp::OtlpGrpcExporterOptions grpc_opt;
322grpc_opt.endpoint = !endpoint.empty() ? endpoint :
"http://localhost:4317/v1/traces";
323m_Tracer =
s_InitTracer(exporter::otlp::OtlpGrpcExporterFactory::Create(grpc_opt));
326 ERR_POST(
"Unknown OTLP protocol");
333m_Tracer =
s_InitTracer(exporter::trace::OStreamSpanExporterFactory::Create(ostr));
339TIdBuf
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);
349constexpr
size_tkVersion_Digits = 2;
350constexpr
size_tkTraceId_Digits = 32;
351constexpr
size_tkParentId_Digits = 16;
352constexpr
size_tkFlags_Digits = 2;
353constexpr
size_tkSeparator1_Pos = kVersion_Digits;
354constexpr
size_tkSeparator2_Pos = kSeparator1_Pos + 1 + kTraceId_Digits;
355constexpr
size_tkSeparator3_Pos = kSeparator2_Pos + 1 + kParentId_Digits;
356constexpr
size_tkTraceParent_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 &&
363traceparent[kSeparator1_Pos] ==
'-'&&
364traceparent[kSeparator2_Pos] ==
'-'&&
365traceparent[kSeparator3_Pos] ==
'-') {
368TIdBuf tid_buf =
string_to_id(traceparent.substr(kSeparator1_Pos + 1, kTraceId_Digits));
369TIdBuf pid_buf =
string_to_id(traceparent.substr(kSeparator2_Pos + 1, kParentId_Digits));
370TIdBuf flags_buf =
string_to_id(traceparent.substr(kSeparator3_Pos + 1, kFlags_Digits));
371trace_id = trace::TraceId(TTraceIdBuf(tid_buf.data(), tid_buf.size()));
372parent_id = trace::SpanId(TSpanIdBuf(pid_buf.data(), pid_buf.size()));
373 flags= (flags_buf[0] << 8) | flags_buf[1];
388 stringts =
ctx.GetSrcTraceState();
389 size_tncbi_pos = ts.find(
"ncbi");
390 if(ncbi_pos ==
NPOS)
returnts;
392vector<string> states;
395 for(
const auto&
state: states) {
401 if(
key==
"ncbi") {
403 if(!new_ts.empty())
val+=
",";
404new_ts =
key+
"="+
val+ new_ts;
408 if(!new_ts.empty()) new_ts +=
",";
418 if(!tracer)
return;
422 if(¤t_ctx != &
context) {
426 stringphid_cur = current_ctx.IsSetHitID() ? current_ctx.GetHitID() :
"<empty>";
427 stringphid_arg =
context.IsSetHitID() ?
context.GetHitID() :
"<empty>";
429 "Request context is not current, trace and span ids may be incorrect. Current context phid="<<
430phid_cur <<
"; tracer context phid="<< phid_arg);
433trace::TraceId context_tid;
434trace::SpanId context_pid;
438trace::SpanContext parent_ctx(context_tid, context_pid, trace::TraceFlags(0),
false);
439trace::StartSpanOptions options;
440options.parent = parent_ctx;
442 switch(
context.GetTracerSpanKind()) {
444options.kind = trace::SpanKind::kInternal;
447options.kind = trace::SpanKind::kServer;
450options.kind = trace::SpanKind::kClient;
453options.kind = m_SpanKind;
462span->SetStatus(trace_api::StatusCode::kOk);
470span->SetAttribute(
"ncbi.guid",
buf);
471span->SetAttribute(
"ncbi.pid", diag_context.
GetPID());
473span->SetAttribute(
"ncbi.host", diag_context.
GetHost());
474span->SetAttribute(
"ncbi.request_id",
context.GetRequestID());
475span->SetAttribute(
"ncbi.phid",
context.GetHitID());
476 strings =
context.GetClientIP();
477 if( !s.empty() ) span->SetAttribute(
"ncbi.client_ip", s);
479 if( !s.empty() ) span->SetAttribute(
"ncbi.session_id", s);
481 chartrace_id_buf[2*trace::TraceId::kSize + 1];
482trace_id_buf[2*trace::TraceId::kSize] = 0;
483span->GetContext().trace_id().ToLowerBase16(
484nostd::span<char, 2*trace::TraceId::kSize>(trace_id_buf, 2*trace::TraceId::kSize));
485 stringtrace_id(trace_id_buf);
487 charspan_id_buf[2*trace::SpanId::kSize + 1];
488span_id_buf[2*trace::SpanId::kSize] = 0;
489span->GetContext().span_id().ToLowerBase16(
490nostd::span<char, 2*trace::SpanId::kSize>(span_id_buf, 2*trace::SpanId::kSize));
491 stringspan_id(span_id_buf);
494.
Print(
"opentelemetry_trace_id", trace_id)
495.
Print(
"opentelemetry_span_id", span_id);
497shared_ptr<COpentelemetryTracerSpan> ctx_span = make_shared<COpentelemetryTracerSpan>(span);
500ctx_span->m_TraceParent =
"00-"+
string(trace_id) +
"-"+ span_id +
"-01";
502 context.SetTracerSpan(ctx_span);
508 autoctx_span =
context.GetTracerSpan();
509 if(!ctx_span)
return;
510 autoot_span = dynamic_pointer_cast<COpentelemetryTracerSpan>(ctx_span);
511 if(!ot_span)
return;
512ot_span->GetSpan()->End();
526 if(!m_Span)
return;
527m_Span->UpdateName(name);
533 if( !m_Span )
return;
539m_Span->SetAttribute(trace::SemanticConventions::kClientAddress,
value);
542m_Span->SetAttribute(trace::SemanticConventions::kClientPort,
value);
545m_Span->SetAttribute(trace::SemanticConventions::kServerAddress,
value);
548m_Span->SetAttribute(trace::SemanticConventions::kServerPort,
value);
551m_Span->SetAttribute(trace::SemanticConventions::kUrlFull,
value);
554m_Span->SetAttribute(trace::SemanticConventions::kHttpRequestMethod,
value);
557m_Span->SetAttribute(trace::SemanticConventions::kUrlScheme,
value);
560m_Span->SetAttribute(trace::SemanticConventions::kHttpScheme,
value);
563m_Span->SetAttribute(trace::SemanticConventions::kHttpResponseStatusCode,
value);
566m_Span->SetAttribute(trace::SemanticConventions::kErrorType,
value);
576 if( !m_Span )
return;
577m_Span->SetAttribute(attr,
value);
583 if( !m_Span )
return;
584 switch(header_type) {
586SetCustomAttribute(
"http.request.header."+ name,
value);
589SetCustomAttribute(
"http.response.header."+ name,
value);
599 if( !m_Span )
return;
600m_Span->SetStatus(status ==
eSuccess? trace::StatusCode::kOk : trace::StatusCode::kError);
612 returnm_TraceParent;
618 if( !m_Span )
return;
636m_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