forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_metrics.go
180 lines (164 loc) · 5.6 KB
/
sum_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
// 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
createSumTableSQL = `
CREATE TABLE IF NOT EXISTS %s_sum (
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)),
Value Float64 CODEC(ZSTD(1)),
Flags UInt32 CODEC(ZSTD(1)),
Exemplars Nested (
FilteredAttributes Map(LowCardinality(String), String),
TimeUnix DateTime64(9),
Value Float64,
SpanId String,
TraceId String
) CODEC(ZSTD(1)),
AggTemp Int32 CODEC(ZSTD(1)),
IsMonotonic Boolean CODEC(Delta, 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
insertSumTableSQL = `INSERT INTO %s_sum (
ResourceAttributes,
ResourceSchemaUrl,
ScopeName,
ScopeVersion,
ScopeAttributes,
ScopeDroppedAttrCount,
ScopeSchemaUrl,
MetricName,
MetricDescription,
MetricUnit,
Attributes,
StartTimeUnix,
TimeUnix,
Value,
Flags,
Exemplars.FilteredAttributes,
Exemplars.TimeUnix,
Exemplars.Value,
Exemplars.SpanId,
Exemplars.TraceId,
AggTemp,
IsMonotonic) VALUES `
sumValueCounts = 22
)
var sumPlaceholders = newPlaceholder(sumValueCounts)
type sumModel struct {
metricName string
metricDescription string
metricUnit string
metadata *MetricsMetaData
sum pmetric.Sum
}
type sumMetrics struct {
sumModel []*sumModel
insertSQL string
count int
}
func (s *sumMetrics) insert(ctx context.Context, db *sql.DB) error {
if s.count == 0 {
return nil
}
valueArgs := make([]any, s.count*sumValueCounts)
var b strings.Builder
index := 0
for _, model := range s.sumModel {
for i := 0; i < model.sum.DataPoints().Len(); i++ {
dp := model.sum.DataPoints().At(i)
b.WriteString(*sumPlaceholders)
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] = getValue(dp.IntValue(), dp.DoubleValue(), dp.ValueType())
valueArgs[index+14] = uint32(dp.Flags())
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
valueArgs[index+15] = attrs
valueArgs[index+16] = times
valueArgs[index+17] = values
valueArgs[index+18] = traceIDs
valueArgs[index+19] = spanIDs
valueArgs[index+20] = int32(model.sum.AggregationTemporality())
valueArgs[index+21] = model.sum.IsMonotonic()
index += sumValueCounts
}
}
start := time.Now()
err := doWithTx(ctx, db, func(tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, fmt.Sprintf("%s %s", s.insertSQL, strings.TrimSuffix(b.String(), ",")), valueArgs...)
return err
})
duration := time.Since(start)
if err != nil {
logger.Debug("insert sum metrics fail", zap.Duration("cost", duration))
return fmt.Errorf("insert sum metrics fail:%w", err)
}
// TODO latency metrics
logger.Debug("insert sum metrics", zap.Int("records", s.count),
zap.Duration("cost", duration))
return nil
}
func (s *sumMetrics) Add(resAttr map[string]string, resURL string, scopeInstr pcommon.InstrumentationScope, scopeURL string, metrics any, name string, description string, unit string) error {
sum, ok := metrics.(pmetric.Sum)
if !ok {
return fmt.Errorf("metrics param is not type of Sum")
}
s.count += sum.DataPoints().Len()
s.sumModel = append(s.sumModel, &sumModel{
metricName: name,
metricDescription: description,
metricUnit: unit,
metadata: &MetricsMetaData{
ResAttr: resAttr,
ResURL: resURL,
ScopeURL: scopeURL,
ScopeInstr: scopeInstr,
},
sum: sum,
})
return nil
}