forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauge_metrics.go
179 lines (163 loc) · 5.78 KB
/
gauge_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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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/pmetric"
"go.uber.org/zap"
)
const (
// language=ClickHouse SQL
createGaugeTableSQL = `
CREATE TABLE IF NOT EXISTS %s_gauge (
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)),
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
insertGaugeTableSQL = `INSERT INTO %s_gauge (
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) VALUES `
gaugeValueCounts = 20
)
var gaugePlaceholders = newPlaceholder(gaugeValueCounts)
type gaugeModel struct {
metricName string
metricDescription string
metricUnit string
metadata *MetricsMetaData
gauge pmetric.Gauge
}
type gaugeMetrics struct {
gaugeModels []*gaugeModel
insertSQL string
count int
}
func (g *gaugeMetrics) insert(ctx context.Context, db *sql.DB) error {
if g.count == 0 {
return nil
}
valueArgs := make([]any, g.count*gaugeValueCounts)
var b strings.Builder
index := 0
for _, model := range g.gaugeModels {
for i := 0; i < model.gauge.DataPoints().Len(); i++ {
dp := model.gauge.DataPoints().At(i)
b.WriteString(*gaugePlaceholders)
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
index += gaugeValueCounts
}
}
start := time.Now()
err := doWithTx(ctx, db, func(tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, fmt.Sprintf("%s %s", g.insertSQL, strings.TrimSuffix(b.String(), ",")), valueArgs...)
return err
})
duration := time.Since(start)
if err != nil {
logger.Debug("insert gauge metrics fail", zap.Duration("cost", duration))
return fmt.Errorf("insert gauge metrics fail:%w", err)
}
// TODO latency metrics
logger.Debug("insert gauge metrics", zap.Int("records", g.count),
zap.Duration("cost", duration))
return nil
}
func (g *gaugeMetrics) Add(metrics any, metaData *MetricsMetaData, name string, description string, unit string) error {
gauge, ok := metrics.(pmetric.Gauge)
if !ok {
return fmt.Errorf("metrics param is not type of Gauge")
}
g.count += gauge.DataPoints().Len()
g.gaugeModels = append(g.gaugeModels, &gaugeModel{
metricName: name,
metricDescription: description,
metricUnit: unit,
metadata: metaData,
gauge: gauge,
})
return nil
}