This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathrunning_span_store_impl.cc
More file actions
106 lines (94 loc) · 3.1 KB
/
running_span_store_impl.cc
File metadata and controls
106 lines (94 loc) · 3.1 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2017, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "opencensus/trace/internal/running_span_store_impl.h"
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "absl/base/internal/endian.h"
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "opencensus/trace/exporter/span_data.h"
#include "opencensus/trace/internal/span_impl.h"
#include "opencensus/trace/span_context.h"
#include "opencensus/trace/span_id.h"
namespace opencensus {
namespace trace {
namespace exporter {
RunningSpanStoreImpl* RunningSpanStoreImpl::Get() {
static RunningSpanStoreImpl* global_running_span_store =
new RunningSpanStoreImpl;
return global_running_span_store;
}
void RunningSpanStoreImpl::AddSpan(const SpanId& id,
const std::shared_ptr<SpanImpl>& span) {
absl::MutexLock l(&mu_);
spans_.insert({id, span});
}
bool RunningSpanStoreImpl::RemoveSpan(const SpanId& id) {
absl::MutexLock l(&mu_);
auto iter = spans_.find(id);
if (iter == spans_.end()) {
return false; // Not tracked.
}
spans_.erase(iter);
return true;
}
bool RunningSpanStoreImpl::FindSpan(const SpanId& id,
std::shared_ptr<SpanImpl>* span) {
absl::MutexLock l(&mu_);
auto iter = spans_.find(id);
if (iter != spans_.end()) {
*span = iter->second;
return true;
} else {
return false;
}
}
RunningSpanStore::Summary RunningSpanStoreImpl::GetSummary() const {
RunningSpanStore::Summary summary;
absl::MutexLock l(&mu_);
for (const auto& addr_span : spans_) {
const std::string& name = addr_span.second->name_constref();
auto it = summary.per_span_name_summary.find(name);
if (it != summary.per_span_name_summary.end()) {
it->second.num_running_spans++;
} else {
summary.per_span_name_summary[name] = {1};
}
}
return summary;
}
std::vector<SpanData> RunningSpanStoreImpl::GetRunningSpans(
const RunningSpanStore::Filter& filter) const {
std::vector<SpanData> running_spans;
absl::MutexLock l(&mu_);
for (const auto& it : spans_) {
if (running_spans.size() >= filter.max_spans_to_return) break;
if (filter.span_name.empty() || (it.second->name() == filter.span_name)) {
running_spans.emplace_back(it.second->ToSpanData());
}
}
return running_spans;
}
void RunningSpanStoreImpl::ClearForTesting() {
absl::MutexLock l(&mu_);
spans_.clear();
}
} // namespace exporter
} // namespace trace
} // namespace opencensus