config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package docker // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker"
  4. import (
  5. "errors"
  6. "fmt"
  7. "time"
  8. )
  9. type Config struct {
  10. // The URL of the docker server. Default is "unix:///var/run/docker.sock"
  11. Endpoint string `mapstructure:"endpoint"`
  12. // The maximum amount of time to wait for docker API responses. Default is 5s
  13. Timeout time.Duration `mapstructure:"timeout"`
  14. // A list of filters whose matching images are to be excluded. Supports literals, globs, and regex.
  15. ExcludedImages []string `mapstructure:"excluded_images"`
  16. // Docker client API version.
  17. DockerAPIVersion float64 `mapstructure:"api_version"`
  18. }
  19. // NewConfig creates a new config to be used when creating
  20. // a docker client
  21. func NewConfig(endpoint string, timeout time.Duration, excludedImages []string, apiVersion float64) (*Config, error) {
  22. cfg := &Config{
  23. Endpoint: endpoint,
  24. Timeout: timeout,
  25. ExcludedImages: excludedImages,
  26. DockerAPIVersion: apiVersion,
  27. }
  28. err := cfg.validate()
  29. return cfg, err
  30. }
  31. // NewDefaultConfig creates a new config with default values
  32. // to be used when creating a docker client
  33. func NewDefaultConfig() *Config {
  34. cfg := &Config{
  35. Endpoint: "unix:///var/run/docker.sock",
  36. Timeout: 5 * time.Second,
  37. DockerAPIVersion: minimalRequiredDockerAPIVersion,
  38. }
  39. return cfg
  40. }
  41. // validate asserts that an endpoint field is set
  42. // on the config struct
  43. func (config Config) validate() error {
  44. if config.Endpoint == "" {
  45. return errors.New("config.Endpoint must be specified")
  46. }
  47. if config.DockerAPIVersion < minimalRequiredDockerAPIVersion {
  48. return fmt.Errorf("Docker API version must be at least %v", minimalRequiredDockerAPIVersion)
  49. }
  50. return nil
  51. }