Skip to content

Commit

Permalink
[exporter/prometheusexporter] Use HTTPServerSettings to allow TLS (op…
Browse files Browse the repository at this point in the history
…en-telemetry#12921)

Prometheus exporter now uses confighttp.HTTPServerSettings to allow tls.
  • Loading branch information
neelayu authored Aug 12, 2022
1 parent b26a689 commit 5bdc8be
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 19 deletions.
6 changes: 5 additions & 1 deletion exporter/prometheusexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Exports data in the [Prometheus format](https://prometheus.io/docs/concepts/data

The following settings are required:

- `endpoint` (no default): the address on which metrics will be exposed by the Prometheus scrape handler.
- `endpoint` (no default): the address on which metrics will be exposed by the Prometheus scrape handler. For full list of `HTTPServerSettings` refer [here](https://github.com/open-telemetry/opentelemetry-collector/tree/main/config/confighttp).

The following settings can be optionally configured:

Expand All @@ -30,6 +30,10 @@ Example:
exporters:
prometheus:
endpoint: "1.2.3.4:1234"
tls:
ca_file: "/path/to/ca.pem"
cert_file: "/path/to/cert.pem"
key_file: "/path/to/key.pem"
namespace: test-space
const_labels:
label1: value1
Expand Down
7 changes: 3 additions & 4 deletions exporter/prometheusexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ import (

"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry"
)

// Config defines configuration for Prometheus exporter.
type Config struct {
config.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

// Endpoint is the address on which the Prometheus scrape handler will be run on.
Endpoint string `mapstructure:"endpoint"`
config.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
confighttp.HTTPServerSettings `mapstructure:",squash"`

// Namespace if set, exports metrics under the provided value.
Namespace string `mapstructure:"namespace"`
Expand Down
15 changes: 13 additions & 2 deletions exporter/prometheusexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/service/servicetest"
)

Expand All @@ -45,8 +47,17 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, e1,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")),
Endpoint: "1.2.3.4:1234",
Namespace: "test-space",
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "1.2.3.4:1234",
TLSSetting: &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CertFile: "certs/server.crt",
KeyFile: "certs/server.key",
CAFile: "certs/ca.crt",
},
},
},
Namespace: "test-space",
ConstLabels: map[string]string{
"label1": "value1",
"another label": "spaced value",
Expand Down
5 changes: 4 additions & 1 deletion exporter/prometheusexporter/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
Expand Down Expand Up @@ -69,7 +70,9 @@ func TestEndToEndSummarySupport(t *testing.T) {
exporterCfg := &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
Namespace: "test",
Endpoint: ":8787",
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":8787",
},
SendTimestamps: true,
MetricExpiration: 2 * time.Hour,
}
Expand Down
7 changes: 7 additions & 0 deletions exporter/prometheusexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ require (
github.com/envoyproxy/go-control-plane v0.10.3 // indirect
github.com/envoyproxy/protoc-gen-validate v0.6.7 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
Expand All @@ -64,6 +66,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down Expand Up @@ -91,6 +94,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/knadh/koanf v1.4.2 // indirect
github.com/kolo/xmlrpc v0.0.0-20201022064351-38db28db192b // indirect
github.com/linode/linodego v1.8.0 // indirect
Expand All @@ -107,17 +111,20 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vultr/govultr/v2 v2.17.2 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.34.0 // indirect
go.opentelemetry.io/otel v1.9.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/trace v1.9.0 // indirect
Expand Down
10 changes: 9 additions & 1 deletion exporter/prometheusexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions exporter/prometheusexporter/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package prometheusexporter // import "github.com/open-telemetry/opentelemetry-co
import (
"context"
"errors"
"net"
"net/http"
"strings"

Expand All @@ -28,12 +27,14 @@ import (
)

type prometheusExporter struct {
config Config
name string
endpoint string
shutdownFunc func() error
handler http.Handler
collector *collector
registry *prometheus.Registry
settings component.TelemetrySettings
}

var errBlankPrometheusAddress = errors.New("expecting a non-blank address to run the Prometheus metrics handler")
Expand All @@ -48,6 +49,7 @@ func newPrometheusExporter(config *Config, set component.ExporterCreateSettings)
registry := prometheus.NewRegistry()
_ = registry.Register(collector)
return &prometheusExporter{
config: *config,
name: config.ID().String(),
endpoint: addr,
collector: collector,
Expand All @@ -63,8 +65,8 @@ func newPrometheusExporter(config *Config, set component.ExporterCreateSettings)
}, nil
}

func (pe *prometheusExporter) Start(_ context.Context, _ component.Host) error {
ln, err := net.Listen("tcp", pe.endpoint)
func (pe *prometheusExporter) Start(_ context.Context, host component.Host) error {
ln, err := pe.config.ToListener()
if err != nil {
return err
}
Expand All @@ -73,7 +75,10 @@ func (pe *prometheusExporter) Start(_ context.Context, _ component.Host) error {

mux := http.NewServeMux()
mux.Handle("/metrics", pe.handler)
srv := &http.Server{Handler: mux}
srv, err := pe.config.ToServer(host, pe.settings, mux)
if err != nil {
return err
}
go func() {
_ = srv.Serve(ln)
}()
Expand Down
Loading

0 comments on commit 5bdc8be

Please sign in to comment.