Skip to content

Commit

Permalink
[receiver/simpleprometheus] Support customizing job name (#34408)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>

Resolve
#31502


**Link to tracking Issue:** 31502
  • Loading branch information
chenlujjj authored Oct 30, 2024
1 parent 1773b66 commit c860d1d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/simple_prometheus_job_name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: simpleprometheusreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support to set `job_name` in config

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [31502]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
2 changes: 1 addition & 1 deletion receiver/simpleprometheusreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The following settings are optional:
- `collection_interval` (default = `10s`): The internal at which metrics should
be emitted by this receiver.
- `metrics_path` (default = `/metrics`): The path to the metrics endpoint.
- `job_name` (default = `prometheus_simple/${endpoint}`): Prometheus scrape job name.
- `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.
- `use_service_account` (default = `false`): Whether or not to use the
Kubernetes Pod service account for authentication.
Expand Down Expand Up @@ -76,4 +77,3 @@ Example:
The full list of settings exposed for this receiver are documented [here](./config.go)
with detailed sample configurations [here](./testdata/config.yaml).
2 changes: 2 additions & 0 deletions receiver/simpleprometheusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Config struct {
Labels map[string]string `mapstructure:"labels,omitempty"`
// Whether or not to use pod service account to authenticate.
UseServiceAccount bool `mapstructure:"use_service_account"`
// JobName allows users to customize the job name optionally.
JobName string `mapstructure:"job_name"`
}

// TODO: Move to a common package for use by other receivers and also pull
Expand Down
1 change: 1 addition & 0 deletions receiver/simpleprometheusreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestLoadConfig(t *testing.T) {
},
CollectionInterval: 30 * time.Second,
MetricsPath: "/v2/metrics",
JobName: "job123",
Params: url.Values{"columns": []string{"name", "messages"}, "key": []string{"foo", "bar"}},
UseServiceAccount: true,
},
Expand Down
6 changes: 5 additions & 1 deletion receiver/simpleprometheusreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ func getPrometheusConfig(cfg *Config) (*prometheusreceiver.Config, error) {
}
labels[model.AddressLabel] = model.LabelValue(cfg.Endpoint)

jobName := cfg.JobName
if jobName == "" {
jobName = fmt.Sprintf("%s/%s", metadata.Type, cfg.Endpoint)
}
scrapeConfig := &config.ScrapeConfig{
ScrapeInterval: model.Duration(cfg.CollectionInterval),
ScrapeTimeout: model.Duration(cfg.CollectionInterval),
JobName: fmt.Sprintf("%s/%s", metadata.Type, cfg.Endpoint),
JobName: jobName,
HonorTimestamps: true,
Scheme: scheme,
MetricsPath: cfg.MetricsPath,
Expand Down
35 changes: 35 additions & 0 deletions receiver/simpleprometheusreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,41 @@ func TestGetPrometheusConfig(t *testing.T) {
},
},
},
{
name: "Test with job name",
config: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "localhost:1234",
},
CollectionInterval: 10 * time.Second,
MetricsPath: "/metric",
JobName: "job123",
},
want: &prometheusreceiver.Config{
PrometheusConfig: &prometheusreceiver.PromConfig{
GlobalConfig: config.DefaultGlobalConfig,
ScrapeConfigs: []*config.ScrapeConfig{
{
ScrapeInterval: model.Duration(10 * time.Second),
ScrapeTimeout: model.Duration(10 * time.Second),
JobName: "job123",
HonorTimestamps: true,
Scheme: "https",
MetricsPath: "/metric",
ServiceDiscoveryConfigs: discovery.Configs{
&discovery.StaticConfig{
{
Targets: []model.LabelSet{
{model.AddressLabel: model.LabelValue("localhost:1234")},
},
},
},
},
},
},
},
},
},
{
name: "Test with TLS",
config: &Config{
Expand Down
1 change: 1 addition & 0 deletions receiver/simpleprometheusreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ prometheus_simple/all_settings:
endpoint: "localhost:1234"
collection_interval: 30s
metrics_path: /v2/metrics
job_name: "job123"
params:
columns: "name,messages"
key: [ "foo","bar" ]
Expand Down

0 comments on commit c860d1d

Please sign in to comment.