Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promtail: Add custom headers on push requests (#7470) #7472

Merged
merged 2 commits into from
Feb 3, 2023
Merged

Promtail: Add custom headers on push requests (#7470) #7472

merged 2 commits into from
Feb 3, 2023

Conversation

Jack-Kingdom
Copy link
Contributor

What this PR does / why we need it:
add custom headers support on promtail send reqeust

Which issue(s) this PR fixes:
Fixes #7470

Checklist

  • Reviewed the CONTRIBUTING.md guide
  • Documentation added
  • Tests updated
  • CHANGELOG.md updated
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/upgrading/_index.md

@Jack-Kingdom Jack-Kingdom requested a review from a team as a code owner October 19, 2022 16:10
@CLAassistant
Copy link

CLAassistant commented Oct 19, 2022

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories label Oct 19, 2022
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
- querier/queryrange	-0.1%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

Copy link
Contributor

@chaudum chaudum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Jack-Kingdom thanks for your PR. I proposed some minor changes.

CHANGELOG.md Outdated Show resolved Hide resolved
docs/sources/clients/promtail/configuration.md Outdated Show resolved Hide resolved
clients/pkg/promtail/config/config_test.go Outdated Show resolved Hide resolved
@Jack-Kingdom Jack-Kingdom requested a review from chaudum October 20, 2022 15:41
@Jack-Kingdom Jack-Kingdom changed the title Promtail: Add custom headers on send reqeust (#7470) Promtail: Add custom headers on push requests (#7470) Oct 20, 2022
@liguozhong
Copy link
Contributor

liguozhong commented Oct 21, 2022

clients:
  - name: custom-headers
    url: ../api/v1/push
    tenant_id: tenant1
    custom_headers:
       X-Scope-OrgID: tenant2

What will happen to custom_header if it has X-Scope-OrgID header, tenant1 or tenant2?
should we avoid user error and report error before promtail startup.for example

// UnmarshalYAML implement Yaml Unmarshaler
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
	...
	if err := unmarshal(&cfg); err != nil {
		return err
	}

	//Validate config
	if c.TenantID!=""&&len(c.CustomHeaders)>0 {
		_,ok:=c.CustomHeaders["X-Scope-OrgID"]
		if ok {
			return errors.New("illegal promtail header X-Scope-OrgID")
		}
	}

	*c = Config(cfg)
	return nil
}

@chaudum
Copy link
Contributor

chaudum commented Oct 21, 2022

What will happen to custom_header if it has X-Scope-OrgID header, tenant1 or tenant2? should we avoid user error and report error before promtail startup.for example

In the current implementation X-Scope-OrgID in custom_headers would replace the header value set by tenant_id. However, it should be the other way round. tenant_id should have higher priority. @Jack-Kingdom There should be an explicit test for this case as well.

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

-           ingester	-0.1%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@Jack-Kingdom
Copy link
Contributor Author

IMO it's inelegant to check config race condition. I update those logical, so that makt it won't override existing headers.

	// Add custom headers on request
	if len(c.cfg.CustomHeaders) > 0 {
		for k, v := range c.cfg.CustomHeaders {
			if req.Header.Get(k) == "" {
				req.Header.Add(k, v)
			}else {
				level.Warn(c.logger).Log("msg", "custom header key already exists, skipping", "key", k)
			}
		}
	}

doc mentioned this
Screen Shot 2022-10-23 at 2 09 46 AM

@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@liguozhong
Copy link
Contributor

liguozhong commented Oct 24, 2022

clients:
  - name: custom-headers
    custom_headers:
       X-Scope-OrgID: tenant2
clients:
  - name: headers
    headers:
       X-Scope-OrgID: tenant2

What do you think, if we replace custom_headers with headers, custom_headers is too long for me and it's easy for me to make mistakes.

https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
prometheus doc

# Custom HTTP headers to be sent along with each remote write request.
# Be aware that headers that are set by Prometheus itself can't be overwritten.
headers:
  [ key: value ... ]

@liguozhong
Copy link
Contributor

In addition, this header feature is great, we have some gateway systems for security, need to have some authentication header.

@salvacorts salvacorts added size/L and removed size/M labels Oct 27, 2022
Copy link
Contributor

@MichelHollands MichelHollands left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jack-Kingdom Thanks for this contribution. This is looking good. There is an open question on renaming this to headers. This would make it consistent with Loki. Can you have a look at that?

docs/sources/clients/promtail/configuration.md Outdated Show resolved Hide resolved
@chaudum
Copy link
Contributor

chaudum commented Nov 29, 2022

@Jack-Kingdom Do you want to address the remaining issue (renaming to headers)? Otherwise I will change it so we can merge the feature into the main branch.

@pull-request-size pull-request-size bot added size/M and removed size/L labels Dec 3, 2022
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@Jack-Kingdom Jack-Kingdom requested review from MichelHollands and chaudum and removed request for chaudum and MichelHollands December 3, 2022 08:41
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

1 similar comment
@grafanabot
Copy link
Collaborator

./tools/diff_coverage.sh ../loki-main/test_results.txt test_results.txt ingester,distributor,querier,querier/queryrange,iter,storage,chunkenc,logql,loki

Change in test coverage per package. Green indicates 0 or positive change, red indicates that test coverage for a package fell.

+           ingester	0%
+        distributor	0%
+            querier	0%
+ querier/queryrange	0%
+               iter	0%
+            storage	0%
+           chunkenc	0%
+              logql	0%
+               loki	0%

@Jack-Kingdom
Copy link
Contributor Author

@chaudum Hi,I have rename custom_header to header in client config. Please hava a review of this commit.

@Jack-Kingdom Jack-Kingdom requested review from chaudum and MichelHollands and removed request for MichelHollands and chaudum December 4, 2022 20:30
Copy link
Contributor

@chaudum chaudum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for changes. I have two small wording suggestions for documentation.

docs/sources/clients/promtail/configuration.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
clients/pkg/promtail/client/client.go Outdated Show resolved Hide resolved
@Jack-Kingdom
Copy link
Contributor Author

@chaudum I made some changes to those codes and combine them into a single commit. Please take a review.

@jeschkies
Copy link
Contributor

@Jack-Kingdom could you merge main and see that the build gets green. I can merge it then 😊

@jeschkies jeschkies requested a review from JStickler as a code owner January 31, 2023 16:09
Copy link
Contributor

@JStickler JStickler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Docs squad] LGTM

@jeschkies jeschkies merged commit d434e80 into grafana:main Feb 3, 2023
@Jack-Kingdom Jack-Kingdom deleted the custom-headers branch February 6, 2023 19:51
@hatrena
Copy link

hatrena commented Feb 27, 2023

Hi, Is this already released?
I am using grafana/promtail:2.7.4 and getting this error:
field headers not found in type client.raw

@DylanGuedes
Copy link
Contributor

Hi, Is this already released? I am using grafana/promtail:2.7.4 and getting this error: field headers not found in type client.raw

This wasn't added to v2.7.4 because it is a new feature instead of a fix/patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/M type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<feature request> Promtail support custom headers when it send logs to loki