This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exporter/metrics: add Prometheus metrics exporter
Added a Prometheus metrics exporter that allows us to export metrics. Its configuration is available under YAML field option ```yaml exporters prometheus: <key>: <value> ``` where <key> could be any of * address -- the address on which to run the Prometheus scrape endpoint /metrics on * const_labels -- labels that will be applied to each metric that is exported * namespace -- optionally if defined records metrics under a specific namespace, leave it blank to preserve the names of the proxied metrics (if that's the case) This change uses the Prometheus metrics exporter at github.com/orijtech/prometheus-go-metrics-exporter which will be handed over to the OpenCensus community whenever a repository is created under the "census-ecosystem" organization. Fixes #292 Depends on #294
- Loading branch information
Showing
5 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2019, OpenCensus 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 exporterparser | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/census-instrumentation/opencensus-service/data" | ||
"github.com/census-instrumentation/opencensus-service/exporter" | ||
"github.com/orijtech/prometheus-go-metrics-exporter" | ||
prometheus_golang "github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type prometheusConfig struct { | ||
// Namespace if set, exports metrics under the provided value. | ||
Namespace string `yaml:"namespace"` | ||
|
||
// ConstLabels are values that are applied for every exported metric. | ||
ConstLabels prometheus_golang.Labels `yaml:"const_labels"` | ||
|
||
// The address on which the Prometheus scrape handler will be run on. | ||
Address string `yaml:"address"` | ||
} | ||
|
||
var errBlankPrometheusAddress = errors.New("expecting a non-blank address to run the Prometheus metrics handler") | ||
|
||
// PrometheusExportersFromYAML parses the yaml bytes and returns exporter.MetricsExporters targeting | ||
// Prometheus according to the configuration settings. | ||
func PrometheusExportersFromYAML(config []byte) (tes []exporter.TraceExporter, mes []exporter.MetricsExporter, doneFns []func() error, err error) { | ||
var cfg struct { | ||
Exporters *struct { | ||
Prometheus *prometheusConfig `yaml:"prometheus"` | ||
} `yaml:"exporters"` | ||
} | ||
if err := yamlUnmarshal(config, &cfg); err != nil { | ||
return nil, nil, nil, err | ||
} | ||
if cfg.Exporters == nil { | ||
return nil, nil, nil, nil | ||
} | ||
|
||
pcfg := cfg.Exporters.Prometheus | ||
addr := strings.TrimSpace(pcfg.Address) | ||
if addr == "" { | ||
err = errBlankPrometheusAddress | ||
return | ||
} | ||
|
||
opts := prometheus.Options{ | ||
Namespace: pcfg.Namespace, | ||
ConstLabels: pcfg.ConstLabels, | ||
} | ||
pe, err := prometheus.New(opts) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
|
||
// The Prometheus metrics exporter has to run on the provided address | ||
// as a server that'll be scraped by Prometheus. | ||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", pe) | ||
|
||
srv := http.Server{ | ||
Handler: mux, | ||
Addr: pcfg.Address, | ||
} | ||
go func() { | ||
_ = srv.ListenAndServe() | ||
}() | ||
doneFns = append(doneFns, srv.Close) | ||
pexp := &prometheusExporter{exporter: pe} | ||
mes = append(mes, pexp) | ||
|
||
return | ||
} | ||
|
||
type prometheusExporter struct { | ||
exporter *prometheus.Exporter | ||
} | ||
|
||
var _ exporter.MetricsExporter = (*prometheusExporter)(nil) | ||
|
||
func (pe *prometheusExporter) ExportMetricsData(ctx context.Context, md data.MetricsData) error { | ||
for _, metric := range md.Metrics { | ||
_ = pe.exporter.ExportMetric(ctx, md.Node, md.Resource, metric) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters