-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
transaction.go
273 lines (245 loc) · 8.96 KB
/
transaction.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// 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://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 (
"context"
"errors"
"math"
"net"
"sync/atomic"
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/exemplar"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/obsreport"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/opencensus"
)
const (
portAttr = "port"
schemeAttr = "scheme"
jobAttr = "job"
instanceAttr = "instance"
transport = "http"
dataformat = "prometheus"
)
var errMetricNameNotFound = errors.New("metricName not found from labels")
var errTransactionAborted = errors.New("transaction aborted")
var errNoJobInstance = errors.New("job or instance cannot be found from labels")
var errNoStartTimeMetrics = errors.New("process_start_time_seconds metric is missing")
// A transaction is corresponding to an individual scrape operation or stale report.
// That said, whenever prometheus receiver scrapped a target metric endpoint a page of raw metrics is returned,
// a transaction, which acts as appender, is created to process this page of data, the scrapeLoop will call the Add or
// AddFast method to insert metrics data points, when finished either Commit, which means success, is called and data
// will be flush to the downstream consumer, or Rollback, which means discard all the data, is called and all data
// points are discarded.
type transaction struct {
id int64
ctx context.Context
isNew bool
sink consumer.Metrics
job string
instance string
jobsMap *JobsMap
useStartTimeMetric bool
startTimeMetricRegex string
ms *metadataService
node *commonpb.Node
resource *resourcepb.Resource
metricBuilder *metricBuilder
externalLabels labels.Labels
logger *zap.Logger
obsrecv *obsreport.Receiver
stalenessStore *stalenessStore
startTimeMs int64
}
func newTransaction(
ctx context.Context,
jobsMap *JobsMap,
useStartTimeMetric bool,
startTimeMetricRegex string,
receiverID config.ComponentID,
ms *metadataService,
sink consumer.Metrics,
externalLabels labels.Labels,
logger *zap.Logger, stalenessStore *stalenessStore) *transaction {
return &transaction{
id: atomic.AddInt64(&idSeq, 1),
ctx: ctx,
isNew: true,
sink: sink,
jobsMap: jobsMap,
useStartTimeMetric: useStartTimeMetric,
startTimeMetricRegex: startTimeMetricRegex,
ms: ms,
externalLabels: externalLabels,
logger: logger,
obsrecv: obsreport.NewReceiver(obsreport.ReceiverSettings{ReceiverID: receiverID, Transport: transport}),
stalenessStore: stalenessStore,
startTimeMs: -1,
}
}
// ensure *transaction has implemented the storage.Appender interface
var _ storage.Appender = (*transaction)(nil)
// Append always returns 0 to disable label caching.
func (tr *transaction) Append(ref uint64, ls labels.Labels, t int64, v float64) (uint64, error) {
if tr.startTimeMs < 0 {
tr.startTimeMs = t
}
// Important, must handle. prometheus will still try to feed the appender some data even if it failed to
// scrape the remote target, if the previous scrape was success and some data were cached internally
// in our case, we don't need these data, simply drop them shall be good enough. more details:
// https://github.com/prometheus/prometheus/blob/851131b0740be7291b98f295567a97f32fffc655/scrape/scrape.go#L933-L935
if math.IsNaN(v) {
return 0, nil
}
select {
case <-tr.ctx.Done():
return 0, errTransactionAborted
default:
}
if len(tr.externalLabels) > 0 {
// TODO(jbd): Improve the allocs.
ls = append(ls, tr.externalLabels...)
}
if tr.isNew {
if err := tr.initTransaction(ls); err != nil {
return 0, err
}
}
return 0, tr.metricBuilder.AddDataPoint(ls, t, v)
}
func (tr *transaction) AppendExemplar(ref uint64, l labels.Labels, e exemplar.Exemplar) (uint64, error) {
return 0, nil
}
// AddFast always returns error since caching is not supported by Add() function.
func (tr *transaction) AddFast(_ uint64, _ int64, _ float64) error {
return storage.ErrNotFound
}
func (tr *transaction) initTransaction(ls labels.Labels) error {
job, instance := ls.Get(model.JobLabel), ls.Get(model.InstanceLabel)
if job == "" || instance == "" {
return errNoJobInstance
}
// discover the binding target when this method is called for the first time during a transaction
mc, err := tr.ms.Get(job, instance)
if err != nil {
return err
}
if tr.jobsMap != nil {
tr.job = job
tr.instance = instance
}
tr.node, tr.resource = createNodeAndResource(job, instance, mc.SharedLabels().Get(model.SchemeLabel))
tr.metricBuilder = newMetricBuilder(mc, tr.useStartTimeMetric, tr.startTimeMetricRegex, tr.logger, tr.stalenessStore, tr.startTimeMs)
tr.isNew = false
return nil
}
// Commit submits metrics data to consumers.
func (tr *transaction) Commit() error {
if tr.isNew {
// In a situation like not able to connect to the remote server, scrapeloop will still commit even if it had
// never added any data points, that the transaction has not been initialized.
return nil
}
// Before building metrics, issue staleness markers for every stale metric.
staleLabels := tr.stalenessStore.emitStaleLabels()
for _, sEntry := range staleLabels {
tr.metricBuilder.AddDataPoint(sEntry.labels, sEntry.seenAtMs, stalenessSpecialValue)
}
tr.startTimeMs = -1
ctx := tr.obsrecv.StartMetricsOp(tr.ctx)
metrics, _, _, err := tr.metricBuilder.Build()
if err != nil {
// Only error by Build() is errNoDataToBuild, with numReceivedPoints set to zero.
tr.obsrecv.EndMetricsOp(ctx, dataformat, 0, err)
return err
}
if tr.useStartTimeMetric {
// startTime is mandatory in this case, but may be zero when the
// process_start_time_seconds metric is missing from the target endpoint.
if tr.metricBuilder.startTime == 0.0 {
// Since we are unable to adjust metrics properly, we will drop them
// and return an error.
err = errNoStartTimeMetrics
tr.obsrecv.EndMetricsOp(ctx, dataformat, 0, err)
return err
}
adjustStartTimestamp(tr.metricBuilder.startTime, metrics)
} else {
// AdjustMetrics - jobsMap has to be non-nil in this case.
// Note: metrics could be empty after adjustment, which needs to be checked before passing it on to ConsumeMetrics()
metrics, _ = NewMetricsAdjuster(tr.jobsMap.get(tr.job, tr.instance), tr.logger).AdjustMetrics(metrics)
}
numPoints := 0
if len(metrics) > 0 {
md := opencensus.OCToMetrics(tr.node, tr.resource, metrics)
numPoints = md.DataPointCount()
err = tr.sink.ConsumeMetrics(ctx, md)
}
tr.obsrecv.EndMetricsOp(ctx, dataformat, numPoints, err)
return err
}
func (tr *transaction) Rollback() error {
tr.startTimeMs = -1
return nil
}
func adjustStartTimestamp(startTime float64, metrics []*metricspb.Metric) {
startTimeTs := timestampFromFloat64(startTime)
for _, metric := range metrics {
switch metric.GetMetricDescriptor().GetType() {
case metricspb.MetricDescriptor_GAUGE_DOUBLE, metricspb.MetricDescriptor_GAUGE_DISTRIBUTION:
continue
default:
for _, ts := range metric.GetTimeseries() {
ts.StartTimestamp = startTimeTs
}
}
}
}
func timestampFromFloat64(ts float64) *timestamppb.Timestamp {
secs := int64(ts)
nanos := int64((ts - float64(secs)) * 1e9)
return ×tamppb.Timestamp{
Seconds: secs,
Nanos: int32(nanos),
}
}
func createNodeAndResource(job, instance, scheme string) (*commonpb.Node, *resourcepb.Resource) {
host, port, err := net.SplitHostPort(instance)
if err != nil {
host = instance
}
node := &commonpb.Node{
ServiceInfo: &commonpb.ServiceInfo{Name: job},
Identifier: &commonpb.ProcessIdentifier{
HostName: host,
},
}
resource := &resourcepb.Resource{
Labels: map[string]string{
jobAttr: job,
instanceAttr: instance,
portAttr: port,
schemeAttr: scheme,
},
}
return node, resource
}