-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
config.go
80 lines (69 loc) · 3.03 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package postgresqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
import (
"errors"
"fmt"
"net"
"time"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver/internal/metadata"
)
// Errors for missing required config parameters.
const (
ErrNoUsername = "invalid config: missing username"
ErrNoPassword = "invalid config: missing password" // #nosec G101 - not hardcoded credentials
ErrNotSupported = "invalid config: field '%s' not supported"
ErrTransportsSupported = "invalid config: 'transport' must be 'tcp' or 'unix'"
ErrHostPort = "invalid config: 'endpoint' must be in the form <host>:<port> no matter what 'transport' is configured"
)
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
Username string `mapstructure:"username"`
Password configopaque.String `mapstructure:"password"`
Databases []string `mapstructure:"databases"`
ExcludeDatabases []string `mapstructure:"exclude_databases"`
confignet.AddrConfig `mapstructure:",squash"` // provides Endpoint and Transport
configtls.ClientConfig `mapstructure:"tls,omitempty"` // provides SSL details
ConnectionPool `mapstructure:"connection_pool,omitempty"`
metadata.MetricsBuilderConfig `mapstructure:",squash"`
}
type ConnectionPool struct {
MaxIdleTime *time.Duration `mapstructure:"max_idle_time,omitempty"`
MaxLifetime *time.Duration `mapstructure:"max_lifetime,omitempty"`
MaxIdle *int `mapstructure:"max_idle,omitempty"`
MaxOpen *int `mapstructure:"max_open,omitempty"`
}
func (cfg *Config) Validate() error {
var err error
if cfg.Username == "" {
err = multierr.Append(err, errors.New(ErrNoUsername))
}
if cfg.Password == "" {
err = multierr.Append(err, errors.New(ErrNoPassword))
}
// The lib/pq module does not support overriding ServerName or specifying supported TLS versions
if cfg.ServerName != "" {
err = multierr.Append(err, fmt.Errorf(ErrNotSupported, "ServerName"))
}
if cfg.MaxVersion != "" {
err = multierr.Append(err, fmt.Errorf(ErrNotSupported, "MaxVersion"))
}
if cfg.MinVersion != "" {
err = multierr.Append(err, fmt.Errorf(ErrNotSupported, "MinVersion"))
}
switch cfg.Transport {
case confignet.TransportTypeTCP, confignet.TransportTypeUnix:
_, _, endpointErr := net.SplitHostPort(cfg.Endpoint)
if endpointErr != nil {
err = multierr.Append(err, errors.New(ErrHostPort))
}
default:
err = multierr.Append(err, errors.New(ErrTransportsSupported))
}
return err
}