Couchbase C++ SDK 1.0.4 (rev. 5355b0f)
Loading...
Searching...
No Matches
otel_meter.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright 2021 Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include "opentelemetry/sdk/metrics/meter.h"
22
23#include <algorithm>
24#include <iostream>
25#include <thread>
26#include <utility>
27
30
31namespace nostd = opentelemetry::nostd;
32namespace metrics_api = opentelemetry::metrics;
33namespace metrics_sdk = opentelemetry::sdk::metrics;
34
35namespace couchbase::metrics
36{
37
39{
40public:
41 otel_sync_histogram(nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter)
42 : histogram_counter_(histogram_counter)
43 {
44 }
45
46 void record(std::uint64_t value,
47 const opentelemetry::common::KeyValueIterable& tags,
48 opentelemetry::context::Context& ctx)
49 {
50 histogram_counter_->Record(value, tags, ctx);
51 }
52
53private:
54 nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter_;
55 std::mutex mutex_;
56};
57
59{
60public:
62 nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter,
63 const std::map<std::string, std::string>& tags)
64 : histogram_counter_(histogram_counter)
65 , tags_(tags)
66 {
67 }
68 void record_value(std::int64_t value) override
69 {
70 value = std::max<int64_t>(value, 0);
71 auto uvalue = static_cast<std::uint64_t>(value);
72 histogram_counter_->Record(
73 uvalue, opentelemetry::common::KeyValueIterableView<decltype(tags_)>{ tags_ }, context_);
74 }
75
76 const std::map<std::string, std::string> tags()
77 {
78 return tags_;
79 }
80
81 nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter()
82 {
83 return histogram_counter_;
84 }
85
86private:
87 nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter_;
88 const std::map<std::string, std::string> tags_;
89 opentelemetry::context::Context context_{};
90 std::mutex mutex_;
91};
92
94{
95public:
96 explicit otel_meter(nostd::shared_ptr<metrics_api::Meter> meter)
97 : meter_(meter)
98 {
99 }
100
101 auto get_value_recorder(const std::string& name, const std::map<std::string, std::string>& tags)
102 -> std::shared_ptr<value_recorder> override
103 {
104 // first look up the histogram, in case we already have it...
105 std::scoped_lock<std::mutex> lock(mutex_);
106 auto it = recorders_.equal_range(name);
107 if (it.first == it.second) {
108 // this name isn't associated with any histogram, so make one and return it.
109 // Note we'd like to make one with more buckets than default, given the range of
110 // response times we'd like to display (queries vs kv for instance), but otel
111 // api doesn't seem to allow this.
112 return recorders_
113 .insert({ name,
114 std::make_shared<otel_value_recorder>(
115 meter_->CreateUInt64Histogram(name, "", "us"), tags) })
116 ->second;
117 }
118 // so it is already, lets see if we already have one with those tags, or need
119 // to make a new one (using the histogram we already have).
120 for (auto itr = it.first; itr != it.second; itr++) {
121 if (tags == itr->second->tags()) {
122 return itr->second;
123 }
124 }
125 // if you are here, we need to add one with these tags and the histogram associated with the
126 // name.
127 return recorders_
128 .insert(
129 { name,
130 std::make_shared<otel_value_recorder>(it.first->second->histogram_counter(), tags) })
131 ->second;
132 }
133
134private:
135 nostd::shared_ptr<metrics_api::Meter> meter_;
136 std::mutex mutex_;
137 std::multimap<std::string, std::shared_ptr<otel_value_recorder>> recorders_;
138};
139} // namespace couchbase::metrics
Definition meter.hxx:40
Definition otel_meter.hxx:94
auto get_value_recorder(const std::string &name, const std::map< std::string, std::string > &tags) -> std::shared_ptr< value_recorder > override
Definition otel_meter.hxx:101
otel_meter(nostd::shared_ptr< metrics_api::Meter > meter)
Definition otel_meter.hxx:96
Definition otel_meter.hxx:39
void record(std::uint64_t value, const opentelemetry::common::KeyValueIterable &tags, opentelemetry::context::Context &ctx)
Definition otel_meter.hxx:46
otel_sync_histogram(nostd::shared_ptr< metrics_api::Histogram< std::uint64_t > > histogram_counter)
Definition otel_meter.hxx:41
Definition otel_meter.hxx:59
void record_value(std::int64_t value) override
Definition otel_meter.hxx:68
otel_value_recorder(nostd::shared_ptr< metrics_api::Histogram< std::uint64_t > > histogram_counter, const std::map< std::string, std::string > &tags)
Definition otel_meter.hxx:61
nostd::shared_ptr< metrics_api::Histogram< std::uint64_t > > histogram_counter()
Definition otel_meter.hxx:81
const std::map< std::string, std::string > tags()
Definition otel_meter.hxx:76
Definition meter.hxx:27
Definition meter.hxx:25