forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exponential_histogram_metrics.go
202 lines (185 loc) · 6.62 KB
/
exponential_histogram_metrics.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal"
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)
const (
// language=ClickHouse SQL
createExpHistogramTableSQL = `
CREATE TABLE IF NOT EXISTS %s_exponential_histogram (
ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
ResourceSchemaUrl String CODEC(ZSTD(1)),
ScopeName String CODEC(ZSTD(1)),
ScopeVersion String CODEC(ZSTD(1)),
ScopeAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
ScopeDroppedAttrCount UInt32 CODEC(ZSTD(1)),
ScopeSchemaUrl String CODEC(ZSTD(1)),
MetricName String CODEC(ZSTD(1)),
MetricDescription String CODEC(ZSTD(1)),
MetricUnit String CODEC(ZSTD(1)),
Attributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
StartTimeUnix DateTime64(9) CODEC(Delta, ZSTD(1)),
TimeUnix DateTime64(9) CODEC(Delta, ZSTD(1)),
Count Int64 CODEC(Delta, ZSTD(1)),
Sum Float64 CODEC(ZSTD(1)),
Scale Int32 CODEC(ZSTD(1)),
ZeroCount UInt64 CODEC(ZSTD(1)),
PositiveOffset Int32 CODEC(ZSTD(1)),
PositiveBucketCounts Array(UInt64) CODEC(ZSTD(1)),
NegativeOffset Int32 CODEC(ZSTD(1)),
NegativeBucketCounts Array(UInt64) CODEC(ZSTD(1)),
Exemplars Nested (
FilteredAttributes Map(LowCardinality(String), String),
TimeUnix DateTime64(9),
Value Float64,
SpanId String,
TraceId String
) CODEC(ZSTD(1)),
Flags UInt32 CODEC(ZSTD(1)),
Min Float64 CODEC(ZSTD(1)),
Max Float64 CODEC(ZSTD(1)),
INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_scope_attr_key mapKeys(ScopeAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_scope_attr_value mapValues(ScopeAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_key mapKeys(Attributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_attr_value mapValues(Attributes) TYPE bloom_filter(0.01) GRANULARITY 1
) ENGINE MergeTree()
%s
PARTITION BY toDate(TimeUnix)
ORDER BY (MetricName, Attributes, toUnixTimestamp64Nano(TimeUnix))
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
`
// language=ClickHouse SQL
insertExpHistogramTableSQL = `INSERT INTO %s_exponential_histogram (
ResourceAttributes,
ResourceSchemaUrl,
ScopeName,
ScopeVersion,
ScopeAttributes,
ScopeDroppedAttrCount,
ScopeSchemaUrl,
MetricName,
MetricDescription,
MetricUnit,
Attributes,
StartTimeUnix,
TimeUnix,
Count,
Sum,
Scale,
ZeroCount,
PositiveOffset,
PositiveBucketCounts,
NegativeOffset,
NegativeBucketCounts,
Exemplars.FilteredAttributes,
Exemplars.TimeUnix,
Exemplars.Value,
Exemplars.SpanId,
Exemplars.TraceId,
Flags,
Min,
Max) VALUES `
expHistogramValueCounts = 29
)
var expHistogramPlaceholders = newPlaceholder(expHistogramValueCounts)
type expHistogramModel struct {
metricName string
metricDescription string
metricUnit string
metadata *MetricsMetaData
expHistogram pmetric.ExponentialHistogram
}
type expHistogramMetrics struct {
expHistogramModels []*expHistogramModel
insertSQL string
count int
}
func (e *expHistogramMetrics) insert(ctx context.Context, db *sql.DB) error {
if e.count == 0 {
return nil
}
valueArgs := make([]any, e.count*expHistogramValueCounts)
var b strings.Builder
index := 0
for _, model := range e.expHistogramModels {
for i := 0; i < model.expHistogram.DataPoints().Len(); i++ {
dp := model.expHistogram.DataPoints().At(i)
b.WriteString(*expHistogramPlaceholders)
valueArgs[index] = model.metadata.ResAttr
valueArgs[index+1] = model.metadata.ResURL
valueArgs[index+2] = model.metadata.ScopeInstr.Name()
valueArgs[index+3] = model.metadata.ScopeInstr.Version()
valueArgs[index+4] = attributesToMap(model.metadata.ScopeInstr.Attributes())
valueArgs[index+5] = model.metadata.ScopeInstr.DroppedAttributesCount()
valueArgs[index+6] = model.metadata.ScopeURL
valueArgs[index+7] = model.metricName
valueArgs[index+8] = model.metricDescription
valueArgs[index+9] = model.metricUnit
valueArgs[index+10] = attributesToMap(dp.Attributes())
valueArgs[index+11] = dp.StartTimestamp().AsTime().UnixNano()
valueArgs[index+12] = dp.Timestamp().AsTime().UnixNano()
valueArgs[index+13] = dp.Count()
valueArgs[index+14] = dp.Sum()
valueArgs[index+15] = dp.Scale()
valueArgs[index+16] = dp.ZeroCount()
valueArgs[index+17] = dp.Positive().Offset()
valueArgs[index+18] = convertSliceToArraySet(dp.Positive().BucketCounts().AsRaw())
valueArgs[index+19] = dp.Negative().Offset()
valueArgs[index+20] = convertSliceToArraySet(dp.Negative().BucketCounts().AsRaw())
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
valueArgs[index+21] = attrs
valueArgs[index+22] = times
valueArgs[index+23] = values
valueArgs[index+24] = traceIDs
valueArgs[index+25] = spanIDs
valueArgs[index+26] = uint32(dp.Flags())
valueArgs[index+27] = dp.Min()
valueArgs[index+28] = dp.Max()
index += expHistogramValueCounts
}
}
start := time.Now()
err := doWithTx(ctx, db, func(tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, fmt.Sprintf("%s %s", e.insertSQL, strings.TrimSuffix(b.String(), ",")), valueArgs...)
return err
})
duration := time.Since(start)
if err != nil {
logger.Debug("insert exponential histogram metrics fail", zap.Duration("cost", duration))
return fmt.Errorf("insert exponential histogram metrics fail:%w", err)
}
// TODO latency metrics
logger.Debug("insert exponential histogram metrics", zap.Int("records", e.count),
zap.Duration("cost", duration))
return nil
}
func (e *expHistogramMetrics) Add(resAttr map[string]string, resURL string, scopeInstr pcommon.InstrumentationScope, scopeURL string, metrics any, name string, description string, unit string) error {
expHistogram, ok := metrics.(pmetric.ExponentialHistogram)
if !ok {
return fmt.Errorf("metrics param is not type of ExponentialHistogram")
}
e.count += expHistogram.DataPoints().Len()
e.expHistogramModels = append(e.expHistogramModels, &expHistogramModel{
metricName: name,
metricDescription: description,
metricUnit: unit,
metadata: &MetricsMetaData{
ResAttr: resAttr,
ResURL: resURL,
ScopeURL: scopeURL,
ScopeInstr: scopeInstr,
},
expHistogram: expHistogram,
})
return nil
}