-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
receiver.go
122 lines (104 loc) · 3.65 KB
/
receiver.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
// 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://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 simpleprometheusreceiver
import (
"context"
"errors"
"fmt"
configutil "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver/prometheusreceiver"
"k8s.io/client-go/rest"
)
type prometheusReceiverWrapper struct {
params component.ReceiverCreateSettings
config *Config
consumer consumer.Metrics
prometheusRecever component.MetricsReceiver
}
// new returns a prometheusReceiverWrapper
func new(params component.ReceiverCreateSettings, cfg *Config, consumer consumer.Metrics) *prometheusReceiverWrapper {
return &prometheusReceiverWrapper{params: params, config: cfg, consumer: consumer}
}
// Start creates and starts the prometheus receiver.
func (prw *prometheusReceiverWrapper) Start(ctx context.Context, host component.Host) error {
pFactory := prometheusreceiver.NewFactory()
pConfig, err := getPrometheusConfig(prw.config)
if err != nil {
return fmt.Errorf("failed to create prometheus receiver config: %v", err)
}
pr, err := pFactory.CreateMetricsReceiver(ctx, prw.params, pConfig, prw.consumer)
if err != nil {
return fmt.Errorf("failed to create prometheus receiver: %v", err)
}
prw.prometheusRecever = pr
return prw.prometheusRecever.Start(ctx, host)
}
func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) {
var bearerToken string
if cfg.UseServiceAccount {
restConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
bearerToken = restConfig.BearerToken
if bearerToken == "" {
return nil, errors.New("bearer token was empty")
}
}
out := &prometheusreceiver.Config{}
httpConfig := configutil.HTTPClientConfig{}
scheme := "http"
if cfg.TLSEnabled {
scheme = "https"
httpConfig.TLSConfig = configutil.TLSConfig{
CAFile: cfg.TLSConfig.CAFile,
CertFile: cfg.TLSConfig.CertFile,
KeyFile: cfg.TLSConfig.KeyFile,
InsecureSkipVerify: cfg.TLSConfig.InsecureSkipVerify,
}
}
httpConfig.BearerToken = configutil.Secret(bearerToken)
scrapeConfig := &config.ScrapeConfig{
ScrapeInterval: model.Duration(cfg.CollectionInterval),
ScrapeTimeout: model.Duration(cfg.CollectionInterval),
JobName: fmt.Sprintf("%s/%s", typeStr, cfg.Endpoint),
HonorTimestamps: true,
Scheme: scheme,
MetricsPath: cfg.MetricsPath,
Params: cfg.Params,
ServiceDiscoveryConfigs: discovery.Configs{
&discovery.StaticConfig{
{
Targets: []model.LabelSet{
{model.AddressLabel: model.LabelValue(cfg.Endpoint)},
},
},
},
},
}
scrapeConfig.HTTPClientConfig = httpConfig
out.PrometheusConfig = &config.Config{ScrapeConfigs: []*config.ScrapeConfig{
scrapeConfig,
}}
return out, nil
}
// Shutdown stops the underlying Prometheus receiver.
func (prw *prometheusReceiverWrapper) Shutdown(ctx context.Context) error {
return prw.prometheusRecever.Shutdown(ctx)
}