forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_runnable.go
117 lines (100 loc) · 3.44 KB
/
redis_runnable.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
// Copyright 2020, 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 redisreceiver
import (
"context"
"time"
"github.com/open-telemetry/opentelemetry-collector/consumer"
"github.com/open-telemetry/opentelemetry-collector/obsreport"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver/interval"
)
var _ interval.Runnable = (*redisRunnable)(nil)
// Runs intermittently, fetching info from Redis, creating metrics/datapoints,
// and feeding them to a metricsConsumer.
type redisRunnable struct {
ctx context.Context
metricsConsumer consumer.MetricsConsumerOld
redisSvc *redisSvc
redisMetrics []*redisMetric
logger *zap.Logger
timeBundle *timeBundle
serviceName string
}
func newRedisRunnable(
ctx context.Context,
client client,
serviceName string,
metricsConsumer consumer.MetricsConsumerOld,
logger *zap.Logger,
) *redisRunnable {
return &redisRunnable{
ctx: ctx,
serviceName: serviceName,
redisSvc: newRedisSvc(client),
metricsConsumer: metricsConsumer,
logger: logger,
}
}
// Builds a data structure of all of the keys, types, converters and such to
// later extract data from Redis.
func (r *redisRunnable) Setup() error {
r.redisMetrics = getDefaultRedisMetrics()
return nil
}
// Run is called periodically, querying Redis and building Metrics to send to
// the next consumer. First builds 'fixed' metrics (non-keyspace metrics)
// defined at startup time. Then builds 'keyspace' metrics if there are any
// keyspace lines returned by Redis. There should be one keyspace line per
// active Redis database, of which there can be 16.
func (r *redisRunnable) Run() error {
const dataFormat = "redis"
const transport = "http" // todo verify this
ctx := obsreport.StartMetricsReceiveOp(r.ctx, dataFormat, transport)
inf, err := r.redisSvc.info()
if err != nil {
obsreport.EndMetricsReceiveOp(ctx, dataFormat, 0, 0, err)
return nil
}
uptime, err := inf.getUptimeInSeconds()
if err != nil {
obsreport.EndMetricsReceiveOp(ctx, dataFormat, 0, 0, err)
return nil
}
if r.timeBundle == nil {
r.timeBundle = newTimeBundle(time.Now(), uptime)
} else {
r.timeBundle.update(time.Now(), uptime)
}
metrics, warnings := inf.buildFixedProtoMetrics(r.redisMetrics, r.timeBundle)
if warnings != nil {
r.logger.Warn(
"errors parsing redis string",
zap.Errors("parsing errors", warnings),
)
}
keyspaceMetrics, warnings := inf.buildKeyspaceProtoMetrics(r.timeBundle)
metrics = append(metrics, keyspaceMetrics...)
if warnings != nil {
r.logger.Warn(
"errors parsing keyspace string",
zap.Errors("parsing errors", warnings),
)
}
md := newMetricsData(metrics, r.serviceName)
err = r.metricsConsumer.ConsumeMetricsData(r.ctx, *md)
numTimeSeries, numPoints := obsreport.CountMetricPoints(*md)
obsreport.EndMetricsReceiveOp(ctx, dataFormat, numPoints, numTimeSeries, err)
return nil
}