Skip to content

Commit a9aa4eb

Browse files
knyarjkohen
authored andcommitted
Export OpenCensus metrics to Stackdriver (#118)
1 parent aafa975 commit a9aa4eb

File tree

118 files changed

+18132
-845
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+18132
-845
lines changed

cmd/stackdriver-prometheus-sidecar/main.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"time"
3333

3434
md "cloud.google.com/go/compute/metadata"
35+
oc_stackdriver "contrib.go.opencensus.io/exporter/stackdriver"
3536
"github.com/Stackdriver/stackdriver-prometheus-sidecar/metadata"
3637
"github.com/Stackdriver/stackdriver-prometheus-sidecar/retrieval"
3738
"github.com/Stackdriver/stackdriver-prometheus-sidecar/stackdriver"
@@ -184,6 +185,7 @@ func main() {
184185
filters []string
185186
metricRenames map[string]string
186187
staticMetadata []scrape.MetricMetadata
188+
monitoringBackends []string
187189

188190
logLevel promlog.AllowedLevel
189191
}{}
@@ -228,6 +230,9 @@ func main() {
228230
a.Flag("prometheus.api-address", "Address to listen on for UI, API, and telemetry.").
229231
Default("http://127.0.0.1:9090/").URLVar(&cfg.prometheusURL)
230232

233+
a.Flag("monitoring.backend", "Monitoring backend(s) for internal metrics").Default("prometheus").
234+
EnumsVar(&cfg.monitoringBackends, "prometheus", "stackdriver")
235+
231236
a.Flag("web.listen-address", "Address to listen on for UI, API, and telemetry.").
232237
Default("0.0.0.0:9091").StringVar(&cfg.listenAddress)
233238

@@ -258,20 +263,38 @@ func main() {
258263
level.Info(logger).Log("host_details", Uname())
259264
level.Info(logger).Log("fd_limits", FdLimits())
260265

261-
promExporter, err := oc_prometheus.NewExporter(oc_prometheus.Options{
262-
Registry: prometheus.DefaultRegisterer.(*prometheus.Registry),
263-
})
264-
if err != nil {
265-
level.Error(logger).Log("msg", "Creating Prometheus exporter failed", "err", err)
266-
os.Exit(1)
267-
}
268-
view.RegisterExporter(promExporter)
269-
270266
httpClient := &http.Client{Transport: &ochttp.Transport{}}
271267

272268
if *projectId == "" {
273269
*projectId = getGCEProjectID()
274270
}
271+
272+
for _, backend := range cfg.monitoringBackends {
273+
switch backend {
274+
case "prometheus":
275+
promExporter, err := oc_prometheus.NewExporter(oc_prometheus.Options{
276+
Registry: prometheus.DefaultRegisterer.(*prometheus.Registry),
277+
})
278+
if err != nil {
279+
level.Error(logger).Log("msg", "Creating Prometheus exporter failed", "err", err)
280+
os.Exit(1)
281+
}
282+
view.RegisterExporter(promExporter)
283+
case "stackdriver":
284+
sd, err := oc_stackdriver.NewExporter(oc_stackdriver.Options{ProjectID: *projectId})
285+
if err != nil {
286+
level.Error(logger).Log("msg", "Creating Stackdriver exporter failed", "err", err)
287+
os.Exit(1)
288+
}
289+
defer sd.Flush()
290+
view.RegisterExporter(sd)
291+
view.SetReportingPeriod(60 * time.Second)
292+
default:
293+
level.Error(logger).Log("msg", "Unknown monitoring backend", "backend", backend)
294+
os.Exit(1)
295+
}
296+
}
297+
275298
var staticLabels = map[string]string{
276299
retrieval.ProjectIDLabel: *projectId,
277300
retrieval.KubernetesLocationLabel: cfg.kubernetesLabels.location,

vendor/cloud.google.com/go/compute/metadata/metadata.go

Lines changed: 20 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)