Skip to content

Commit

Permalink
Merge branch 'main' into better-version-validations
Browse files Browse the repository at this point in the history
  • Loading branch information
mowies authored Aug 7, 2024
2 parents b03017e + 72c7d7f commit 559644b
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 2 deletions.
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_limit-for-component-names.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: component

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow component names of up to 1024 characters in length.

# One or more tracking issues or pull requests related to the change
issues: [10816]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
7 changes: 5 additions & 2 deletions component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,14 @@ var (
)

// nameRegexp is used to validate the name of a component. A name can consist of
// 1 to 63 unicode characters excluding whitespace, control characters, and
// 1 to 1024 unicode characters excluding whitespace, control characters, and
// symbols.
var nameRegexp = regexp.MustCompile(`^[^\pZ\pC\pS]{1,63}$`)
var nameRegexp = regexp.MustCompile(`^[^\pZ\pC\pS]+$`)

func validateName(nameStr string) error {
if len(nameStr) > 1024 {
return fmt.Errorf("name %q is longer than 1024 characters (%d characters)", nameStr, len(nameStr))
}
if !nameRegexp.MatchString(nameStr) {
return fmt.Errorf("invalid character(s) in name %q", nameStr)
}
Expand Down
10 changes: 10 additions & 0 deletions component/identifiable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package component

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,6 +44,11 @@ func TestUnmarshalText(t *testing.T) {
idStr: "valid_type/name-with-dashes",
expectedID: ID{typeVal: validType, nameVal: "name-with-dashes"},
},
// issue 10816
{
idStr: "valid_type/Linux-Messages-File_01J49HCH3SWFXRVASWFZFRT3J2__processor0__logs",
expectedID: ID{typeVal: validType, nameVal: "Linux-Messages-File_01J49HCH3SWFXRVASWFZFRT3J2__processor0__logs"},
},
{
idStr: "valid_type/1",
expectedID: ID{typeVal: validType, nameVal: "1"},
Expand Down Expand Up @@ -71,6 +77,10 @@ func TestUnmarshalText(t *testing.T) {
idStr: "valid_type/invalid name",
expectedErr: true,
},
{
idStr: "valid_type/" + strings.Repeat("a", 1025),
expectedErr: true,
},
}

for _, test := range testCases {
Expand Down
13 changes: 13 additions & 0 deletions confmap/internal/e2e/testdata/indirect-slice-env-var-main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
receivers:
nop:
otlp:
protocols:
grpc:

exporters:
nop:
otlp:
endpoint: localhost:4317

service:
pipelines: ${file:${env:BASE_FOLDER}/indirect-slice-env-var-pipelines.yaml}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
logs:
receivers: ${env:OTEL_LOGS_RECEIVER}
exporters: ${env:OTEL_LOGS_EXPORTER}
51 changes: 51 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,54 @@ logging:
cfgStr.Field,
)
}

func TestIndirectSliceEnvVar(t *testing.T) {
previousValue := globalgates.StrictlyTypedInputGate.IsEnabled()
err := featuregate.GlobalRegistry().Set(globalgates.StrictlyTypedInputID, true)
require.NoError(t, err)
defer func() {
seterr := featuregate.GlobalRegistry().Set(globalgates.StrictlyTypedInputID, previousValue)
require.NoError(t, seterr)
}()

// This replicates the situation in https://github.com/open-telemetry/opentelemetry-collector/issues/10799
// where a configuration file is loaded that contains a reference to a slice of strings in an environment variable.
t.Setenv("BASE_FOLDER", "testdata")
t.Setenv("OTEL_LOGS_RECEIVER", "[nop, otlp]")
t.Setenv("OTEL_LOGS_EXPORTER", "[otlp, nop]")
resolver := NewResolver(t, "indirect-slice-env-var-main.yaml")
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)

type CollectorConf struct {
Exporters struct {
OTLP struct {
Endpoint string `mapstructure:"endpoint"`
} `mapstructure:"otlp"`
Nop struct{} `mapstructure:"nop"`
} `mapstructure:"exporters"`
Receivers struct {
OTLP struct {
Protocols struct {
GRPC struct{} `mapstructure:"grpc"`
} `mapstructure:"protocols"`
} `mapstructure:"otlp"`
Nop struct{} `mapstructure:"nop"`
} `mapstructure:"receivers"`
Service struct {
Pipelines struct {
Logs struct {
Exporters []string `mapstructure:"exporters"`
Receivers []string `mapstructure:"receivers"`
} `mapstructure:"logs"`
} `mapstructure:"pipelines"`
} `mapstructure:"service"`
}

var collectorConf CollectorConf
err = conf.Unmarshal(&collectorConf)
require.NoError(t, err)
assert.Equal(t, collectorConf.Exporters.OTLP.Endpoint, "localhost:4317")
assert.Equal(t, collectorConf.Service.Pipelines.Logs.Receivers, []string{"nop", "otlp"})
assert.Equal(t, collectorConf.Service.Pipelines.Logs.Exporters, []string{"otlp", "nop"})
}
52 changes: 52 additions & 0 deletions receiver/otlpreceiver/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otlpreceiver

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/logs"
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/metrics"
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/trace"
"go.opentelemetry.io/collector/receiver/receivertest"
)

func FuzzReceiverHandlers(f *testing.F) {
f.Fuzz(func(_ *testing.T, data []byte, pb bool, handler int) {
req, err := http.NewRequest("POST", "", bytes.NewReader(data))
if err != nil {
return
}
if pb {
req.Header.Add("Content-Type", pbContentType)
} else {
req.Header.Add("Content-Type", jsonContentType)
}
set := receivertest.NewNopSettings()
set.TelemetrySettings = componenttest.NewNopTelemetrySettings()
set.ID = otlpReceiverID
cfg := createDefaultConfig().(*Config)
r, err := newOtlpReceiver(cfg, &set)
if err != nil {
panic(err)
}
resp := httptest.NewRecorder()
switch handler % 3 {
case 0:
httpTracesReceiver := trace.New(r.nextTraces, r.obsrepHTTP)
handleTraces(resp, req, httpTracesReceiver)
case 1:
httpMetricsReceiver := metrics.New(r.nextMetrics, r.obsrepHTTP)
handleMetrics(resp, req, httpMetricsReceiver)
case 2:
httpLogsReceiver := logs.New(r.nextLogs, r.obsrepHTTP)
handleLogs(resp, req, httpLogsReceiver)
}

})
}

0 comments on commit 559644b

Please sign in to comment.