Coverage Report

Created: 2024-11-20 12:56

/root/doris/be/src/util/telemetry/telemetry.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <opentelemetry/nostd/shared_ptr.h>
21
#include <opentelemetry/trace/noop.h>
22
#include <opentelemetry/trace/scope.h>
23
#include <opentelemetry/trace/span.h>
24
#include <opentelemetry/trace/span_context.h>
25
#include <opentelemetry/trace/tracer.h>
26
#include <opentelemetry/trace/tracer_provider.h>
27
28
#include <string>
29
30
// IWYU pragma: no_include <opentelemetry/common/threadlocal.h>
31
#include "common/compiler_util.h" // IWYU pragma: keep
32
#include "opentelemetry/trace/provider.h"
33
34
/// A trace represents the execution process of a single request in the system, span represents a
35
/// logical operation unit with start time and execution duration in the system, and multiple spans
36
/// form a trace.
37
namespace doris {
38
39
using OpentelemetryTracer = opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer>;
40
using OpentelemetrySpan = opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>;
41
using OpentelemetryScope = opentelemetry::trace::Scope;
42
43
/// Start a span with the specified tracer, name, and variable name, and create a Scope for this
44
/// span.
45
///
46
/// span represents the execution time of an operation unit, the time of its construction is the
47
/// start time, the time of its destructuring is the end time, you can also call the End method to
48
/// let span terminate, like "span->End();"
49
///
50
/// We can add Event and Attribute to span. Event will record the timestamp and event content,
51
/// and Attribute can record key-value pairs.
52
/// For example:
53
/// span->AddEvent("event content");
54
/// span->SetAttribute("key", "value");
55
///
56
/// Scope will add the span to a thread-local stack during construction, and remove the span from
57
/// the stack during destructuring. When starting a span, the top-of-stack span will be the parent
58
/// span by default, and the top-of-stack span can be obtained via the
59
/// opentelemetry::trace::Tracer::GetCurrentSpan() method.
60
#define START_AND_SCOPE_SPAN(tracer, span, name) \
61
0
    auto span = tracer->StartSpan(name);         \
62
0
    OpentelemetryScope scope {span};
63
64
namespace telemetry {
65
66
void init_tracer();
67
68
/// Return NoopTracer, the span generated by NoopTracer is NoopSpan, and the method bodies of
69
/// NoopTracer and NoopSpan are empty.
70
713
inline OpentelemetryTracer& get_noop_tracer() {
71
713
    static OpentelemetryTracer noop_tracer =
72
713
            opentelemetry::nostd::shared_ptr<opentelemetry::trace::NoopTracer>(
73
713
                    new opentelemetry::trace::NoopTracer);
74
713
    return noop_tracer;
75
713
}
76
77
0
inline OpentelemetryTracer get_tracer(const std::string& tracer_name) {
78
0
    return opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(tracer_name);
79
0
}
80
81
/// Returns true if the active pan stack is not empty.
82
16
inline bool is_current_span_valid() {
83
16
    return opentelemetry::trace::Tracer::GetCurrentSpan()->GetContext().IsValid();
84
16
}
85
86
} // namespace telemetry
87
} // namespace doris