-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathconfig.go
More file actions
103 lines (82 loc) · 3.4 KB
/
config.go
File metadata and controls
103 lines (82 loc) · 3.4 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver"
import (
"errors"
"fmt"
"net/url"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/scraper/scraperhelper"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver/internal/metadata"
)
// Predefined error responses for configuration validation failures
var (
errInvalidEndpoint = errors.New(`"endpoint" must be in the form of <scheme>://<hostname>[:<port>]`)
errMissingEndpoint = errors.New("at least one of 'endpoint' or 'endpoints' must be specified")
)
// Config defines the configuration for the various elements of the receiver agent.
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
metadata.MetricsBuilderConfig `mapstructure:",squash"`
Targets []*targetConfig `mapstructure:"targets"`
// prevent unkeyed literal initialization
_ struct{}
}
// validationConfig defines configuration for response validation
type validationConfig struct {
// String matching
Contains string `mapstructure:"contains"`
NotContains string `mapstructure:"not_contains"`
// JSON path validation
JSONPath string `mapstructure:"json_path"`
Equals string `mapstructure:"equals"`
// Size validation
MaxSize *int64 `mapstructure:"max_size"`
MinSize *int64 `mapstructure:"min_size"`
// Regex validation
Regex string `mapstructure:"regex"`
}
// targetConfig defines configuration for individual HTTP checks.
type targetConfig struct {
confighttp.ClientConfig `mapstructure:",squash"`
Method string `mapstructure:"method"`
Endpoints []string `mapstructure:"endpoints"` // Field for a list of endpoints
Body string `mapstructure:"body"` // Request body content
AutoContentType bool `mapstructure:"auto_content_type"` // Whether to automatically set Content-Type based on body
Validations []validationConfig `mapstructure:"validations"` // Response validation rules
}
// Validate validates an individual targetConfig.
func (cfg *targetConfig) Validate() error {
var err error
// Ensure at least one of 'endpoint' or 'endpoints' is specified.
if cfg.Endpoint == "" && len(cfg.Endpoints) == 0 {
err = multierr.Append(err, errMissingEndpoint)
}
// Validate the single endpoint in ClientConfig.
if cfg.Endpoint != "" {
if _, parseErr := url.ParseRequestURI(cfg.Endpoint); parseErr != nil {
err = multierr.Append(err, fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr))
}
}
// Validate each endpoint in the Endpoints list.
for _, endpoint := range cfg.Endpoints {
if _, parseErr := url.ParseRequestURI(endpoint); parseErr != nil {
err = multierr.Append(err, fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr))
}
}
return err
}
// Validate validates the top-level Config by checking each targetConfig.
func (cfg *Config) Validate() error {
var err error
// Ensure at least one target is configured.
if len(cfg.Targets) == 0 {
err = multierr.Append(err, errors.New("no targets configured"))
}
// Validate each targetConfig.
for _, target := range cfg.Targets {
err = multierr.Append(err, target.Validate())
}
return err
}