1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifdef ENABLE_METRICS_PREVIEW
7 # include "opentelemetry/_metrics/instrument.h"
8 # include "opentelemetry/sdk/_metrics/aggregator/aggregator.h"
9 # include "opentelemetry/version.h"
15 OPENTELEMETRY_BEGIN_NAMESPACE
20 const int MinValueIndex
= 0;
21 const int MaxValueIndex
= 1;
22 const int SumValueIndex
= 2;
23 const int CountValueIndex
= 3;
25 * This aggregator stores and maintains a vector of
26 * type T where the contents in the vector are made
27 * up of the minimum value recorded to this instrument,
28 * the maximum value, the sum of all values, and the
29 * count of all values.
31 * @tparam T the type of values stored in this aggregator.
34 class MinMaxSumCountAggregator
: public Aggregator
<T
>
37 explicit MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind kind
)
39 static_assert(std::is_arithmetic
<T
>::value
, "Not an arithmetic type");
41 this->values_
= std::vector
<T
>(4, 0); // {min, max, sum, count}
42 this->checkpoint_
= this->values_
;
43 this->agg_kind_
= AggregatorKind::MinMaxSumCount
;
46 ~MinMaxSumCountAggregator() = default;
48 MinMaxSumCountAggregator(const MinMaxSumCountAggregator
&cp
)
50 this->values_
= cp
.values_
;
51 this->checkpoint_
= cp
.checkpoint_
;
52 this->kind_
= cp
.kind_
;
53 this->agg_kind_
= cp
.agg_kind_
;
54 // use default initialized mutex as they cannot be copied
58 * Receives a captured value from the instrument and applies it to the current aggregator value.
60 * @param val, the raw value used in aggregation
62 void update(T val
) override
65 this->updated_
= true;
67 if (this->values_
[CountValueIndex
] == 0 || val
< this->values_
[MinValueIndex
]) // set min
68 this->values_
[MinValueIndex
] = val
;
69 if (this->values_
[CountValueIndex
] == 0 || val
> this->values_
[MaxValueIndex
]) // set max
70 this->values_
[MaxValueIndex
] = val
;
72 this->values_
[SumValueIndex
] += val
; // compute sum
73 this->values_
[CountValueIndex
]++; // increment count
79 * Checkpoints the current value. This function will overwrite the current checkpoint with the
83 void checkpoint() override
86 this->updated_
= false;
87 this->checkpoint_
= this->values_
;
89 this->values_
[MinValueIndex
] = 0;
90 this->values_
[MaxValueIndex
] = 0;
91 this->values_
[SumValueIndex
] = 0;
92 this->values_
[CountValueIndex
] = 0;
97 * Merges two MinMaxSumCount aggregators together
99 * @param other the aggregator to merge with this aggregator
101 void merge(const MinMaxSumCountAggregator
&other
)
103 if (this->kind_
== other
.kind_
)
106 // First merge values
108 if (this->values_
[CountValueIndex
] == 0 ||
109 other
.values_
[MinValueIndex
] < this->values_
[MinValueIndex
])
110 this->values_
[MinValueIndex
] = other
.values_
[MinValueIndex
];
112 if (this->values_
[CountValueIndex
] == 0 ||
113 other
.values_
[MaxValueIndex
] > this->values_
[MaxValueIndex
])
114 this->values_
[MaxValueIndex
] = other
.values_
[MaxValueIndex
];
116 this->values_
[SumValueIndex
] += other
.values_
[SumValueIndex
];
118 this->values_
[CountValueIndex
] += other
.values_
[CountValueIndex
];
120 // Now merge checkpoints
121 if (this->checkpoint_
[CountValueIndex
] == 0 ||
122 other
.checkpoint_
[MinValueIndex
] < this->checkpoint_
[MinValueIndex
])
123 this->checkpoint_
[MinValueIndex
] = other
.checkpoint_
[MinValueIndex
];
125 if (this->checkpoint_
[CountValueIndex
] == 0 ||
126 other
.checkpoint_
[MaxValueIndex
] > this->checkpoint_
[MaxValueIndex
])
127 this->checkpoint_
[MaxValueIndex
] = other
.checkpoint_
[MaxValueIndex
];
129 this->checkpoint_
[SumValueIndex
] += other
.checkpoint_
[SumValueIndex
];
131 this->checkpoint_
[CountValueIndex
] += other
.checkpoint_
[CountValueIndex
];
143 * Returns the checkpointed value
145 * @return the value of the checkpoint
147 std::vector
<T
> get_checkpoint() override
{ return this->checkpoint_
; }
150 * Returns the values currently held by the aggregator
152 * @return the values held by the aggregator
154 std::vector
<T
> get_values() override
{ return this->values_
; }
156 } // namespace metrics
158 OPENTELEMETRY_END_NAMESPACE