-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scraper.go
95 lines (77 loc) · 3.51 KB
/
scraper.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver"
import (
"context"
"errors"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/scraper/scrapererror"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/model"
)
var errClientNotInit = errors.New("client not initialized")
// riakScraper handles scraping of Riak metrics
type riakScraper struct {
logger *zap.Logger
cfg *Config
settings component.TelemetrySettings
client client
mb *metadata.MetricsBuilder
}
// newScraper creates a new scraper
func newScraper(logger *zap.Logger, cfg *Config, settings receiver.Settings) *riakScraper {
return &riakScraper{
logger: logger,
cfg: cfg,
settings: settings.TelemetrySettings,
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
}
}
// start starts the scraper by creating a new HTTP Client on the scraper
func (r *riakScraper) start(ctx context.Context, host component.Host) (err error) {
r.client, err = newClient(ctx, r.cfg, host, r.settings, r.logger)
return
}
// scrape collects metrics from the Riak API
func (r *riakScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
// Validate we don't attempt to scrape without initializing the client
if r.client == nil {
return pmetric.NewMetrics(), errors.New("client not initialized")
}
// Get stats for processing
stats, err := r.client.GetStats(ctx)
if err != nil {
return pmetric.NewMetrics(), err
}
return r.collectStats(stats)
}
// collectStats collects metrics
func (r *riakScraper) collectStats(stat *model.Stats) (pmetric.Metrics, error) {
now := pcommon.NewTimestampFromTime(time.Now())
var errors scrapererror.ScrapeErrors
// scrape node.operation.count metric
r.mb.RecordRiakNodeOperationCountDataPoint(now, stat.NodeGets, metadata.AttributeRequestGet)
r.mb.RecordRiakNodeOperationCountDataPoint(now, stat.NodePuts, metadata.AttributeRequestPut)
// scrape node.operation.time.mean metric
r.mb.RecordRiakNodeOperationTimeMeanDataPoint(now, stat.NodeGetFsmTimeMean, metadata.AttributeRequestGet)
r.mb.RecordRiakNodeOperationTimeMeanDataPoint(now, stat.NodePutFsmTimeMean, metadata.AttributeRequestPut)
// scrape node.read_repair.count metric
r.mb.RecordRiakNodeReadRepairCountDataPoint(now, stat.ReadRepairs)
// scrape node.memory.limit metric
r.mb.RecordRiakMemoryLimitDataPoint(now, stat.MemAllocated)
// scrape vnode.operation.count metric
r.mb.RecordRiakVnodeOperationCountDataPoint(now, stat.VnodeGets, metadata.AttributeRequestGet)
r.mb.RecordRiakVnodeOperationCountDataPoint(now, stat.VnodePuts, metadata.AttributeRequestPut)
// scrape vnode.index.operation.count metric
r.mb.RecordRiakVnodeIndexOperationCountDataPoint(now, stat.VnodeIndexReads, metadata.AttributeOperationRead)
r.mb.RecordRiakVnodeIndexOperationCountDataPoint(now, stat.VnodeIndexWrites, metadata.AttributeOperationWrite)
r.mb.RecordRiakVnodeIndexOperationCountDataPoint(now, stat.VnodeIndexDeletes, metadata.AttributeOperationDelete)
rb := r.mb.NewResourceBuilder()
rb.SetRiakNodeName(stat.Node)
return r.mb.Emit(metadata.WithResource(rb.Emit())), errors.Combine()
}