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

Commit 4ae4299

Browse files
committed
receiver: implemented Prometheus receiver
This Prometheus receiver requires adding yaml definitions to the configuration file under "receivers" ```yaml receivers: prometheus: - job_name: 'ocjdbc' scrape_interval: 5s static_configs: - targets: ['localhost:9988'] - job_name: 'kafka_metrics' scrape_interval: 4s static_configs: - targets: ['localhost:8887'] buffer_period: 500ms buffer_count: 2 ``` Also added and end-to-end test to ensure that a full scrape by the receiver produces tangible metrics that are then consumed by the agent or whatever metrics sinks exist. Fixes #137
1 parent 4131c30 commit 4ae4299

11 files changed

Lines changed: 1022 additions & 117 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ receivers:
130130
jaeger:
131131
jaeger-thrift-tchannel-port: 14267
132132
jaeger-thrift-http-port: 14268
133+
134+
prometheus:
135+
config:
136+
scrape_configs:
137+
- job_name: 'caching_cluster'
138+
scrape_interval: 5s
139+
static_configs:
140+
- targets: ['localhost:8889']
133141
```
134142
135143
### <a name="config-exporters"></a>Exporters

cmd/ocagent/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/census-instrumentation/opencensus-service/receiver"
3939
"github.com/census-instrumentation/opencensus-service/receiver/jaeger"
4040
"github.com/census-instrumentation/opencensus-service/receiver/opencensus"
41+
promreceiver "github.com/census-instrumentation/opencensus-service/receiver/prometheus"
4142
"github.com/census-instrumentation/opencensus-service/receiver/zipkin"
4243
"github.com/census-instrumentation/opencensus-service/receiver/zipkin/scribe"
4344
)
@@ -135,6 +136,15 @@ func runOCAgent() {
135136
closeFns = append(closeFns, jaegerDoneFn)
136137
}
137138

139+
// If the Prometheus receiver is enabled, then run it.
140+
if agentConfig.PrometheusReceiverEnabled() {
141+
promDoneFn, err := runPrometheusReceiver(agentConfig.PrometheusConfiguration(), commonMetricsSink)
142+
if err != nil {
143+
log.Fatal(err)
144+
}
145+
closeFns = append(closeFns, promDoneFn)
146+
}
147+
138148
// Always cleanup finally
139149
defer func() {
140150
for _, closeFn := range closeFns {
@@ -268,3 +278,18 @@ func runZipkinScribeReceiver(config *config.ScribeReceiverConfig, sr receiver.Tr
268278
log.Printf("Running Zipkin Scribe receiver with %+v", *config)
269279
return doneFn, nil
270280
}
281+
282+
func runPrometheusReceiver(promConfig *promreceiver.Configuration, mr receiver.MetricsReceiverSink) (doneFn func() error, err error) {
283+
pmr, err := promreceiver.New(promConfig)
284+
if err != nil {
285+
return nil, err
286+
}
287+
if err := pmr.StartMetricsReception(context.Background(), mr); err != nil {
288+
return nil, err
289+
}
290+
doneFn = func() error {
291+
return pmr.StopMetricsReception(context.Background())
292+
}
293+
log.Print("Running Prometheus receiver")
294+
return doneFn, nil
295+
}

example/main.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"log"
2122
"math/rand"
2223
"time"
@@ -29,13 +30,17 @@ import (
2930
)
3031

3132
func main() {
32-
oce, err := ocagent.NewExporter(ocagent.WithInsecure())
33+
oce, err := ocagent.NewExporter(
34+
ocagent.WithInsecure(),
35+
ocagent.WithServiceName("example-go"))
3336
if err != nil {
3437
log.Fatalf("Failed to create ocagent-exporter: %v", err)
3538
}
3639
trace.RegisterExporter(oce)
3740
view.RegisterExporter(oce)
3841

42+
// Some configurations to get observability signals out.
43+
view.SetReportingPeriod(7 * time.Second)
3944
trace.ApplyConfig(trace.Config{
4045
DefaultSampler: trace.AlwaysSample(),
4146
})
@@ -45,12 +50,14 @@ func main() {
4550
keyMethod, _ := tag.NewKey("method")
4651

4752
mLatencyMs := stats.Float64("latency", "The latency in milliseconds", "ms")
53+
mLineLengths := stats.Int64("line_lengths", "The length of each line", "By")
54+
4855
views := []*view.View{
4956
{
5057
Name: "opdemo/latency",
51-
Description: "The various counts",
58+
Description: "The various latencies of the methods",
5259
Measure: mLatencyMs,
53-
Aggregation: view.Distribution(0, 10, 50, 100, 200, 400, 800, 1000, 1400, 2000, 5000, 10000),
60+
Aggregation: view.Distribution(0, 10, 50, 100, 200, 400, 800, 1000, 1400, 2000, 5000, 10000, 15000),
5461
TagKeys: []tag.Key{keyClient, keyMethod},
5562
},
5663
{
@@ -60,6 +67,20 @@ func main() {
6067
Aggregation: view.Count(),
6168
TagKeys: []tag.Key{keyClient, keyMethod},
6269
},
70+
{
71+
Name: "opdemo/line_lengths",
72+
Description: "The lengths of the various lines in",
73+
Measure: mLineLengths,
74+
Aggregation: view.Distribution(0, 10, 20, 50, 100, 150, 200, 500, 800),
75+
TagKeys: []tag.Key{keyClient, keyMethod},
76+
},
77+
{
78+
Name: "opdemo/line_counts",
79+
Description: "The counts of the lines in",
80+
Measure: mLineLengths,
81+
Aggregation: view.Count(),
82+
TagKeys: []tag.Key{keyClient, keyMethod},
83+
},
6384
}
6485

6586
if err := view.Register(views...); err != nil {
@@ -71,9 +92,31 @@ func main() {
7192
for {
7293
startTime := time.Now()
7394
_, span := trace.StartSpan(context.Background(), "Foo")
74-
time.Sleep(time.Duration(rng.Int63n(13000)) * time.Millisecond)
95+
var sleep int64
96+
switch modulus := time.Now().Unix() % 5; modulus {
97+
case 0:
98+
sleep = rng.Int63n(17001)
99+
case 1:
100+
sleep = rng.Int63n(8007)
101+
case 2:
102+
sleep = rng.Int63n(917)
103+
case 3:
104+
sleep = rng.Int63n(87)
105+
case 4:
106+
sleep = rng.Int63n(1173)
107+
}
108+
109+
time.Sleep(time.Duration(sleep) * time.Millisecond)
110+
75111
span.End()
76112
latencyMs := float64(time.Since(startTime)) / 1e6
113+
nr := int(rng.Int31n(7))
114+
for i := 0; i < nr; i++ {
115+
randLineLength := rng.Int63n(999)
116+
stats.Record(ctx, mLineLengths.M(randLineLength))
117+
fmt.Printf("#%d: LineLength: %dBy\n", i, randLineLength)
118+
}
77119
stats.Record(ctx, mLatencyMs.M(latencyMs))
120+
fmt.Printf("Latency: %.3fms\n", latencyMs)
78121
}
79122
}

go.mod

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ require (
44
cloud.google.com/go v0.32.0 // indirect
55
contrib.go.opencensus.io/exporter/ocagent v0.4.3
66
contrib.go.opencensus.io/exporter/stackdriver v0.9.1
7-
git.apache.org/thrift.git v0.0.0-20181101003639-92be4f312b88
7+
git.apache.org/thrift.git v0.0.0-20181101003639-92be4f312b88 // indirect
8+
github.com/Azure/azure-sdk-for-go v6.0.0-beta+incompatible // indirect
9+
github.com/Azure/go-autorest v11.4.0+incompatible // indirect
810
github.com/BurntSushi/toml v0.3.1 // indirect
911
github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895 // indirect
1012
github.com/DataDog/opencensus-go-exporter-datadog v0.0.0-20181026070331-e7c4bd17b329
@@ -13,38 +15,50 @@ require (
1315
github.com/aws/aws-sdk-go v1.15.68 // indirect
1416
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b // indirect
1517
github.com/census-instrumentation/opencensus-proto v0.1.0-0.20181214143942-ba49f56771b8
16-
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
17-
github.com/go-kit/kit v0.8.0 // indirect
18+
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
19+
github.com/go-logfmt/logfmt v0.4.0 // indirect
1820
github.com/gogo/googleapis v1.1.0 // indirect
19-
github.com/gogo/protobuf v1.1.1 // indirect
21+
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
2022
github.com/golang/protobuf v1.2.0
23+
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
24+
github.com/googleapis/gnostic v0.2.0 // indirect
25+
github.com/gophercloud/gophercloud v0.0.0-20190206021053-df38e1611dbe // indirect
2126
github.com/gorilla/context v1.1.1 // indirect
2227
github.com/gorilla/mux v1.6.2
23-
github.com/grpc-ecosystem/grpc-gateway v1.5.0
28+
github.com/gregjones/httpcache v0.0.0-20190203031600-7a902570cb17 // indirect
29+
github.com/grpc-ecosystem/grpc-gateway v1.6.3
30+
github.com/hashicorp/consul v1.4.2 // indirect
31+
github.com/hashicorp/go-cleanhttp v0.5.0 // indirect
32+
github.com/hashicorp/go-rootcerts v1.0.0 // indirect
33+
github.com/hashicorp/serf v0.8.2 // indirect
2434
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2535
github.com/jaegertracing/jaeger v1.8.2
2636
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
27-
github.com/kr/pretty v0.1.0 // indirect
37+
github.com/json-iterator/go v1.1.5 // indirect
2838
github.com/mitchellh/mapstructure v1.1.2 // indirect
39+
github.com/modern-go/reflect2 v1.0.1 // indirect
2940
github.com/omnition/scribe-go v0.0.0-20190131012523-9e3c68f31124
41+
github.com/onsi/ginkgo v1.7.0 // indirect
42+
github.com/onsi/gomega v1.4.3 // indirect
3043
github.com/opentracing/opentracing-go v1.0.2 // indirect
3144
github.com/openzipkin/zipkin-go v0.1.3
3245
github.com/orijtech/prometheus-go-metrics-exporter v0.0.2
46+
github.com/orijtech/promreceiver v0.0.3
47+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
3348
github.com/philhofer/fwd v1.0.0 // indirect
3449
github.com/pkg/errors v0.8.0
35-
github.com/pmezard/go-difflib v1.0.0 // indirect
3650
github.com/prashantv/protectmem v0.0.0-20171002184600-e20412882b3a // indirect
37-
github.com/prometheus/client_golang v0.8.0
38-
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273
51+
github.com/prometheus/client_golang v0.9.1
52+
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d
53+
github.com/prometheus/prometheus v0.0.0-20190131111325-62e591f928dd
3954
github.com/rs/cors v1.6.0
55+
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec // indirect
4056
github.com/soheilhy/cmux v0.1.4
4157
github.com/spf13/cast v1.2.0
4258
github.com/spf13/cobra v0.0.3
43-
github.com/spf13/pflag v1.0.3 // indirect
4459
github.com/spf13/viper v1.2.1
4560
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
4661
github.com/stretchr/objx v0.1.1 // indirect
47-
github.com/stretchr/testify v1.2.2 // indirect
4862
github.com/tinylib/msgp v1.0.2 // indirect
4963
github.com/uber-go/atomic v1.3.2 // indirect
5064
github.com/uber/jaeger-client-go v2.15.0+incompatible // indirect
@@ -55,16 +69,19 @@ require (
5569
go.uber.org/atomic v1.3.2 // indirect
5670
go.uber.org/multierr v1.1.0 // indirect
5771
go.uber.org/zap v1.9.1
58-
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 // indirect
59-
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc
72+
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 // indirect
6073
golang.org/x/oauth2 v0.0.0-20181102170140-232e45548389 // indirect
61-
golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc // indirect
62-
golang.org/x/text v0.3.0
74+
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
6375
google.golang.org/api v0.0.0-20181102150758-04bb50b6b83d
6476
google.golang.org/appengine v1.3.0 // indirect
6577
google.golang.org/genproto v0.0.0-20181101192439-c830210a61df // indirect
66-
google.golang.org/grpc v1.16.0
78+
google.golang.org/grpc v1.17.0
6779
gopkg.in/DataDog/dd-trace-go.v1 v1.4.0 // indirect
6880
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
81+
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
82+
gopkg.in/inf.v0 v0.9.1 // indirect
6983
gopkg.in/yaml.v2 v2.2.1
84+
k8s.io/apimachinery v0.0.0-20190207091153-095b9d203467 // indirect
85+
k8s.io/kube-openapi v0.0.0-20190205224424-fd29a9f2f429 // indirect
86+
sigs.k8s.io/structured-merge-diff v0.0.0-20190130003954-e5e029740eb8 // indirect
7087
)

0 commit comments

Comments
 (0)