Skip to content

Commit

Permalink
Remove confighttp fork dependency (aws#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Apr 2, 2024
1 parent aaba03c commit 0865173
Show file tree
Hide file tree
Showing 39 changed files with 2,095 additions and 2,226 deletions.
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ replace (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver => github.com/amazon-contributing/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.0.0-20240314211421-34bd73a6cac8
)

// Omit fields in HTTPClientSettings. Pending fix for https://github.com/open-telemetry/opentelemetry-collector/issues/8627
replace go.opentelemetry.io/collector/config/confighttp => github.com/amazon-contributing/opentelemetry-collector-contrib/config/confighttp v0.0.0-20240314211421-34bd73a6cac8

// Temporary fix, pending PR https://github.com/shirou/gopsutil/pull/957
replace github.com/shirou/gopsutil/v3 => github.com/aws/telegraf/patches/gopsutil/v3 v3.0.0-20231109213610-a8c21c54a2be // indirect

Expand Down Expand Up @@ -111,6 +108,7 @@ require (
github.com/jellydator/ttlcache/v3 v3.1.0
github.com/kardianos/service v1.2.1 // Keep this pinned to v1.2.1. v1.2.2 causes the agent to not register as a service on Windows
github.com/kr/pretty v0.3.1
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4
github.com/oklog/run v1.1.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.89.0
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter v0.89.0
Expand All @@ -135,6 +133,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v1.2.0
go.opentelemetry.io/collector/component v0.89.0
go.opentelemetry.io/collector/config/configopaque v0.89.0
go.opentelemetry.io/collector/config/configtelemetry v0.89.0
go.opentelemetry.io/collector/config/configtls v0.89.0
go.opentelemetry.io/collector/confmap v0.89.0
Expand Down Expand Up @@ -306,7 +305,6 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down Expand Up @@ -382,7 +380,6 @@ require (
go.opentelemetry.io/collector/config/configgrpc v0.89.0 // indirect
go.opentelemetry.io/collector/config/confighttp v0.89.0 // indirect
go.opentelemetry.io/collector/config/confignet v0.89.0 // indirect
go.opentelemetry.io/collector/config/configopaque v0.89.0 // indirect
go.opentelemetry.io/collector/config/internal v0.89.0 // indirect
go.opentelemetry.io/collector/connector v0.89.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.89.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1483 h1:J8HaD+Zpfi1gcel3HCKpoHHEsrcuRrZlSnx7R9SCf5I=
github.com/amazon-contributing/opentelemetry-collector-contrib/config/confighttp v0.0.0-20240314211421-34bd73a6cac8 h1:KhAQN7OLhE6jOnVvt11kpjxHJZcnWlWYyOCGXqpaXJY=
github.com/amazon-contributing/opentelemetry-collector-contrib/config/confighttp v0.0.0-20240314211421-34bd73a6cac8/go.mod h1:3sU3HgF5wc32CVljnzGo4Fn/9+T0N1Z6tCJyKdW2MvM=
github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.0.0-20240314211421-34bd73a6cac8 h1:Ojr9LNjrlDWyuSDguL4V/O5oLzFc8HBUsuqFCRpN4b4=
github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.0.0-20240314211421-34bd73a6cac8/go.mod h1:9L23Mib5WjvuWRMmLyZrH+OJyeDz0fEZ9e2ummzZlgU=
github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsemfexporter v0.0.0-20240314211421-34bd73a6cac8 h1:sPJEbFUsPR1+eNqELsjqZEf28lV3KsRV3GyRUnxwoGM=
Expand Down Expand Up @@ -1207,6 +1205,8 @@ go.opentelemetry.io/collector/config/configcompression v0.89.0 h1:Z4LG045HwoNqXa
go.opentelemetry.io/collector/config/configcompression v0.89.0/go.mod h1:LaavoxZsro5lL7qh1g9DMifG0qixWPEecW18Qr8bpag=
go.opentelemetry.io/collector/config/configgrpc v0.89.0 h1:Lnv/4EbImss5JuVE2nffk00pCGfk4tRyV8Gm3Gl7yuM=
go.opentelemetry.io/collector/config/configgrpc v0.89.0/go.mod h1:2GuAxpU34a1X19kCZ8Kw3FUsxOIiFcWHiYorRCyhnCc=
go.opentelemetry.io/collector/config/confighttp v0.89.0 h1:RatLdeZkCu3uLtCjbS8g5Aec2JB3/CSpB6O7P081Bhg=
go.opentelemetry.io/collector/config/confighttp v0.89.0/go.mod h1:R5BIbvqlxSDQGpCRWd2HBZIWijfSIWRpLeSpZjkKkag=
go.opentelemetry.io/collector/config/confignet v0.89.0 h1:Ekh+XhXelnnJ9as8IWhdUOfjnDPhz12XMJQ8sWNAbGw=
go.opentelemetry.io/collector/config/confignet v0.89.0/go.mod h1:cpO8JYWGONaViOygKVw+Hd2UoBcn2cUiyi0WWeFTwJY=
go.opentelemetry.io/collector/config/configopaque v0.89.0 h1:Ad6yGcGBHs+J9SNjkedY68JsLZ1vBn4kKzdqKuTCRsE=
Expand Down
50 changes: 42 additions & 8 deletions internal/mapstructure/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (

var (
errNonStringEncodedKey = errors.New("non string-encoded key")
errUnsupportedKind = errors.New("unsupported kind")
)

// tagInfo stores the mapstructure tag details.
Expand All @@ -44,8 +45,10 @@ type EncoderConfig struct {
// EncodeHook, if set, is a way to provide custom encoding. It
// will be called before structs and primitive types.
EncodeHook mapstructure.DecodeHookFunc
// OmitAllNil, if set, is a way to omit all nil fields even if omitempty isn't present.
OmitAllNil bool
// NilEmptyMap, if set, is a way to nil out empty maps.
NilEmptyMap bool
// OmitNilFields, if set, is a way to omit all nil struct fields even if omitempty isn't present.
OmitNilFields bool
}

// New returns a new encoder for the configuration.
Expand All @@ -63,11 +66,11 @@ func (e *Encoder) Encode(input any) (any, error) {
func (e *Encoder) encode(value reflect.Value) (any, error) {
if value.IsValid() {
switch value.Kind() {
case reflect.Interface, reflect.Ptr:
case reflect.Interface, reflect.Pointer:
return e.encode(value.Elem())
case reflect.Map:
return e.encodeMap(value)
case reflect.Slice:
case reflect.Slice, reflect.Array:
return e.encodeSlice(value)
case reflect.Struct:
return e.encodeStruct(value)
Expand Down Expand Up @@ -120,9 +123,12 @@ func (e *Encoder) encodeStruct(value reflect.Value) (any, error) {
}
encoded, err := e.encode(field)
if err != nil {
if errors.Is(err, errUnsupportedKind) {
continue
}
return nil, fmt.Errorf("error encoding field %q: %w", info.name, err)
}
if e.config.OmitAllNil && encoded == nil {
if e.config.OmitNilFields && encoded == nil {
continue
}
if info.squash {
Expand All @@ -141,7 +147,7 @@ func (e *Encoder) encodeStruct(value reflect.Value) (any, error) {

// encodeSlice iterates over the slice and encodes each of the elements.
func (e *Encoder) encodeSlice(value reflect.Value) (any, error) {
if value.Kind() != reflect.Slice {
if value.Kind() != reflect.Slice && value.Kind() != reflect.Array {
return nil, &reflect.ValueError{
Method: "encodeSlice",
Kind: value.Kind(),
Expand Down Expand Up @@ -184,6 +190,9 @@ func (e *Encoder) encodeMap(value reflect.Value) (any, error) {
return nil, fmt.Errorf("error encoding map value for key %q: %w", key, err)
}
}
if e.config.NilEmptyMap && len(result) == 0 {
return nil, nil
}
return result, nil
}

Expand Down Expand Up @@ -233,14 +242,39 @@ func TextMarshalerHookFunc() mapstructure.DecodeHookFuncValue {
// NilHookFunc returns a DecodeHookFuncValue that checks if the value matches the type and nils it out. Allows specific
// types to be omitted.
func NilHookFunc[T any]() mapstructure.DecodeHookFuncValue {
return nilHookFunc[T](false)
}

// NilZeroValueHookFunc returns a DecodeHookFuncValue that only nils the field if it's a zero value.
func NilZeroValueHookFunc[T any]() mapstructure.DecodeHookFuncValue {
return nilHookFunc[T](true)
}

func nilHookFunc[T any](onlyIfZero bool) mapstructure.DecodeHookFuncValue {
return func(from reflect.Value, _ reflect.Value) (any, error) {
if !from.IsValid() {
return nil, nil
}
_, ok := from.Interface().(T)
if !ok {
if ok && (!onlyIfZero || from.IsZero()) {
return nil, nil
}
return from.Interface(), nil
}
}

// UnsupportedKindHookFunc returns a DecodeHookFuncValue that checks that the kind isn't one unsupported by the YAML
// encoder.
func UnsupportedKindHookFunc() mapstructure.DecodeHookFuncValue {
return func(from reflect.Value, _ reflect.Value) (any, error) {
if !from.IsValid() {
return nil, nil
}
switch from.Kind() {
case reflect.Chan, reflect.Func:
return nil, fmt.Errorf("%w: %s", errUnsupportedKind, from.Kind())
default:
return from.Interface(), nil
}
return nil, nil
}
}
Loading

0 comments on commit 0865173

Please sign in to comment.