diff --git a/cmd/otelcontribcol/components.go b/cmd/otelcontribcol/components.go index 127700964069..5c1c56a0b3ba 100644 --- a/cmd/otelcontribcol/components.go +++ b/cmd/otelcontribcol/components.go @@ -36,6 +36,7 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerlegacyreceiver" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver" @@ -75,6 +76,7 @@ func components() (config.Factories, error) { &redisreceiver.Factory{}, &simpleprometheusreceiver.Factory{}, &k8sclusterreceiver.Factory{}, + &receivercreator.Factory{}, } for _, rcv := range factories.Receivers { receivers = append(receivers, rcv) diff --git a/extension/observer/k8sobserver/factory.go b/extension/observer/k8sobserver/factory.go index 24ea28204474..1fde88a31ac2 100644 --- a/extension/observer/k8sobserver/factory.go +++ b/extension/observer/k8sobserver/factory.go @@ -25,6 +25,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/clientcmd" ) const ( @@ -82,11 +83,11 @@ func (f *Factory) CreateExtension( // NewFactory should be called to create a factory with default values. func NewFactory() component.ExtensionFactory { return &Factory{createK8sConfig: func() (*rest.Config, error) { - restConfig, err := rest.InClusterConfig() - if err != nil { - return nil, err - } - return restConfig, nil + // TODO: will be made configurable when shared k8s config library lands shortly in separate PR. + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + configOverrides := &clientcmd.ConfigOverrides{} + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + loadingRules, configOverrides).ClientConfig() }} } diff --git a/extension/observer/k8sobserver/go.sum b/extension/observer/k8sobserver/go.sum index fbdf530663f2..039fbf6868ac 100644 --- a/extension/observer/k8sobserver/go.sum +++ b/extension/observer/k8sobserver/go.sum @@ -612,6 +612,7 @@ github.com/hashicorp/serf v0.8.3 h1:MWYcmct5EtKz0efYooPcL0yNkem+7kWxqXDi/UIh+8k= github.com/hashicorp/serf v0.8.3/go.mod h1:UpNcs7fFbpKIyZaUuSW6EPiH+eZC7OuyFD+wc1oal+k= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q= github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= diff --git a/go.mod b/go.mod index c62b4c4905c3..53babcc95adf 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver v0.0.0 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerlegacyreceiver v0.0.0 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver v0.0.0 + github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator v0.0.0 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver v0.0.0 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sapmreceiver v0.0.0 github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver v0.0.0 diff --git a/go.sum b/go.sum index 2fab6d5c22a7..5395363c6360 100644 --- a/go.sum +++ b/go.sum @@ -75,6 +75,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/antonmedv/expr v1.8.4 h1:7kYRFeC4dN9XYeADFkAV+gyx3iK6okBCLVOj+D5ZCiA= +github.com/antonmedv/expr v1.8.4/go.mod h1:5qsM3oLGDND7sDmQGDXHkYfkjYMUX14qsgqmHhwGEk8= github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7 h1:Fv9bK1Q+ly/ROk4aJsVMeuIwPel4bEnD8EPiI91nZMg= github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= @@ -147,6 +149,7 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -203,6 +206,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= @@ -790,6 +795,8 @@ github.com/lightstep/lightstep-tracer-go v0.17.1/go.mod h1:jlF1pusYV4pidLvZ+XD0U github.com/lightstep/opentelemetry-exporter-go v0.1.5 h1:477kDqi+JAZKqMsbd/1PXHXD+HspKici6gij/PH2anc= github.com/lightstep/opentelemetry-exporter-go v0.1.5/go.mod h1:t79BZ3OWNBH/qxmO6fRXZtNhjLgMYgZxbd+31GX9Qj0= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/luna-duclos/instrumentedsql v0.0.0-20181127104832-b7d587d28109/go.mod h1:PWUIzhtavmOR965zfawVsHXbEuU1G29BPZ/CB3C7jXk= github.com/luna-duclos/instrumentedsql v1.1.2/go.mod h1:4LGbEqDnopzNAiyxPPDXhLspyunZxgPTMJBKtC6U0BQ= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= @@ -834,6 +841,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= +github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= @@ -1018,6 +1027,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:5STLWrekHfjyYwxBRVRXNOSewLJ3PWfDJd1VyTS21fI= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rivo/tview v0.0.0-20200219210816-cd38d7432498/go.mod h1:6lkG1x+13OShEf0EaOCaTQYyB7d5nSbb181KtjlS+84= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= @@ -1036,6 +1047,7 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/samuel/go-zookeeper v0.0.0-20190810000440-0ceca61e4d75 h1:cA+Ubq9qEVIQhIWvP2kNuSZ2CmnfBJFSRq+kO1pu2cc= github.com/samuel/go-zookeeper v0.0.0-20190810000440-0ceca61e4d75/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= +github.com/sanity-io/litter v1.2.0/go.mod h1:JF6pZUFgu2Q0sBZ+HSV35P8TVPI1TTzEwyu9FXAw2W4= github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= github.com/santhosh-tekuri/jsonschema/v2 v2.1.0/go.mod h1:yzJzKUGV4RbWqWIBBP4wSOBqavX5saE02yirLS0OTyg= github.com/satori/go.uuid v0.0.0-20160603004225-b111a074d5ef/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= @@ -1143,6 +1155,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -1405,6 +1418,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1420,6 +1434,7 @@ golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200121082415-34d275377bf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200408040146-ea54a3c99b9b h1:h03Ur1RlPrGTjua4koYdpGl8W0eYo8p1uI9w7RPlkdk= golang.org/x/sys v0.0.0-20200408040146-ea54a3c99b9b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/receiver/receivercreator/config.go b/receiver/receivercreator/config.go index 20c84dc3ea27..76b45ba1b112 100644 --- a/receiver/receivercreator/config.go +++ b/receiver/receivercreator/config.go @@ -53,6 +53,7 @@ type receiverTemplate struct { // Rule is the discovery rule that when matched will create a receiver instance // based on receiverTemplate. Rule string `mapstructure:"rule"` + rule rule } // newReceiverTemplate creates a receiverTemplate instance from the full name of a subreceiver @@ -65,7 +66,7 @@ func newReceiverTemplate(name string, config userConfigMap) (receiverTemplate, e return receiverTemplate{ receiverConfig: receiverConfig{ - typeStr: configmodels.Type(typeStr), + typeStr: typeStr, fullName: fullName, config: config, }, diff --git a/receiver/receivercreator/config_test.go b/receiver/receivercreator/config_test.go index 41dd36ed49b8..149aa83a751e 100644 --- a/receiver/receivercreator/config_test.go +++ b/receiver/receivercreator/config_test.go @@ -81,7 +81,7 @@ func TestLoadConfig(t *testing.T) { assert.NotNil(t, r1) assert.Len(t, r1.receiverTemplates, 1) assert.Contains(t, r1.receiverTemplates, "examplereceiver/1") - assert.Equal(t, "enabled", r1.receiverTemplates["examplereceiver/1"].Rule) + assert.Equal(t, `type.port`, r1.receiverTemplates["examplereceiver/1"].Rule) assert.Equal(t, userConfigMap{ endpointConfigKey: "localhost:12345", }, r1.receiverTemplates["examplereceiver/1"].config) diff --git a/receiver/receivercreator/factory.go b/receiver/receivercreator/factory.go index f40ad6718403..49875c5ed95b 100644 --- a/receiver/receivercreator/factory.go +++ b/receiver/receivercreator/factory.go @@ -66,11 +66,15 @@ func (f *Factory) CustomUnmarshaler() component.CustomUnmarshaler { } // Unmarshals receiver_creator configuration like rule. - // TODO: validate discovery rule if err := receiversCfg.UnmarshalKey(subreceiverKey, &subreceiver); err != nil { return fmt.Errorf("failed to deserialize sub-receiver %q: %s", subreceiverKey, err) } + subreceiver.rule, err = newRule(subreceiver.Rule) + if err != nil { + return fmt.Errorf("subreceiver %q rule is invalid: %v", subreceiverKey, err) + } + c.receiverTemplates[subreceiverKey] = subreceiver } diff --git a/receiver/receivercreator/fixtures_test.go b/receiver/receivercreator/fixtures_test.go new file mode 100644 index 000000000000..5dbe0cbf08bb --- /dev/null +++ b/receiver/receivercreator/fixtures_test.go @@ -0,0 +1,44 @@ +// Copyright 2020, OpenTelemetry 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 receivercreator + +import ( + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" +) + +var pod = observer.Pod{ + Name: "pod-1", + Labels: map[string]string{ + "app": "redis", + "region": "west-1", + }, +} + +var podEndpoint = observer.Endpoint{ + ID: "pod-1", + Target: "localhost", + Details: pod, +} + +var portEndpoint = observer.Endpoint{ + ID: "port-1", + Target: "localhost:1234", + Details: observer.Port{ + Name: "http", + Pod: pod, + Port: 1234, + Protocol: observer.ProtocolTCP, + }, +} diff --git a/receiver/receivercreator/go.mod b/receiver/receivercreator/go.mod index 261a66572400..395c4d73a99a 100644 --- a/receiver/receivercreator/go.mod +++ b/receiver/receivercreator/go.mod @@ -1,8 +1,9 @@ -module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receiver_creator +module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator go 1.14 require ( + github.com/antonmedv/expr v1.8.4 github.com/census-instrumentation/opencensus-proto v0.2.1 github.com/open-telemetry/opentelemetry-collector v0.3.1-0.20200503151053-5d1aacc0e168 github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.0.0 diff --git a/receiver/receivercreator/go.sum b/receiver/receivercreator/go.sum index 6ed3d8efdbb5..b5a38ec44928 100644 --- a/receiver/receivercreator/go.sum +++ b/receiver/receivercreator/go.sum @@ -60,6 +60,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/antonmedv/expr v1.8.4 h1:7kYRFeC4dN9XYeADFkAV+gyx3iK6okBCLVOj+D5ZCiA= +github.com/antonmedv/expr v1.8.4/go.mod h1:5qsM3oLGDND7sDmQGDXHkYfkjYMUX14qsgqmHhwGEk8= github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7 h1:Fv9bK1Q+ly/ROk4aJsVMeuIwPel4bEnD8EPiI91nZMg= github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= @@ -122,6 +124,7 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -166,6 +169,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= @@ -713,6 +718,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/luna-duclos/instrumentedsql v0.0.0-20181127104832-b7d587d28109/go.mod h1:PWUIzhtavmOR965zfawVsHXbEuU1G29BPZ/CB3C7jXk= github.com/luna-duclos/instrumentedsql v1.1.2/go.mod h1:4LGbEqDnopzNAiyxPPDXhLspyunZxgPTMJBKtC6U0BQ= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= @@ -757,6 +764,8 @@ github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= @@ -938,6 +947,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:5STLWrekHfjyYwxBRVRXNOSewLJ3PWfDJd1VyTS21fI= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rivo/tview v0.0.0-20200219210816-cd38d7432498/go.mod h1:6lkG1x+13OShEf0EaOCaTQYyB7d5nSbb181KtjlS+84= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= @@ -955,6 +966,7 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/samuel/go-zookeeper v0.0.0-20190810000440-0ceca61e4d75 h1:cA+Ubq9qEVIQhIWvP2kNuSZ2CmnfBJFSRq+kO1pu2cc= github.com/samuel/go-zookeeper v0.0.0-20190810000440-0ceca61e4d75/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= +github.com/sanity-io/litter v1.2.0/go.mod h1:JF6pZUFgu2Q0sBZ+HSV35P8TVPI1TTzEwyu9FXAw2W4= github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= github.com/santhosh-tekuri/jsonschema/v2 v2.1.0/go.mod h1:yzJzKUGV4RbWqWIBBP4wSOBqavX5saE02yirLS0OTyg= github.com/satori/go.uuid v0.0.0-20160603004225-b111a074d5ef/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= @@ -1054,6 +1066,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= @@ -1285,6 +1298,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1298,6 +1312,7 @@ golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200121082415-34d275377bf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200408040146-ea54a3c99b9b h1:h03Ur1RlPrGTjua4koYdpGl8W0eYo8p1uI9w7RPlkdk= golang.org/x/sys v0.0.0-20200408040146-ea54a3c99b9b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/receiver/receivercreator/observerhandler.go b/receiver/receivercreator/observerhandler.go index b41fe91d5845..4dbf262ef3f9 100644 --- a/receiver/receivercreator/observerhandler.go +++ b/receiver/receivercreator/observerhandler.go @@ -67,9 +67,18 @@ func (obs *observerHandler) OnAdd(added []observer.Endpoint) { for _, e := range added { for _, template := range obs.receiverTemplates { - if !ruleMatches(template.Rule, e) { + if matches, err := template.rule.eval(e); err != nil { + obs.logger.Error("failed matching rule", zap.String("rule", template.Rule), zap.Error(err)) + continue + } else if !matches { continue } + + obs.logger.Info("starting receiver", + zap.String("name", template.fullName), + zap.String("type", string(template.typeStr)), + zap.String("endpoint", e.ID)) + rcvr, err := obs.runner.start(template.receiverConfig, userConfigMap{ endpointConfigKey: e.Target, }) @@ -90,6 +99,8 @@ func (obs *observerHandler) OnRemove(removed []observer.Endpoint) { for _, e := range removed { for _, rcvr := range obs.receiversByEndpointID.Get(e.ID) { + obs.logger.Info("stopping receiver", zap.Reflect("receiver", rcvr), zap.String("endpoint", e.ID)) + if err := obs.runner.shutdown(rcvr); err != nil { obs.logger.Error("failed to stop receiver", zap.Reflect("receiver", rcvr)) continue diff --git a/receiver/receivercreator/observerhandler_test.go b/receiver/receivercreator/observerhandler_test.go index b5c466c44eb4..13bd5e96207e 100644 --- a/receiver/receivercreator/observerhandler_test.go +++ b/receiver/receivercreator/observerhandler_test.go @@ -49,16 +49,16 @@ func TestOnAdd(t *testing.T) { handler := &observerHandler{ logger: zap.NewNop(), receiverTemplates: map[string]receiverTemplate{ - "name/1": {rcvrCfg, "enabled"}, + "name/1": {rcvrCfg, "", newRuleOrPanic(`type.port`)}, }, receiversByEndpointID: receiverMap{}, runner: runner, } - runner.On("start", rcvrCfg, userConfigMap{endpointConfigKey: "localhost"}).Return(&config.ExampleReceiverProducer{}, nil) + runner.On("start", rcvrCfg, userConfigMap{endpointConfigKey: "localhost:1234"}).Return(&config.ExampleReceiverProducer{}, nil) handler.OnAdd([]observer.Endpoint{ - {ID: "id-1", Target: "localhost", Details: nil}, + portEndpoint, }) runner.AssertExpectations(t) @@ -74,11 +74,11 @@ func TestOnRemove(t *testing.T) { runner: runner, } - handler.receiversByEndpointID.Put("id-1", rcvr) + handler.receiversByEndpointID.Put("port-1", rcvr) runner.On("shutdown", rcvr).Return(nil) - handler.OnRemove([]observer.Endpoint{{ID: "id-1", Target: "localhost", Details: nil}}) + handler.OnRemove([]observer.Endpoint{portEndpoint}) runner.AssertExpectations(t) assert.Equal(t, 0, handler.receiversByEndpointID.Size()) @@ -92,20 +92,20 @@ func TestOnChange(t *testing.T) { handler := &observerHandler{ logger: zap.NewNop(), receiverTemplates: map[string]receiverTemplate{ - "name/1": {rcvrCfg, "enabled"}, + "name/1": {rcvrCfg, "", newRuleOrPanic(`type.port`)}, }, receiversByEndpointID: receiverMap{}, runner: runner, } - handler.receiversByEndpointID.Put("id-1", oldRcvr) + handler.receiversByEndpointID.Put("port-1", oldRcvr) runner.On("shutdown", oldRcvr).Return(nil) - runner.On("start", rcvrCfg, userConfigMap{endpointConfigKey: "localhost"}).Return(newRcvr, nil) + runner.On("start", rcvrCfg, userConfigMap{endpointConfigKey: "localhost:1234"}).Return(newRcvr, nil) - handler.OnChange([]observer.Endpoint{{ID: "id-1", Target: "localhost", Details: nil}}) + handler.OnChange([]observer.Endpoint{portEndpoint}) runner.AssertExpectations(t) assert.Equal(t, 1, handler.receiversByEndpointID.Size()) - assert.Same(t, newRcvr, handler.receiversByEndpointID.Get("id-1")[0]) + assert.Same(t, newRcvr, handler.receiversByEndpointID.Get("port-1")[0]) } diff --git a/receiver/receivercreator/receiver_test.go b/receiver/receivercreator/receiver_test.go index 21f58cb231e4..7098ea4c2a34 100644 --- a/receiver/receivercreator/receiver_test.go +++ b/receiver/receivercreator/receiver_test.go @@ -73,7 +73,7 @@ func (m *mockObserver) Shutdown(ctx context.Context) error { var _ component.ServiceExtension = (*mockObserver)(nil) func (m *mockObserver) ListAndWatch(notify observer.Notify) { - notify.OnAdd([]observer.Endpoint{{ID: "foobar", Target: "169.168.1.100", Details: nil}}) + notify.OnAdd([]observer.Endpoint{portEndpoint}) } var _ observer.Observable = (*mockObserver)(nil) @@ -146,7 +146,7 @@ func TestMockedEndToEnd(t *testing.T) { assert.True(t, dyn.observerHandler.receiversByEndpointID.Values()[0].(*config.ExampleReceiverProducer).Stopped) } -func TestSafeHost(t *testing.T) { +func TestLoggingHost(t *testing.T) { core, obs := zapObserver.New(zap.ErrorLevel) host := &loggingHost{ Host: componenttest.NewNopHost(), diff --git a/receiver/receivercreator/rules.go b/receiver/receivercreator/rules.go index ffbcb1d9c69c..00b50b6292c9 100644 --- a/receiver/receivercreator/rules.go +++ b/receiver/receivercreator/rules.go @@ -15,10 +15,82 @@ package receivercreator import ( + "errors" + "fmt" + "regexp" + + "github.com/antonmedv/expr" + "github.com/antonmedv/expr/vm" + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" ) -func ruleMatches(rule string, e observer.Endpoint) bool { - // TODO - return rule == "enabled" +// rule wraps expr rule for later evaluation. +type rule struct { + program *vm.Program +} + +// ruleRe is used to verify the rule starts type check. +var ruleRe = regexp.MustCompile(`^type\.(pod|port)`) + +// newRule creates a new rule instance. +func newRule(ruleStr string) (rule, error) { + if ruleStr == "" { + return rule{}, errors.New("rule cannot be empty") + } + if !ruleRe.MatchString(ruleStr) { + // TODO: Try validating against bytecode instead. + return rule{}, errors.New("rule must specify type") + } + + // TODO: Maybe use https://godoc.org/github.com/antonmedv/expr#Env in type checking + // depending on type == specified. + v, err := expr.Compile(ruleStr) + if err != nil { + return rule{}, err + } + return rule{v}, nil +} + +// eval the rule against the given endpoint. +func (r *rule) eval(endpoint observer.Endpoint) (bool, error) { + var env map[string]interface{} + + ruleTypes := map[string]interface{}{ + "port": false, + "pod": false, + } + + switch o := endpoint.Details.(type) { + case observer.Pod: + ruleTypes["pod"] = true + env = map[string]interface{}{ + "type": ruleTypes, + "name": o.Name, + "labels": o.Labels, + } + case observer.Port: + ruleTypes["port"] = true + env = map[string]interface{}{ + "type": ruleTypes, + "name": o.Name, + "port": o.Port, + "pod": map[string]interface{}{ + "name": o.Pod.Name, + "labels": o.Pod.Labels, + }, + "protocol": o.Protocol, + } + default: + return false, fmt.Errorf("unknown endpoint details type %T", endpoint.Details) + } + + res, err := expr.Run(r.program, env) + if err != nil { + return false, err + } + if ret, ok := res.(bool); ok { + return ret, nil + } + return false, errors.New("rule did not return a boolean") } diff --git a/receiver/receivercreator/rules_test.go b/receiver/receivercreator/rules_test.go new file mode 100644 index 000000000000..9ed0f2e2a0ef --- /dev/null +++ b/receiver/receivercreator/rules_test.go @@ -0,0 +1,94 @@ +// Copyright 2020, OpenTelemetry 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 receivercreator + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer" +) + +func newRuleOrPanic(s string) rule { + r, err := newRule(s) + if err != nil { + panic(err) + } + return r +} + +func Test_ruleEval(t *testing.T) { + type args struct { + ruleStr string + endpoint observer.Endpoint + } + tests := []struct { + name string + args args + want bool + wantErr bool + }{ + // Doesn't work yet. See comment in newRule. + // {"unknown variable", args{`type == "port" && unknown_var == 1`, portEndpoint}, false, true}, + {"basic port", args{`type.port && name == "http" && pod.labels["app"] == "redis"`, portEndpoint}, true, false}, + {"basic pod", args{`type.pod && labels["region"] == "west-1"`, podEndpoint}, true, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := newRule(tt.args.ruleStr) + require.NoError(t, err) + require.NotNil(t, got) + + match, err := got.eval(tt.args.endpoint) + + if (err != nil) != tt.wantErr { + t.Errorf("eval() error = %v, wantErr %v", err, tt.wantErr) + return + } + + assert.Equal(t, tt.want, match, "expected eval to return %v but returned %v", tt.want, match) + }) + } +} + +func Test_newRule(t *testing.T) { + type args struct { + ruleStr string + } + tests := []struct { + name string + args args + wantErr bool + }{ + {"empty rule", args{""}, true}, + {"does not start with type", args{"port == 1234"}, true}, + {"invalid syntax", args{"port =="}, true}, + {"valid", args{`type.port && port_name == "http"`}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := newRule(tt.args.ruleStr) + if err == nil { + assert.NotNil(t, got, "expected rule to be created when there was no error") + } + if (err != nil) != tt.wantErr { + t.Errorf("newRule() error = %v, wantErr %v", err, tt.wantErr) + return + } + }) + } +} diff --git a/receiver/receivercreator/runner.go b/receiver/receivercreator/runner.go index 5ba1e12931f0..dd3b7ec3286c 100644 --- a/receiver/receivercreator/runner.go +++ b/receiver/receivercreator/runner.go @@ -46,7 +46,7 @@ var _ runner = (*receiverRunner)(nil) // start a receiver instance from its static config and discovered config. func (run *receiverRunner) start(receiver receiverConfig, discoveredConfig userConfigMap) (component.Receiver, error) { - factory := run.host.GetFactory(component.KindReceiver, configmodels.Type(receiver.typeStr)) + factory := run.host.GetFactory(component.KindReceiver, receiver.typeStr) if factory == nil { return nil, fmt.Errorf("unable to lookup factory for receiver %q", receiver.typeStr) diff --git a/receiver/receivercreator/testdata/config.yaml b/receiver/receivercreator/testdata/config.yaml index d28433c6e699..7c353c8c581a 100644 --- a/receiver/receivercreator/testdata/config.yaml +++ b/receiver/receivercreator/testdata/config.yaml @@ -4,7 +4,7 @@ receivers: watch_observers: [mock_observer] receivers: examplereceiver/1: - rule: enabled + rule: type.port config: endpoint: localhost:12345