]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | #ifndef ENABLE_METRICS_PREVIEW | |
6 | ||
7 | # include "opentelemetry/sdk/metrics/metric_reader.h" | |
8 | # include "opentelemetry/version.h" | |
9 | ||
10 | # include <atomic> | |
11 | # include <chrono> | |
12 | # include <condition_variable> | |
13 | # include <thread> | |
14 | ||
15 | OPENTELEMETRY_BEGIN_NAMESPACE | |
16 | namespace sdk | |
17 | { | |
18 | namespace metrics | |
19 | { | |
20 | ||
21 | class MetricExporter; | |
22 | /** | |
23 | * Struct to hold PeriodicExortingMetricReader options. | |
24 | */ | |
25 | ||
26 | constexpr std::chrono::milliseconds kExportIntervalMillis = std::chrono::milliseconds(60000); | |
27 | constexpr std::chrono::milliseconds kExportTimeOutMillis = std::chrono::milliseconds(30000); | |
28 | struct PeriodicExportingMetricReaderOptions | |
29 | { | |
30 | ||
31 | /* The time interval between two consecutive exports. */ | |
32 | std::chrono::milliseconds export_interval_millis = | |
33 | std::chrono::milliseconds(kExportIntervalMillis); | |
34 | ||
35 | /* how long the export can run before it is cancelled. */ | |
36 | std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); | |
37 | }; | |
38 | ||
39 | class PeriodicExportingMetricReader : public MetricReader | |
40 | { | |
41 | ||
42 | public: | |
43 | PeriodicExportingMetricReader( | |
44 | std::unique_ptr<MetricExporter> exporter, | |
45 | const PeriodicExportingMetricReaderOptions &option, | |
46 | AggregationTemporality aggregation_temporality = AggregationTemporality::kCumulative); | |
47 | ||
48 | private: | |
49 | bool OnForceFlush(std::chrono::microseconds timeout) noexcept override; | |
50 | ||
51 | bool OnShutDown(std::chrono::microseconds timeout) noexcept override; | |
52 | ||
53 | void OnInitialized() noexcept override; | |
54 | ||
55 | std::unique_ptr<MetricExporter> exporter_; | |
56 | std::chrono::milliseconds export_interval_millis_; | |
57 | std::chrono::milliseconds export_timeout_millis_; | |
58 | ||
59 | void DoBackgroundWork(); | |
60 | ||
61 | /* The background worker thread */ | |
62 | std::thread worker_thread_; | |
63 | ||
64 | /* Synchronization primitives */ | |
65 | std::condition_variable cv_; | |
66 | std::mutex cv_m_; | |
67 | }; | |
68 | ||
69 | } // namespace metrics | |
70 | } // namespace sdk | |
71 | OPENTELEMETRY_END_NAMESPACE | |
72 | #endif |