Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit efd4d2d

Browse files
fivesheepsongy23
authored andcommitted
Reimplement prometheus receiver (#572)
1 parent d8a2872 commit efd4d2d

File tree

16 files changed

+3027
-163
lines changed

16 files changed

+3027
-163
lines changed

cmd/ocagent/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func runOCAgent() {
168168

169169
// If the Prometheus receiver is enabled, then run it.
170170
if agentConfig.PrometheusReceiverEnabled() {
171-
promDoneFn, err := runPrometheusReceiver(viperCfg, commonMetricsSink, asyncErrorChan)
171+
promDoneFn, err := runPrometheusReceiver(logger, viperCfg, commonMetricsSink, asyncErrorChan)
172172
if err != nil {
173173
log.Fatal(err)
174174
}
@@ -318,8 +318,8 @@ func runZipkinScribeReceiver(config *config.ScribeReceiverConfig, next consumer.
318318
return doneFn, nil
319319
}
320320

321-
func runPrometheusReceiver(v *viper.Viper, next consumer.MetricsConsumer, asyncErrorChan chan<- error) (doneFn func() error, err error) {
322-
pmr, err := prometheusreceiver.New(v.Sub("receivers.prometheus"), next)
321+
func runPrometheusReceiver(logger *zap.Logger, v *viper.Viper, next consumer.MetricsConsumer, asyncErrorChan chan<- error) (doneFn func() error, err error) {
322+
pmr, err := prometheusreceiver.New(logger, v.Sub("receivers.prometheus"), next)
323323
if err != nil {
324324
return nil, err
325325
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7
1515
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b // indirect
1616
github.com/census-instrumentation/opencensus-proto v0.2.0
17+
github.com/go-kit/kit v0.8.0
1718
github.com/gogo/googleapis v1.2.0 // indirect
1819
github.com/golang/protobuf v1.3.1
1920
github.com/google/go-cmp v0.3.0
@@ -26,11 +27,11 @@ require (
2627
github.com/opentracing/opentracing-go v1.1.0 // indirect
2728
github.com/openzipkin/zipkin-go v0.1.6
2829
github.com/orijtech/prometheus-go-metrics-exporter v0.0.3-0.20190313163149-b321c5297f60
29-
github.com/orijtech/promreceiver v0.0.6
3030
github.com/philhofer/fwd v1.0.0 // indirect
3131
github.com/pkg/errors v0.8.0
3232
github.com/prashantv/protectmem v0.0.0-20171002184600-e20412882b3a // indirect
3333
github.com/prometheus/client_golang v0.9.2
34+
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275
3435
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1
3536
github.com/prometheus/prometheus v0.0.0-20190131111325-62e591f928dd
3637
github.com/rs/cors v1.6.0

receiver/prometheusreceiver/README.md

Lines changed: 465 additions & 0 deletions
Large diffs are not rendered by default.

receiver/prometheusreceiver/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018, OpenCensus Authors
1+
// Copyright 2019, OpenCensus Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package internal
16+
17+
import (
18+
"context"
19+
"errors"
20+
"github.com/census-instrumentation/opencensus-service/data"
21+
"github.com/prometheus/prometheus/pkg/labels"
22+
"github.com/prometheus/prometheus/scrape"
23+
"go.uber.org/zap"
24+
"sync"
25+
)
26+
27+
// test helpers
28+
29+
var zapLogger *zap.Logger
30+
var testLogger *zap.SugaredLogger
31+
32+
func init() {
33+
zl, _ := zap.NewDevelopment()
34+
zapLogger = zl
35+
testLogger = zapLogger.Sugar()
36+
}
37+
38+
type mockMetadataCache struct {
39+
data map[string]scrape.MetricMetadata
40+
}
41+
42+
func (m *mockMetadataCache) Metadata(metricName string) (scrape.MetricMetadata, bool) {
43+
mm, ok := m.data[metricName]
44+
return mm, ok
45+
}
46+
47+
func (m *mockMetadataCache) SharedLabels() labels.Labels {
48+
return labels.FromStrings("__scheme__", "http")
49+
}
50+
51+
func newMockConsumer() *mockConsumer {
52+
return &mockConsumer{
53+
Metrics: make(chan *data.MetricsData, 1),
54+
}
55+
}
56+
57+
type mockConsumer struct {
58+
Metrics chan *data.MetricsData
59+
consumOnce sync.Once
60+
}
61+
62+
func (m *mockConsumer) ConsumeMetricsData(ctx context.Context, md data.MetricsData) error {
63+
m.consumOnce.Do(func() {
64+
m.Metrics <- &md
65+
})
66+
return nil
67+
}
68+
69+
type mockMetadataSvc struct {
70+
caches map[string]*mockMetadataCache
71+
}
72+
73+
func (mm *mockMetadataSvc) Get(job, instance string) (MetadataCache, error) {
74+
if mc, ok := mm.caches[job+"_"+instance]; ok {
75+
return mc, nil
76+
}
77+
78+
return nil, errors.New("cache not found")
79+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package internal
16+
17+
import "go.uber.org/zap"
18+
import gokitLog "github.com/go-kit/kit/log"
19+
20+
// NewZapToGokitLogAdapter create an adapter for zap.Logger to gokitLog.Logger
21+
func NewZapToGokitLogAdapter(logger *zap.Logger) gokitLog.Logger {
22+
// need to skip two levels in order to get the correct caller
23+
// one for this method, the other for gokitLog
24+
logger = logger.WithOptions(zap.AddCallerSkip(2))
25+
return &zapToGokitLogAdapter{l: logger.Sugar()}
26+
}
27+
28+
type zapToGokitLogAdapter struct {
29+
l *zap.SugaredLogger
30+
}
31+
32+
func (w *zapToGokitLogAdapter) Log(keyvals ...interface{}) error {
33+
if len(keyvals)%2 == 0 {
34+
// expecting key value pairs, the number of items need to be even
35+
w.l.Infow("", keyvals...)
36+
} else {
37+
// in case something goes wrong
38+
w.l.Info(keyvals...)
39+
}
40+
return nil
41+
}
42+
43+
var _ gokitLog.Logger = (*zapToGokitLogAdapter)(nil)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package internal
16+
17+
import (
18+
"errors"
19+
"github.com/prometheus/common/model"
20+
"github.com/prometheus/prometheus/pkg/labels"
21+
"github.com/prometheus/prometheus/scrape"
22+
)
23+
24+
// MetadataService is an adapter to scrapeManager and provide only the functionality which is needed
25+
type MetadataService interface {
26+
Get(job, instance string) (MetadataCache, error)
27+
}
28+
29+
// MetadataCache is an adapter to prometheus' scrape.Target and provide only the functionality which is needed
30+
type MetadataCache interface {
31+
Metadata(metricName string) (scrape.MetricMetadata, bool)
32+
SharedLabels() labels.Labels
33+
}
34+
35+
type mService struct {
36+
sm *scrape.Manager
37+
}
38+
39+
func (t *mService) Get(job, instance string) (MetadataCache, error) {
40+
targetGroup, ok := t.sm.TargetsAll()[job]
41+
if !ok {
42+
return nil, errors.New("unable to find a target group with job=" + job)
43+
}
44+
45+
// from the same targetGroup, instance is not going to be duplicated
46+
for _, target := range targetGroup {
47+
if target.DiscoveredLabels().Get(model.AddressLabel) == instance {
48+
return &mCache{target}, nil
49+
}
50+
}
51+
52+
return nil, errors.New("unable to find a target with job=" + job + ", and instance=" + instance)
53+
}
54+
55+
// adapter to get metadata from scrape.Target
56+
type mCache struct {
57+
t *scrape.Target
58+
}
59+
60+
func (m *mCache) Metadata(metricName string) (scrape.MetricMetadata, bool) {
61+
return m.t.Metadata(metricName)
62+
}
63+
64+
func (m *mCache) SharedLabels() labels.Labels {
65+
return m.t.DiscoveredLabels()
66+
}

0 commit comments

Comments
 (0)