-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathgauge_metrics.go
175 lines (164 loc) · 5.11 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
// 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"
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)
const (
// language=ClickHouse SQL
createGaugeTableSQL = `
CREATE TABLE IF NOT EXISTS %s %s (
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)),
ServiceName LowCardinality(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 = %s
%s
PARTITION BY toDate(TimeUnix)
ORDER BY (ServiceName, MetricName, Attributes, toUnixTimestamp64Nano(TimeUnix))
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
`
// language=ClickHouse SQL
insertGaugeTableSQL = `INSERT INTO %s (
ResourceAttributes,
ResourceSchemaUrl,
ScopeName,
ScopeVersion,
ScopeAttributes,
ScopeDroppedAttrCount,
ScopeSchemaUrl,
ServiceName,
MetricName,
MetricDescription,
MetricUnit,
Attributes,
StartTimeUnix,
TimeUnix,
Value,
Flags,
Exemplars.FilteredAttributes,
Exemplars.TimeUnix,
Exemplars.Value,
Exemplars.SpanId,
Exemplars.TraceId) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
)
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
}
start := time.Now()
err := doWithTx(ctx, db, func(tx *sql.Tx) error {
statement, err := tx.PrepareContext(ctx, g.insertSQL)
if err != nil {
return err
}
defer func() {
_ = statement.Close()
}()
for _, model := range g.gaugeModels {
resAttr := AttributesToMap(model.metadata.ResAttr)
scopeAttr := AttributesToMap(model.metadata.ScopeInstr.Attributes())
serviceName := GetServiceName(model.metadata.ResAttr)
for i := 0; i < model.gauge.DataPoints().Len(); i++ {
dp := model.gauge.DataPoints().At(i)
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
_, err = statement.ExecContext(ctx,
resAttr,
model.metadata.ResURL,
model.metadata.ScopeInstr.Name(),
model.metadata.ScopeInstr.Version(),
scopeAttr,
model.metadata.ScopeInstr.DroppedAttributesCount(),
model.metadata.ScopeURL,
serviceName,
model.metricName,
model.metricDescription,
model.metricUnit,
AttributesToMap(dp.Attributes()),
dp.StartTimestamp().AsTime(),
dp.Timestamp().AsTime(),
getValue(dp.IntValue(), dp.DoubleValue(), dp.ValueType()),
uint32(dp.Flags()),
attrs,
times,
values,
spanIDs,
traceIDs,
)
if err != nil {
return fmt.Errorf("ExecContext:%w", err)
}
}
}
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)
}
return nil
}
func (g *gaugeMetrics) Add(resAttr pcommon.Map, resURL string, scopeInstr pcommon.InstrumentationScope, scopeURL string, metrics any, 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: &MetricsMetaData{
ResAttr: resAttr,
ResURL: resURL,
ScopeURL: scopeURL,
ScopeInstr: scopeInstr,
},
gauge: gauge,
})
return nil
}