From 52edf5cc386ff4871ccbd0345679fcd91157888d Mon Sep 17 00:00:00 2001 From: dongdong Date: Tue, 26 Mar 2024 14:49:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20collector=20OT=20receiver=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=20tenantKey=20--story=3D116702353=20(#266)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/collector/VERSION | 2 +- pkg/collector/receiver/otlp/http.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/collector/VERSION b/pkg/collector/VERSION index b423e3bbd..fee31c9f9 100644 --- a/pkg/collector/VERSION +++ b/pkg/collector/VERSION @@ -1 +1 @@ -0.49.x \ No newline at end of file +0.50.x \ No newline at end of file diff --git a/pkg/collector/receiver/otlp/http.go b/pkg/collector/receiver/otlp/http.go index 70fa50227..daa889864 100644 --- a/pkg/collector/receiver/otlp/http.go +++ b/pkg/collector/receiver/otlp/http.go @@ -34,7 +34,9 @@ import ( ) const ( - tokenKey = "X-BK-TOKEN" + tokenKey = "X-BK-TOKEN" + tenantIDKey = "X-Tps-TenantID" + routeV1Traces = "/v1/traces" routeV1Trace = "/v1/trace" routeV1Metrics = "/v1/metrics" @@ -102,6 +104,16 @@ func writeError(w http.ResponseWriter, rh receiver.ResponseHandler, err error, s receiver.WriteResponse(w, rh.ContentType(), statusCode, msg) } +// 允许从 HTTP Header 中读取 token +// 优先读取 TokenKey 再尝试读取 TenantKey +func extractTokenFromHeader(header http.Header) string { + token := header.Get(tokenKey) + if len(token) > 0 { + return token + } + return header.Get(tenantIDKey) +} + func (s HttpService) httpExport(w http.ResponseWriter, req *http.Request, rtype define.RecordType) { defer utils.HandleCrash() ip := utils.ParseRequestIP(req.RemoteAddr) @@ -135,8 +147,7 @@ func (s HttpService) httpExport(w http.ResponseWriter, req *http.Request, rtype Data: data, } - // 允许从 HTTP Header 中读取 token - tk := req.Header.Get(tokenKey) + tk := extractTokenFromHeader(req.Header) if len(tk) > 0 { r.Token = define.Token{Original: tk} }