Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #include <hpactor/metrics/metrics_actor.hpp>
16 : : #include <hpactor/common.pb.h>
17 : : #include <cstdio>
18 : :
19 : : namespace hpactor::metrics {
20 : :
21 : 0 : MetricsActor::MetricsActor(ActorSystem& system,
22 : 0 : std::shared_ptr<MpscRingBuffer<MetricEvent>> ring_buffer)
23 : : : EventBasedActor(nullptr, system)
24 : 0 : , ring_buffer_(std::move(ring_buffer))
25 : 0 : , aggregator_(registry_, system) {}
26 : :
27 : 0 : void MetricsActor::register_handlers() {
28 : 0 : on_request<MetricsRequest, MetricsResponse>(
29 : 0 : [this](const MetricsRequest& /*req*/) -> MetricsResponse {
30 : 0 : aggregator_.begin_drain();
31 : 0 : ring_buffer_->drain([this](const MetricEvent& e) {
32 : 0 : aggregator_.on_event(e);
33 : 0 : return true;
34 : : });
35 : 0 : aggregator_.end_drain();
36 : :
37 : 0 : events_lost_ += ring_buffer_->events_lost();
38 : :
39 : 0 : auto snapshot = registry_.snapshot();
40 : 0 : std::string body = formatter_.format(snapshot);
41 : :
42 : : char buf[256];
43 : 0 : int n = snprintf(buf, sizeof(buf),
44 : : "# HELP hpactor_metrics_events_lost_total Events lost due to ring buffer overflow.\n"
45 : : "# TYPE hpactor_metrics_events_lost_total counter\n"
46 : : "hpactor_metrics_events_lost_total %llu\n",
47 : 0 : static_cast<unsigned long long>(events_lost_));
48 : 0 : body.append(buf, static_cast<size_t>(n));
49 : :
50 : 0 : MetricsResponse resp;
51 : : resp.set_body(body);
52 : 0 : return resp;
53 : 0 : });
54 : 0 : }
55 : :
56 : : } // namespace hpactor::metrics
|