Skip to content

Commit 80182f7

Browse files
dashpoleokankoAMZ
authored andcommitted
Split target allocator into an internal package (open-telemetry#33223)
Fixes open-telemetry#33146
1 parent 69bd759 commit 80182f7

File tree

11 files changed

+773
-322
lines changed

11 files changed

+773
-322
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: prometheusreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Move the TargetAllocator configuration struct to an internal directory
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33146]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

receiver/prometheusreceiver/config.go

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ package prometheusreceiver // import "github.com/open-telemetry/opentelemetry-co
66
import (
77
"errors"
88
"fmt"
9-
"net/url"
109
"os"
1110
"sort"
1211
"strings"
13-
"time"
1412

1513
commonconfig "github.com/prometheus/common/config"
1614
promconfig "github.com/prometheus/prometheus/config"
17-
promHTTP "github.com/prometheus/prometheus/discovery/http"
1815
"github.com/prometheus/prometheus/discovery/kubernetes"
19-
"go.opentelemetry.io/collector/config/confighttp"
2016
"go.opentelemetry.io/collector/confmap"
2117
"gopkg.in/yaml.v2"
18+
19+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/targetallocator"
2220
)
2321

2422
// Config defines configuration for Prometheus receiver.
@@ -37,7 +35,7 @@ type Config struct {
3735
// ReportExtraScrapeMetrics - enables reporting of additional metrics for Prometheus client like scrape_body_size_bytes
3836
ReportExtraScrapeMetrics bool `mapstructure:"report_extra_scrape_metrics"`
3937

40-
TargetAllocator *TargetAllocator `mapstructure:"target_allocator"`
38+
TargetAllocator *targetallocator.Config `mapstructure:"target_allocator"`
4139
}
4240

4341
// Validate checks the receiver configuration is valid.
@@ -48,27 +46,6 @@ func (cfg *Config) Validate() error {
4846
return nil
4947
}
5048

51-
type TargetAllocator struct {
52-
confighttp.ClientConfig `mapstructure:",squash"`
53-
Interval time.Duration `mapstructure:"interval"`
54-
CollectorID string `mapstructure:"collector_id"`
55-
HTTPSDConfig *PromHTTPSDConfig `mapstructure:"http_sd_config"`
56-
HTTPScrapeConfig *PromHTTPClientConfig `mapstructure:"http_scrape_config"`
57-
}
58-
59-
func (cfg *TargetAllocator) Validate() error {
60-
// ensure valid endpoint
61-
if _, err := url.ParseRequestURI(cfg.Endpoint); err != nil {
62-
return fmt.Errorf("TargetAllocator endpoint is not valid: %s", cfg.Endpoint)
63-
}
64-
// ensure valid collectorID without variables
65-
if cfg.CollectorID == "" || strings.Contains(cfg.CollectorID, "${") {
66-
return fmt.Errorf("CollectorID is not a valid ID")
67-
}
68-
69-
return nil
70-
}
71-
7249
// PromConfig is a redeclaration of promconfig.Config because we need custom unmarshaling
7350
// as prometheus "config" uses `yaml` tags.
7451
type PromConfig promconfig.Config
@@ -126,43 +103,6 @@ func (cfg *PromConfig) Validate() error {
126103
return nil
127104
}
128105

129-
// PromHTTPSDConfig is a redeclaration of promHTTP.SDConfig because we need custom unmarshaling
130-
// as prometheus "config" uses `yaml` tags.
131-
type PromHTTPSDConfig promHTTP.SDConfig
132-
133-
var _ confmap.Unmarshaler = (*PromHTTPSDConfig)(nil)
134-
135-
func (cfg *PromHTTPSDConfig) Unmarshal(componentParser *confmap.Conf) error {
136-
cfgMap := componentParser.ToStringMap()
137-
if len(cfgMap) == 0 {
138-
return nil
139-
}
140-
cfgMap["url"] = "http://placeholder" // we have to set it as else marshaling will fail
141-
return unmarshalYAML(cfgMap, (*promHTTP.SDConfig)(cfg))
142-
}
143-
144-
type PromHTTPClientConfig commonconfig.HTTPClientConfig
145-
146-
var _ confmap.Unmarshaler = (*PromHTTPClientConfig)(nil)
147-
148-
func (cfg *PromHTTPClientConfig) Unmarshal(componentParser *confmap.Conf) error {
149-
cfgMap := componentParser.ToStringMap()
150-
if len(cfgMap) == 0 {
151-
return nil
152-
}
153-
return unmarshalYAML(cfgMap, (*commonconfig.HTTPClientConfig)(cfg))
154-
}
155-
156-
func (cfg *PromHTTPClientConfig) Validate() error {
157-
httpCfg := (*commonconfig.HTTPClientConfig)(cfg)
158-
if err := validateHTTPClientConfig(httpCfg); err != nil {
159-
return err
160-
}
161-
// Prometheus UnmarshalYaml implementation by default calls Validate,
162-
// but it is safer to do it here as well.
163-
return httpCfg.Validate()
164-
}
165-
166106
func unmarshalYAML(in map[string]any, out any) error {
167107
yamlOut, err := yaml.Marshal(in)
168108
if err != nil {

receiver/prometheusreceiver/config_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -341,34 +341,3 @@ func TestFileSDConfigWithoutSDFile(t *testing.T) {
341341

342342
require.NoError(t, component.ValidateConfig(cfg))
343343
}
344-
345-
func TestPromHTTPClientConfigValidateAuthorization(t *testing.T) {
346-
cfg := PromHTTPClientConfig{}
347-
require.NoError(t, component.ValidateConfig(cfg))
348-
cfg.Authorization = &promConfig.Authorization{}
349-
require.NoError(t, component.ValidateConfig(cfg))
350-
cfg.Authorization.CredentialsFile = "none"
351-
require.Error(t, component.ValidateConfig(cfg))
352-
cfg.Authorization.CredentialsFile = filepath.Join("testdata", "dummy-tls-cert-file")
353-
require.NoError(t, component.ValidateConfig(cfg))
354-
}
355-
356-
func TestPromHTTPClientConfigValidateTLSConfig(t *testing.T) {
357-
cfg := PromHTTPClientConfig{}
358-
require.NoError(t, component.ValidateConfig(cfg))
359-
cfg.TLSConfig.CertFile = "none"
360-
require.Error(t, component.ValidateConfig(cfg))
361-
cfg.TLSConfig.CertFile = filepath.Join("testdata", "dummy-tls-cert-file")
362-
cfg.TLSConfig.KeyFile = "none"
363-
require.Error(t, component.ValidateConfig(cfg))
364-
cfg.TLSConfig.KeyFile = filepath.Join("testdata", "dummy-tls-key-file")
365-
require.NoError(t, component.ValidateConfig(cfg))
366-
}
367-
368-
func TestPromHTTPClientConfigValidateMain(t *testing.T) {
369-
cfg := PromHTTPClientConfig{}
370-
require.NoError(t, component.ValidateConfig(cfg))
371-
cfg.BearerToken = "foo"
372-
cfg.BearerTokenFile = filepath.Join("testdata", "dummy-tls-key-file")
373-
require.Error(t, component.ValidateConfig(cfg))
374-
}

0 commit comments

Comments
 (0)