Skip to content

Commit d7cd6e1

Browse files
committed
[receiver/simpleprometheus] Support customizing job name
1 parent 3f1c619 commit d7cd6e1

File tree

7 files changed

+72
-2
lines changed

7 files changed

+72
-2
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: simpleprometheusreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Support to set `job_name` in config
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: [31502]
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: [user]

receiver/simpleprometheusreceiver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following settings are optional:
2929
- `collection_interval` (default = `10s`): The internal at which metrics should
3030
be emitted by this receiver.
3131
- `metrics_path` (default = `/metrics`): The path to the metrics endpoint.
32+
- `job_name` (default = `prometheus_simple/${endpoint}`): Prometheus scrape job name.
3233
- `params` (default = `{}`): The query parameters to pass to the metrics endpoint. If specified, params are appended to `metrics_path` to form the URL with which the target is scraped.
3334
- `use_service_account` (default = `false`): Whether or not to use the
3435
Kubernetes Pod service account for authentication.
@@ -76,4 +77,3 @@ Example:
7677
7778
The full list of settings exposed for this receiver are documented [here](./config.go)
7879
with detailed sample configurations [here](./testdata/config.yaml).
79-

receiver/simpleprometheusreceiver/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Config struct {
2525
Labels map[string]string `mapstructure:"labels,omitempty"`
2626
// Whether or not to use pod service account to authenticate.
2727
UseServiceAccount bool `mapstructure:"use_service_account"`
28+
// JobName allows users to customize the job name optionally.
29+
JobName string `mapstructure:"job_name"`
2830
}
2931

3032
// TODO: Move to a common package for use by other receivers and also pull

receiver/simpleprometheusreceiver/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestLoadConfig(t *testing.T) {
4949
},
5050
CollectionInterval: 30 * time.Second,
5151
MetricsPath: "/v2/metrics",
52+
JobName: "job123",
5253
Params: url.Values{"columns": []string{"name", "messages"}, "key": []string{"foo", "bar"}},
5354
UseServiceAccount: true,
5455
},

receiver/simpleprometheusreceiver/receiver.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,14 @@ func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) {
109109
}
110110
labels[model.AddressLabel] = model.LabelValue(cfg.Endpoint)
111111

112+
jobName := cfg.JobName
113+
if jobName == "" {
114+
jobName = fmt.Sprintf("%s/%s", metadata.Type, cfg.Endpoint)
115+
}
112116
scrapeConfig := &config.ScrapeConfig{
113117
ScrapeInterval: model.Duration(cfg.CollectionInterval),
114118
ScrapeTimeout: model.Duration(cfg.CollectionInterval),
115-
JobName: fmt.Sprintf("%s/%s", metadata.Type, cfg.Endpoint),
119+
JobName: jobName,
116120
HonorTimestamps: true,
117121
Scheme: scheme,
118122
MetricsPath: cfg.MetricsPath,

receiver/simpleprometheusreceiver/receiver_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,41 @@ func TestGetPrometheusConfig(t *testing.T) {
111111
},
112112
},
113113
},
114+
{
115+
name: "Test with job name",
116+
config: &Config{
117+
ClientConfig: confighttp.ClientConfig{
118+
Endpoint: "localhost:1234",
119+
},
120+
CollectionInterval: 10 * time.Second,
121+
MetricsPath: "/metric",
122+
JobName: "job123",
123+
},
124+
want: &prometheusreceiver.Config{
125+
PrometheusConfig: &prometheusreceiver.PromConfig{
126+
GlobalConfig: config.DefaultGlobalConfig,
127+
ScrapeConfigs: []*config.ScrapeConfig{
128+
{
129+
ScrapeInterval: model.Duration(10 * time.Second),
130+
ScrapeTimeout: model.Duration(10 * time.Second),
131+
JobName: "job123",
132+
HonorTimestamps: true,
133+
Scheme: "https",
134+
MetricsPath: "/metric",
135+
ServiceDiscoveryConfigs: discovery.Configs{
136+
&discovery.StaticConfig{
137+
{
138+
Targets: []model.LabelSet{
139+
{model.AddressLabel: model.LabelValue("localhost:1234")},
140+
},
141+
},
142+
},
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
114149
{
115150
name: "Test with TLS",
116151
config: &Config{

receiver/simpleprometheusreceiver/testdata/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ prometheus_simple/all_settings:
33
endpoint: "localhost:1234"
44
collection_interval: 30s
55
metrics_path: /v2/metrics
6+
job_name: "job123"
67
params:
78
columns: "name,messages"
89
key: [ "foo","bar" ]

0 commit comments

Comments
 (0)