From 5bdc8bed072f2d6602f83b425c11f65c78708167 Mon Sep 17 00:00:00 2001 From: Neelay Upadhyaya Date: Sat, 13 Aug 2022 01:04:18 +0530 Subject: [PATCH] [exporter/prometheusexporter] Use HTTPServerSettings to allow TLS (#12921) Prometheus exporter now uses confighttp.HTTPServerSettings to allow tls. --- exporter/prometheusexporter/README.md | 6 +- exporter/prometheusexporter/config.go | 7 +- exporter/prometheusexporter/config_test.go | 15 ++- .../prometheusexporter/end_to_end_test.go | 5 +- exporter/prometheusexporter/go.mod | 7 ++ exporter/prometheusexporter/go.sum | 10 +- exporter/prometheusexporter/prometheus.go | 13 +- .../prometheusexporter/prometheus_test.go | 111 +++++++++++++++++- .../prometheusexporter/testdata/certs/ca.crt | 20 ++++ .../testdata/certs/client.crt | 20 ++++ .../testdata/certs/client.key | 27 +++++ .../testdata/certs/server.crt | 20 ++++ .../testdata/certs/server.key | 27 +++++ .../prometheusexporter/testdata/config.yaml | 4 + unreleased/promexp.yaml | 11 ++ 15 files changed, 284 insertions(+), 19 deletions(-) create mode 100644 exporter/prometheusexporter/testdata/certs/ca.crt create mode 100644 exporter/prometheusexporter/testdata/certs/client.crt create mode 100644 exporter/prometheusexporter/testdata/certs/client.key create mode 100644 exporter/prometheusexporter/testdata/certs/server.crt create mode 100644 exporter/prometheusexporter/testdata/certs/server.key create mode 100755 unreleased/promexp.yaml diff --git a/exporter/prometheusexporter/README.md b/exporter/prometheusexporter/README.md index a304ce2be512..9396deca0a48 100644 --- a/exporter/prometheusexporter/README.md +++ b/exporter/prometheusexporter/README.md @@ -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: @@ -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 diff --git a/exporter/prometheusexporter/config.go b/exporter/prometheusexporter/config.go index 505bd18b53a6..a582cc364f77 100644 --- a/exporter/prometheusexporter/config.go +++ b/exporter/prometheusexporter/config.go @@ -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"` diff --git a/exporter/prometheusexporter/config_test.go b/exporter/prometheusexporter/config_test.go index 5313d987c851..4c2c4a048e6e 100644 --- a/exporter/prometheusexporter/config_test.go +++ b/exporter/prometheusexporter/config_test.go @@ -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" ) @@ -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", diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index a640c82aca09..24485a6123b1 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -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" @@ -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, } diff --git a/exporter/prometheusexporter/go.mod b/exporter/prometheusexporter/go.mod index c5a3c9883a1f..40192495ac79 100644 --- a/exporter/prometheusexporter/go.mod +++ b/exporter/prometheusexporter/go.mod @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/exporter/prometheusexporter/go.sum b/exporter/prometheusexporter/go.sum index 1890f978f42b..7b8531f9feda 100644 --- a/exporter/prometheusexporter/go.sum +++ b/exporter/prometheusexporter/go.sum @@ -197,6 +197,7 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= +github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -223,9 +224,11 @@ github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= @@ -287,6 +290,7 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= @@ -459,6 +463,7 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8 github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= +github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/knadh/koanf v1.4.2 h1:2itp+cdC6miId4pO4Jw7c/3eiYD26Z/Sz3ATJMwHxIs= github.com/knadh/koanf v1.4.2/go.mod h1:4NCo0q4pmU398vF9vq2jStF9MWQZ8JEDcDMHlDCr4h0= github.com/kolo/xmlrpc v0.0.0-20201022064351-38db28db192b h1:iNjcivnc6lhbvJA3LD622NPrUponluJrBWPIwGG/3Bg= @@ -543,8 +548,9 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk= -github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -616,6 +622,7 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U= +github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= @@ -683,6 +690,7 @@ go.opentelemetry.io/collector/pdata v0.58.0/go.mod h1:iMv7Pz+hRthi30rkYkwLVusxQ9 go.opentelemetry.io/collector/semconv v0.58.0 h1:wk9KXVnt8IRdNzD9mmdW3d1M/IJ3HyLp1Lz2ZY1fBCM= go.opentelemetry.io/collector/semconv v0.58.0/go.mod h1:aRkHuJ/OshtDFYluKEtnG5nkKTsy1HZuvZVHmakx+Vo= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.34.0 h1:9NkMW03wwEzPtP/KciZ4Ozu/Uz5ZA7kfqXJIObnrjGU= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.34.0/go.mod h1:548ZsYzmT4PL4zWKRd8q/N4z0Wxzn/ZxUE+lkEpwWQA= go.opentelemetry.io/otel v1.9.0 h1:8WZNQFIB2a71LnANS9JeyidJKKGOOremcUtb/OtHISw= go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo= go.opentelemetry.io/otel/exporters/prometheus v0.31.0 h1:jwtnOGBM8dIty5AVZ+9ZCzZexCea3aVKmUfZAQcHqxs= diff --git a/exporter/prometheusexporter/prometheus.go b/exporter/prometheusexporter/prometheus.go index 15887e49d54b..7b52d25f8443 100644 --- a/exporter/prometheusexporter/prometheus.go +++ b/exporter/prometheusexporter/prometheus.go @@ -17,7 +17,6 @@ package prometheusexporter // import "github.com/open-telemetry/opentelemetry-co import ( "context" "errors" - "net" "net/http" "strings" @@ -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") @@ -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, @@ -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 } @@ -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) }() diff --git a/exporter/prometheusexporter/prometheus_test.go b/exporter/prometheusexporter/prometheus_test.go index 13eda635077d..b1c4f26fab02 100644 --- a/exporter/prometheusexporter/prometheus_test.go +++ b/exporter/prometheusexporter/prometheus_test.go @@ -27,6 +27,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/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" conventions "go.opentelemetry.io/collector/semconv/v1.6.1" @@ -49,7 +51,9 @@ func TestPrometheusExporter(t *testing.T) { "foo0": "bar0", "code0": "one0", }, - Endpoint: ":8999", + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":8999", + }, SendTimestamps: false, MetricExpiration: 60 * time.Second, }, @@ -57,7 +61,9 @@ func TestPrometheusExporter(t *testing.T) { { config: &Config{ ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)), - Endpoint: ":88999", + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":88999", + }, }, wantStartErr: "listen tcp: address 88999: invalid port", }, @@ -99,6 +105,91 @@ func TestPrometheusExporter(t *testing.T) { } } +func TestPrometheusExporter_WithTLS(t *testing.T) { + cfg := &Config{ + ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)), + Namespace: "test", + ConstLabels: map[string]string{ + "foo2": "bar2", + "code2": "one2", + }, + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":7777", + TLSSetting: &configtls.TLSServerSetting{ + TLSSetting: configtls.TLSSetting{ + CertFile: "./testdata/certs/server.crt", + KeyFile: "./testdata/certs/server.key", + CAFile: "./testdata/certs/ca.crt", + }, + }, + }, + SendTimestamps: true, + MetricExpiration: 120 * time.Minute, + ResourceToTelemetrySettings: resourcetotelemetry.Settings{ + Enabled: true, + }, + } + factory := NewFactory() + set := componenttest.NewNopExporterCreateSettings() + exp, err := factory.CreateMetricsExporter(context.Background(), set, cfg) + require.NoError(t, err) + + tlscs := configtls.TLSClientSetting{ + TLSSetting: configtls.TLSSetting{ + CAFile: "./testdata/certs/ca.crt", + CertFile: "./testdata/certs/client.crt", + KeyFile: "./testdata/certs/client.key", + }, + ServerName: "localhost", + } + tls, err := tlscs.LoadTLSConfig() + assert.NoError(t, err) + httpClient := &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: tls, + }, + } + + t.Cleanup(func() { + require.NoError(t, exp.Shutdown(context.Background())) + // trigger a get so that the server cleans up our keepalive socket + _, err = httpClient.Get("https://localhost:7777/metrics") + require.NoError(t, err) + }) + + assert.NotNil(t, exp) + + require.NoError(t, exp.Start(context.Background(), componenttest.NewNopHost())) + + md := testdata.GenerateMetricsOneMetric() + assert.NotNil(t, md) + + assert.NoError(t, exp.ConsumeMetrics(context.Background(), md)) + + rsp, err := httpClient.Get("https://localhost:7777/metrics") + require.NoError(t, err, "Failed to perform a scrape") + + if g, w := rsp.StatusCode, 200; g != w { + t.Errorf("Mismatched HTTP response status code: Got: %d Want: %d", g, w) + } + + blob, _ := io.ReadAll(rsp.Body) + _ = rsp.Body.Close() + + want := []string{ + `# HELP test_counter_int`, + `# TYPE test_counter_int counter`, + `test_counter_int{code2="one2",foo2="bar2",label_1="label-value-1",resource_attr="resource-attr-val-1"} 123 1581452773000`, + `test_counter_int{code2="one2",foo2="bar2",label_2="label-value-2",resource_attr="resource-attr-val-1"} 456 1581452773000`, + } + + for _, w := range want { + if !strings.Contains(string(blob), w) { + t.Errorf("Missing %v from response:\n%v", w, string(blob)) + } + } +} + // See: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/4986 func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) { cfg := &Config{ @@ -108,7 +199,9 @@ func TestPrometheusExporter_endToEndMultipleTargets(t *testing.T) { "foo1": "bar1", "code1": "one1", }, - Endpoint: ":7777", + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":7777", + }, MetricExpiration: 120 * time.Minute, } @@ -189,7 +282,9 @@ func TestPrometheusExporter_endToEnd(t *testing.T) { "foo1": "bar1", "code1": "one1", }, - Endpoint: ":7777", + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":7777", + }, MetricExpiration: 120 * time.Minute, } @@ -264,7 +359,9 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) { "foo2": "bar2", "code2": "one2", }, - Endpoint: ":7777", + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":7777", + }, SendTimestamps: true, MetricExpiration: 120 * time.Minute, } @@ -340,7 +437,9 @@ func TestPrometheusExporter_endToEndWithResource(t *testing.T) { "foo2": "bar2", "code2": "one2", }, - Endpoint: ":7777", + HTTPServerSettings: confighttp.HTTPServerSettings{ + Endpoint: ":7777", + }, SendTimestamps: true, MetricExpiration: 120 * time.Minute, ResourceToTelemetrySettings: resourcetotelemetry.Settings{ diff --git a/exporter/prometheusexporter/testdata/certs/ca.crt b/exporter/prometheusexporter/testdata/certs/ca.crt new file mode 100644 index 000000000000..0d84b1dd0820 --- /dev/null +++ b/exporter/prometheusexporter/testdata/certs/ca.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDNjCCAh4CCQCJD1dy4qbuGDANBgkqhkiG9w0BAQsFADBdMQswCQYDVQQGEwJB +VTESMBAGA1UECAwJQXVzdHJhbGlhMQ8wDQYDVQQHDAZTeWRuZXkxEjAQBgNVBAoM +CU15T3JnTmFtZTEVMBMGA1UEAwwMTXlDb21tb25OYW1lMB4XDTIyMDgwODA4NDY1 +NVoXDTMyMDgwNTA4NDY1NVowXTELMAkGA1UEBhMCQVUxEjAQBgNVBAgMCUF1c3Ry +YWxpYTEPMA0GA1UEBwwGU3lkbmV5MRIwEAYDVQQKDAlNeU9yZ05hbWUxFTATBgNV +BAMMDE15Q29tbW9uTmFtZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB +APD2FhCOYLwPUNAyteR41+dTkytlRHVWtbWyehYzXIafJmGCvLa3hoIUiFQ0tkO8 +rU6B8kvpJvUo24lJf8msZatuXdDc3wZ64q07NtGKnf/Ejg1jSOQ2vIKAwXr1B+ZL +bT0rJL9eH7+I0w/rJwbfFbLN/74r+V/fMpcD2KXGtTySiwCjSt9zBpMQ9hN0PjQS +1MJsFDkymM1WwjTNP058QwEWHJIZjNCcmXxYm1+QVNhqEGDEMyG8hztfJmZXbP1l +V5NNHg1uw5liEWtnJLWN5jfNViswbf2zB7IDny3vpwCJlMgBwn3m8l+Rz7Qpk9qD +KfB2dgi4YrQ+KBvGBdU6FE8CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA5HuubroO +JOZXWNTQngTcNpjThd04/fsIWM2Dss6xGsZD4bzfzok9RgI3bfLOwnlUzlCvHDR1 +X3jUREOVssJKV64RvmWL3Fi0izTf1Mi+NJY/pLPNdxCRxL9E7JdfEndNfvFhxjiB +3dbNL+QbsxS4rf7GOHPefuIdGlS3Cp3qBVFKYKfW+hLu7lrQMXFQpFiI8ks4cuL4 +nFa/h/F4FbTdM92MPt8p02gzzISJiiH1MgJhtokM++wcB9GfH+lLpdQRFiClPjse +gHn03R8l5Y+4RBytRc4nIDrLRKb1fkk8EEw4bxn8OUhoXjJTFEuoPNMZRj1PEaAz +SRWDwjDPRiIx2g== +-----END CERTIFICATE----- diff --git a/exporter/prometheusexporter/testdata/certs/client.crt b/exporter/prometheusexporter/testdata/certs/client.crt new file mode 100644 index 000000000000..8115a8673110 --- /dev/null +++ b/exporter/prometheusexporter/testdata/certs/client.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDVTCCAj2gAwIBAgIJAOeyWM/nvfhTMA0GCSqGSIb3DQEBCwUAMF0xCzAJBgNV +BAYTAkFVMRIwEAYDVQQIDAlBdXN0cmFsaWExDzANBgNVBAcMBlN5ZG5leTESMBAG +A1UECgwJTXlPcmdOYW1lMRUwEwYDVQQDDAxNeUNvbW1vbk5hbWUwHhcNMjIwODA4 +MDg0NjU2WhcNMzIwODA1MDg0NjU2WjBdMQswCQYDVQQGEwJBVTESMBAGA1UECAwJ +QXVzdHJhbGlhMQ8wDQYDVQQHDAZTeWRuZXkxEjAQBgNVBAoMCU15T3JnTmFtZTEV +MBMGA1UEAwwMTXlDb21tb25OYW1lMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB +CgKCAQEA45lyLxdNUjF2cIECtJTo7cmtg++7xRv/HbczQ1m3Lb77HvGnV18WZFkP +UXqFEkolBnLNAdQSJucp5Hnw8eMlMT5CyH3pu4KobHNbXjvT2NkrT5dSVL3ZjuXv +KAKqSK9tzwedaKVyATFTrdhgrD8LOoAXkMGRl4dPEsOylHNxyELNL4gwrlHM8dAt +EZP5a0zlnMITg8yMaKp+q6MzXthCA5WRUinyYeMw1pJ5qmt/wj/kSWqjlQHhtAjB +lyoDaLmSFUZaUKv7rKvTG8cvH89fZKblBHP3OVAuPZJyfRNRuG3ljagS2n6hndRx +6FyzecoLPcBsPr0Usau+6WNcAtxm6wIDAQABoxgwFjAUBgNVHREEDTALgglsb2Nh +bGhvc3QwDQYJKoZIhvcNAQELBQADggEBANCyuYlLV6r8rOMKnphsn0Lh38hUgBzM +FLQSaU/LpQYHscWVPXhuWXxXMAfC8XDwPPju4EeyPwRjBKhLKBYjuclP8llh3Epc +FiAM8/iyaKlVU6pFsVREkvq28MyqnncOEZw91ZhOKAWDIp/SBRsLQyAIL4vGLGF5 +H4wIJEXiLassmXJn5V8n/WP55pn7+9Sx7zrqwkyNPPw24xibthgYaimut7SqqzUu +Zr2alUMt8r77/vy9EtLTfw6gY3YTUARyq2EFr5b+Orx70fEYJvdVY6L0JzE3OZlk +P7qDX6wDRWw2e1oIuwPpQZkY6dLyXFvIVknGEFkswOBSLaJrwQ+gx8I= +-----END CERTIFICATE----- diff --git a/exporter/prometheusexporter/testdata/certs/client.key b/exporter/prometheusexporter/testdata/certs/client.key new file mode 100644 index 000000000000..be77b3e8423e --- /dev/null +++ b/exporter/prometheusexporter/testdata/certs/client.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEA45lyLxdNUjF2cIECtJTo7cmtg++7xRv/HbczQ1m3Lb77HvGn +V18WZFkPUXqFEkolBnLNAdQSJucp5Hnw8eMlMT5CyH3pu4KobHNbXjvT2NkrT5dS +VL3ZjuXvKAKqSK9tzwedaKVyATFTrdhgrD8LOoAXkMGRl4dPEsOylHNxyELNL4gw +rlHM8dAtEZP5a0zlnMITg8yMaKp+q6MzXthCA5WRUinyYeMw1pJ5qmt/wj/kSWqj +lQHhtAjBlyoDaLmSFUZaUKv7rKvTG8cvH89fZKblBHP3OVAuPZJyfRNRuG3ljagS +2n6hndRx6FyzecoLPcBsPr0Usau+6WNcAtxm6wIDAQABAoIBACIhjMILGIlO6ZgN +gL5b5mJicCbs+JeijyuBoapj9dhiX35QduvfUphR0r5BRVtsywighqXTi7VRzTJ6 +JuvfSGCOGa2/VCHOgjthQT5scmR+jMQuySX0IoAPc0HoxGiKBkmZtr1Osj4Di6zy +Wez6t1BajS4kGRVEMVN7DXi9pz6EnYjI+6cCyjg8tEzcehIoSQVSPFlvLCfS3Gpz +iF8PrUE0EgDJXxL7nHP7+VqDlBrOWDMhl7crMgQKhyDsUVSwLPl9dc+vHqLF3EGG +YOPXow4cYc3NS458pIH5AbZxpUe+jHfyUDPAcr5zq1mWwNhLMcOBYZayDNt/c2zC +TGHH+6ECgYEA/o3gX86ebbB75DFM6z9OlRosipiXXHYcPJuzELXfETQr7CHrSklu +ZYELbMqScEPtWmfvU+vvBLuTTmnJjRI3jU368ekwFBeE1Z4+Zi3uDoz1m/u4zEvr +/EWrWSg7HRg/ZvczppL+piUbFvnGCGnjuBe1Nk+qVuHIJr3V5AK4OScCgYEA5ORg +iYCdJamMo3uA6OgaXRNTiage2UkqOjay5q6ZOLS0edJ895W6uMxmTJM+DwO6jFfM +FxB160PGiHJdzHqE5IossHmxgMzHKKRUUDuHvULREOv/YMzg1ctfjZ/GgAZLWaOC +sYNkuSE+dz1NkO9JW5890c2jDyVRi46y2BBIFp0CgYEAgBw5Ox6Kn1u9zmd9dbvD +uciVeB85th//MAWQRi2yGT9vh4S+nQF93PnoHRWKQ5P8JY6/ZTYNcg1RIpqdBDGR +4bzIOe2I8+OuR7A/aT9eWtZYWeuC4tam6qzJgrJ7pbXpWtG4nfHG1SlBi/uizeM0 +5xaPbckESnuRNuY9emZf/usCgYApdOu/O9o6nhDo80+P36oWvY9d2WNs+4F4dbvF +aE48JQ6jGeqkdSwSmILWz7xnv8cj8Cr46nRRHj9xKzWt+WrkcdpSRFhP+ccAp3LV +7VJShy2SoQvqHnGCh4lQ/2VWfs1a+PCM8j1mAo1rz2CjJZPl0lvCCjrEXX/9hOUD +mT9VSQKBgBox20mwhG4R8d4WWb2ySEMzTqYQOK4cynwVjcEWTPVtKPyRUB4lTYBp +9XseRswvjdWVjnskYbtyVDpE2qEosBeiTncVhHSoyCf3VIWEC3TrdF06g372QU5d +1b//pJS/1Kxm2fyF3HghXjyft2fgFUdOMKiCmHL6Nkd5PdeWpgnQ +-----END RSA PRIVATE KEY----- diff --git a/exporter/prometheusexporter/testdata/certs/server.crt b/exporter/prometheusexporter/testdata/certs/server.crt new file mode 100644 index 000000000000..4e2b228e29b5 --- /dev/null +++ b/exporter/prometheusexporter/testdata/certs/server.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDVTCCAj2gAwIBAgIJAOeyWM/nvfhSMA0GCSqGSIb3DQEBCwUAMF0xCzAJBgNV +BAYTAkFVMRIwEAYDVQQIDAlBdXN0cmFsaWExDzANBgNVBAcMBlN5ZG5leTESMBAG +A1UECgwJTXlPcmdOYW1lMRUwEwYDVQQDDAxNeUNvbW1vbk5hbWUwHhcNMjIwODA4 +MDg0NjU2WhcNMzIwODA1MDg0NjU2WjBdMQswCQYDVQQGEwJBVTESMBAGA1UECAwJ +QXVzdHJhbGlhMQ8wDQYDVQQHDAZTeWRuZXkxEjAQBgNVBAoMCU15T3JnTmFtZTEV +MBMGA1UEAwwMTXlDb21tb25OYW1lMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB +CgKCAQEAu1tovGIcr07qhnpxpgX/oerW/JzX/NzzaaKA0QXTwsLD8TSKiMBtMSBX +acqtymxazJXhgVYrJzpnKox/vkvvPNjFbt6D3xpMbBBcbe7RbExbhSdmVBq3crvp +ha/phU9/pmo4UFztDkXO7hAgx52+SGTz8V/ImldEOPSyp5I/7eokPvGSGy2rjgIv +IlnVMVeyNk9+MaZpw3fwDCm8COHeVW2swCZtouF8IOPh6c6cRLz5uwXLFcg+fJvZ +bmuj9ikFQB5n7pPUEDrzsu2evkEBeCO2VKOvLGDDx6jltb7cFV3smOg3MiCDk6p9 +9OWy/h3BdtXpNFkyRFUEPXLzkUun8wIDAQABoxgwFjAUBgNVHREEDTALgglsb2Nh +bGhvc3QwDQYJKoZIhvcNAQELBQADggEBAAY/EnzWK1nRqygXa7t/W+l7b5i2gZPI +7kvBZuSKetINB5rWxuMw85XrhYEEa+iXgKuRou1JAO+genbHDDSsrzohz8cQKZiE +VDH7S2qP3cDE5F+t8ACkgcOAWOLOwXy63/WsRTfSyk5cK6YzS/XFOqhvO4MeA1Hs +MTS1slweQ9+iCt3+r6I+VawHb55VaOAGDOgP70kRKYmCJpoLr8PN8ryRVip97RxK +gZnMweJFwFpztRqct07Ejf3rH63/f1UgVsdjHLB8/rLvKarTraW00f4pv5YqXCBV +Oqx1ueSs+i0iz7cvzx7uUI31xdzUxsUwbuxnqktImPxuXvhytuiEmcU= +-----END CERTIFICATE----- diff --git a/exporter/prometheusexporter/testdata/certs/server.key b/exporter/prometheusexporter/testdata/certs/server.key new file mode 100644 index 000000000000..e53c4af39359 --- /dev/null +++ b/exporter/prometheusexporter/testdata/certs/server.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAu1tovGIcr07qhnpxpgX/oerW/JzX/NzzaaKA0QXTwsLD8TSK +iMBtMSBXacqtymxazJXhgVYrJzpnKox/vkvvPNjFbt6D3xpMbBBcbe7RbExbhSdm +VBq3crvpha/phU9/pmo4UFztDkXO7hAgx52+SGTz8V/ImldEOPSyp5I/7eokPvGS +Gy2rjgIvIlnVMVeyNk9+MaZpw3fwDCm8COHeVW2swCZtouF8IOPh6c6cRLz5uwXL +Fcg+fJvZbmuj9ikFQB5n7pPUEDrzsu2evkEBeCO2VKOvLGDDx6jltb7cFV3smOg3 +MiCDk6p99OWy/h3BdtXpNFkyRFUEPXLzkUun8wIDAQABAoIBAEgor5EGlJesZEVA ++W6BFvCsuvp2CfXYv2Mq9EQM4386di77FnQO8L8f/qq/Jeo1i08KVtzybBJiuwM3 +M6f+JPkhYAEQcGfIADEKqB6rATuduQ3ym8W+uHLXDtyvYNg4Qyi79k51S9PYbRdh +XM6k4XmOcCnBw0g2Xg81p21EtZySjDBqJweTrYKzmMt78iQtu5IftINwQSKgbxa0 +hz0aT+wWCjnNM4nw6OfnER5M8Fs83HXfu2wIuFassMN3tfsxWpBqvDYo5CaBqMKz +n+EzlN+YjxPp2stbuliobUEcHIQIbE56F82W+HjKOYeuiVcfxuAzBYDUwmpRwIS8 +MDGxkxECgYEA+IkGDFLA0Dn0YuEkimSXqrl2WmOzcopKoK8P8sOEDXsYUSm9waeT +kSaMJkUyJaN0l/UVbSt9fLcYoPcPZfUbvlpjX9S2ZYIoqM2s1Z3Lf1AxglEvUNNd +DF11Kw3V57dhCuoyV5GiBSZpjHzGdO0YHRCwzbU5funQTJCdBOc3zGkCgYEAwPv9 +PI8LIBa7ntjeF9V2rEzkmBgFJ7G7XW32A+BihYXB/O6FLZuomeHuPlJoaCUmWQC7 +QldCOTkDEa5Gkufa7FmSdNOoz5JNfXNsRQapo4MUbSZnxl0/qPfpXgBOUd3sKeqb +PLnFVjHqIfXNyjxMONIUYDD+3iH5Sz1YWheQtfsCgYEAhAZWHim/n12KAxTSkmnJ +X7nRs0aPRuNLrXwRnsZZ2cdwLX1c/giFDXBKtvG/J9tizm41CZ82tA+Xl7pmhaTl +LgNaEsgudFHmQ3WuwHNobKMvCakS137QSQdEfLCG1ubFOEUcpQQJ625yV1zZF7fz +PU1mfoAsoMyYCHQx1S72pzECgYEAg8/Z4PIm/Czqy41+7LKNkxQWEthdNQaNXjxM +C8vIH0EEBdl4t3UZTLnne0PYMkIllicMwDxPbDxHn1z0sUSR4Fsx0H/9ToG0Udpz +ehYZ6igO3JqdyPxKDgP+rojw9bPpqZunmmucoMoJDFcSz24t9aVv61+rxVdZKg3l +eeucmN8CgYA57jmu/rSu1seiOE5trW73AkiF0mFojh0IX0tr8Um/myR06XEtoBLQ +RLtnwhFyBH8nFkuzmhf1motE0RoOe3kKUOUlc3asS/rlm33wSxG4tAgSquDrQK2K +g4wJcgEJKsVeObmVSxq4PoYTHLHxRc7zoeAP3Zg5scPrXq0htU8KTw== +-----END RSA PRIVATE KEY----- diff --git a/exporter/prometheusexporter/testdata/config.yaml b/exporter/prometheusexporter/testdata/config.yaml index 553d077090a6..df49a4a80277 100644 --- a/exporter/prometheusexporter/testdata/config.yaml +++ b/exporter/prometheusexporter/testdata/config.yaml @@ -8,6 +8,10 @@ exporters: prometheus: prometheus/2: endpoint: "1.2.3.4:1234" + tls: + ca_file: "certs/ca.crt" + key_file: "certs/server.key" + cert_file: "certs/server.crt" namespace: test-space const_labels: label1: value1 diff --git a/unreleased/promexp.yaml b/unreleased/promexp.yaml new file mode 100755 index 000000000000..3e78b6b3a7c2 --- /dev/null +++ b/unreleased/promexp.yaml @@ -0,0 +1,11 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: prometheusexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Use HTTPServerSettings in the exporter to leverage inbuilt TLS. + +# One or more tracking issues related to the change +issues: [10851]