Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce simplified parsers #2972

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tons o tests
jaronoff97 committed May 20, 2024
commit 36aeffdab8ef20e35c50928472e6ad1e5d1bd69b
5 changes: 0 additions & 5 deletions internal/components/component.go
Original file line number Diff line number Diff line change
@@ -48,11 +48,6 @@ func WithTargetPort(targetPort int32) PortBuilderOption {
servicePort.TargetPort = intstr.FromInt32(targetPort)
}
}
func WithNodePort(nodePort int32) PortBuilderOption {
return func(servicePort *corev1.ServicePort) {
servicePort.NodePort = nodePort
}
}

func WithAppProtocol(proto *string) PortBuilderOption {
return func(servicePort *corev1.ServicePort) {
1 change: 1 addition & 0 deletions internal/components/component_test.go
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ func TestReceiverParsePortFromEndpoint(t *testing.T) {
{"no protocol", "0.0.0.0:1234", 1234, false},
{"just port", ":1234", 1234, false},
{"no port at all", "http://localhost", 0, true},
{"overflow", "0.0.0.0:2147483648", 0, true},
} {
t.Run(tt.desc, func(t *testing.T) {
// test
5 changes: 2 additions & 3 deletions internal/components/multi_endpoint.go
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
package components

import (
"errors"
"fmt"

"github.com/go-logr/logr"
@@ -84,7 +83,7 @@ type MultiProtocolEndpointConfig struct {
Protocols map[string]*SingleEndpointConfig `json:"protocols"`
}

// MultiPortOption allows the setting of options for
// MultiPortOption allows the setting of options for a MultiPortReceiver.
type MultiPortOption func(parser *MultiPortReceiver)

// MultiPortReceiver is a special parser for components with endpoints for each protocol.
@@ -109,7 +108,7 @@ func (m *MultiPortReceiver) Ports(logger logr.Logger, config interface{}) ([]cor
}
ports = append(ports, ConstructServicePort(defaultSvc, port))
} else {
return nil, errors.New(fmt.Sprintf("unknown protocol set: %s", protocol))
return nil, fmt.Errorf("unknown protocol set: %s", protocol)
}
}
return ports, nil
25 changes: 25 additions & 0 deletions internal/components/multi_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -329,6 +329,31 @@ func TestMultiEndpointParsers(t *testing.T) {
p := components.BuilderFor(tt.receiverName)
assert.Equal(t, tt.parserName, p.ParserName())
})

t.Run("bad config errors", func(t *testing.T) {
// prepare
parser := components.BuilderFor(tt.receiverName)

// test
_, err := parser.Ports(logger, []interface{}{"junk"})

// verify
assert.ErrorContains(t, err, "cannot unmarshal array")
})
t.Run("good config, unknown protocol", func(t *testing.T) {
// prepare
parser := components.BuilderFor(tt.receiverName)

// test
_, err := parser.Ports(logger, map[string]interface{}{
"protocols": map[string]interface{}{
"garbage": map[string]interface{}{},
},
})

// verify
assert.ErrorContains(t, err, "unknown protocol set: garbage")
})
for _, kase := range tt.cases {
t.Run(kase.name, func(t *testing.T) {
// prepare
98 changes: 98 additions & 0 deletions internal/components/scraper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright The 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 components_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/internal/components"
)

func TestScraperParsers(t *testing.T) {
for _, tt := range []struct {
receiverName string
parserName string
defaultPort int
}{
{"prometheus", "__prometheus", 0},
{"kubeletstats", "__kubeletstats", 0},
{"sshcheck", "__sshcheck", 0},
{"cloudfoundry", "__cloudfoundry", 0},
{"vcenter", "__vcenter", 0},
{"oracledb", "__oracledb", 0},
{"snmp", "__snmp", 0},
{"googlecloudpubsub", "__googlecloudpubsub", 0},
{"chrony", "__chrony", 0},
{"jmx", "__jmx", 0},
{"podman_stats", "__podman_stats", 0},
{"pulsar", "__pulsar", 0},
{"docker_stats", "__docker_stats", 0},
{"aerospike", "__aerospike", 0},
{"zookeeper", "__zookeeper", 0},
{"prometheus_simple", "__prometheus_simple", 0},
{"saphana", "__saphana", 0},
{"riak", "__riak", 0},
{"redis", "__redis", 0},
{"rabbitmq", "__rabbitmq", 0},
{"purefb", "__purefb", 0},
{"postgresql", "__postgresql", 0},
{"nsxt", "__nsxt", 0},
{"nginx", "__nginx", 0},
{"mysql", "__mysql", 0},
{"memcached", "__memcached", 0},
{"httpcheck", "__httpcheck", 0},
{"haproxy", "__haproxy", 0},
{"flinkmetrics", "__flinkmetrics", 0},
{"couchdb", "__couchdb", 0},
} {
t.Run(tt.receiverName, func(t *testing.T) {
t.Run("builds successfully", func(t *testing.T) {
// test
parser := components.BuilderFor(tt.receiverName)

// verify
assert.Equal(t, tt.parserName, parser.ParserName())
})

t.Run("default is nothing", func(t *testing.T) {
// prepare
parser := components.BuilderFor(tt.receiverName)

// test
ports, err := parser.Ports(logger, map[string]interface{}{})

// verify
assert.NoError(t, err)
assert.Len(t, ports, 0)
})

t.Run("always returns nothing", func(t *testing.T) {
// prepare
parser := components.BuilderFor(tt.receiverName)

// test
ports, err := parser.Ports(logger, map[string]interface{}{
"endpoint": "0.0.0.0:65535",
})

// verify
assert.NoError(t, err)
assert.Len(t, ports, 0)
})
})
}
}
64 changes: 45 additions & 19 deletions internal/components/single_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/open-telemetry/opentelemetry-operator/internal/components"
@@ -59,25 +60,28 @@ func TestFailedToParseEndpoint(t *testing.T) {

func TestDownstreamParsers(t *testing.T) {
for _, tt := range []struct {
desc string
receiverName string
parserName string
defaultPort int
desc string
receiverName string
parserName string
defaultPort int
listenAddrParser bool
}{
{"zipkin", "zipkin", "__zipkin", 9411},
{"opencensus", "opencensus", "__opencensus", 55678},
{"zipkin", "zipkin", "__zipkin", 9411, false},
{"opencensus", "opencensus", "__opencensus", 55678, false},

// contrib receivers
{"carbon", "carbon", "__carbon", 2003},
{"collectd", "collectd", "__collectd", 8081},
{"sapm", "sapm", "__sapm", 7276},
{"signalfx", "signalfx", "__signalfx", 9943},
{"wavefront", "wavefront", "__wavefront", 2003},
{"fluentforward", "fluentforward", "__fluentforward", 8006},
{"statsd", "statsd", "__statsd", 8125},
{"influxdb", "influxdb", "__influxdb", 8086},
{"splunk_hec", "splunk_hec", "__splunk_hec", 8088},
{"awsxray", "awsxray", "__awsxray", 2000},
{"carbon", "carbon", "__carbon", 2003, false},
{"collectd", "collectd", "__collectd", 8081, false},
{"sapm", "sapm", "__sapm", 7276, false},
{"signalfx", "signalfx", "__signalfx", 9943, false},
{"wavefront", "wavefront", "__wavefront", 2003, false},
{"fluentforward", "fluentforward", "__fluentforward", 8006, false},
{"statsd", "statsd", "__statsd", 8125, false},
{"influxdb", "influxdb", "__influxdb", 8086, false},
{"splunk_hec", "splunk_hec", "__splunk_hec", 8088, false},
{"awsxray", "awsxray", "__awsxray", 2000, false},
{"tcplog", "tcplog", "__tcplog", 0, true},
{"udplog", "udplog", "__udplog", 0, true},
} {
t.Run(tt.receiverName, func(t *testing.T) {
t.Run("builds successfully", func(t *testing.T) {
@@ -87,6 +91,16 @@ func TestDownstreamParsers(t *testing.T) {
// verify
assert.Equal(t, tt.parserName, parser.ParserName())
})
t.Run("bad config errors", func(t *testing.T) {
// prepare
parser := components.BuilderFor(tt.receiverName)

// test throwing in pure junk
_, err := parser.Ports(logger, func() {})

// verify
assert.ErrorContains(t, err, "unsupported type")
})

t.Run("assigns the expected port", func(t *testing.T) {
// prepare
@@ -95,6 +109,10 @@ func TestDownstreamParsers(t *testing.T) {
// test
ports, err := parser.Ports(logger, map[string]interface{}{})

if tt.defaultPort == 0 {
assert.Len(t, ports, 0)
return
}
// verify
assert.NoError(t, err)
assert.Len(t, ports, 1)
@@ -107,9 +125,17 @@ func TestDownstreamParsers(t *testing.T) {
parser := components.BuilderFor(tt.receiverName)

// test
ports, err := parser.Ports(logger, map[string]interface{}{
"endpoint": "0.0.0.0:65535",
})
var ports []corev1.ServicePort
var err error
if tt.listenAddrParser {
ports, err = parser.Ports(logger, map[string]interface{}{
"listen_address": "0.0.0.0:65535",
})
} else {
ports, err = parser.Ports(logger, map[string]interface{}{
"endpoint": "0.0.0.0:65535",
})
}

// verify
assert.NoError(t, err)