From 56204cc2f43acbe0cbaf6d1872dbf299e78b2974 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sat, 7 Dec 2024 23:35:27 +0100 Subject: [PATCH 1/5] [feature] add support for sending headers to tracing system Add support for sending additional HTTP or gRPC headers which can be used for authentication or other additional information for the tracing system without having to set up a local instance of the OpenTelemetry Collector to add these headers. Example with Dash0: ```yaml tracing-enabled: false tracing-transport: "grpc" tracing-endpoint: "ingress.eu-west-1.aws.dash0.com:4317" tracing-headers: "Authorization": "Bearer DASH0_AUTH_TOKEN" "Dash0-Dataset": "gotosocial" ``` Example with Honeycomb: ```yaml tracing-enabled: false tracing-transport: "grpc" tracing-endpoint: "api.honeycomb.io:443" tracing-headers: "x-honeycomb-team": "YOUR_API_KEY" "x-honeycomb-dataset": "YOUR_DATASET" ``` --- docs/advanced/tracing.md | 3 +++ docs/configuration/observability.md | 7 +++++++ docs/locales/zh/advanced/tracing.md | 3 +++ docs/locales/zh/configuration/observability.md | 5 +++++ example/config.yaml | 7 +++++++ internal/config/config.go | 9 +++++---- internal/config/defaults.go | 1 + internal/config/helpers.gen.go | 16 ++++++++++++++++ internal/tracing/tracing.go | 1 + 9 files changed, 48 insertions(+), 4 deletions(-) diff --git a/docs/advanced/tracing.md b/docs/advanced/tracing.md index c06b449c3f..801930d7c2 100644 --- a/docs/advanced/tracing.md +++ b/docs/advanced/tracing.md @@ -12,6 +12,9 @@ You'll need the files in [`example/tracing`][ext]. Once you have those you can r tracing-enabled: true tracing-transport: "grpc" tracing-endpoint: "localhost:4317" +tracing-headers: + "Authorization": "Bearer super-secret-token" + "Dataset": "gotosocial" tracing-insecure-transport: true ``` diff --git a/docs/configuration/observability.md b/docs/configuration/observability.md index 0fcf4710e9..1e3718fac4 100644 --- a/docs/configuration/observability.md +++ b/docs/configuration/observability.md @@ -35,6 +35,13 @@ tracing-transport: "grpc" # Default: "" tracing-endpoint: "" +# Map of strings. Additional headers to send to the trace ingester. +# Additional HTTP or gRPC headers can be used for authentication or other additional +# information for the tracing system. +# Examples: {"Authorization": "Bearer super-secret-token", "Dataset": "gotosocial"} +# Default: {} +tracing-headers: {} + # Bool. Disable TLS for the gRPC and HTTP transport protocols. # Default: false tracing-insecure-transport: false diff --git a/docs/locales/zh/advanced/tracing.md b/docs/locales/zh/advanced/tracing.md index dc502b9e42..50a5c3f23d 100644 --- a/docs/locales/zh/advanced/tracing.md +++ b/docs/locales/zh/advanced/tracing.md @@ -12,6 +12,9 @@ GoToSocial 内置了基于 [OpenTelemetry][otel] 的追踪功能。虽然并没 tracing-enabled: true tracing-transport: "grpc" tracing-endpoint: "localhost:4317" +tracing-headers: + "Authorization": "Bearer super-secret-token" + "Dataset": "gotosocial" tracing-insecure-transport: true ``` diff --git a/docs/locales/zh/configuration/observability.md b/docs/locales/zh/configuration/observability.md index c26b2b3510..743ee91021 100644 --- a/docs/locales/zh/configuration/observability.md +++ b/docs/locales/zh/configuration/observability.md @@ -31,6 +31,11 @@ tracing-transport: "grpc" # 默认值: "" tracing-endpoint: "" +# TODO +# 示例: {"Authorization": "Bearer super-secret-token", "Dataset": "gotosocial"} +# 默认值: {} +tracing-headers: {} + # 布尔值。禁用gRPC和HTTP传输协议的TLS。 # 默认值: false tracing-insecure-transport: false diff --git a/example/config.yaml b/example/config.yaml index 644b51575a..7a3742671a 100644 --- a/example/config.yaml +++ b/example/config.yaml @@ -928,6 +928,13 @@ tracing-transport: "grpc" # Default: "" tracing-endpoint: "" +# Map of strings. Additional headers to send to the trace ingester. +# Additional HTTP or gRPC headers can be used for authentication or other additional +# information for the tracing system. +# Examples: {"Authorization": "Bearer super-secret-token", "Dataset": "gotosocial"} +# Default: {} +tracing-headers: {} + # Bool. Disable TLS for the gRPC and HTTP transport protocols. # Default: false tracing-insecure-transport: false diff --git a/internal/config/config.go b/internal/config/config.go index 413743409a..ee950526a2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -140,10 +140,11 @@ type Configuration struct { OIDCAllowedGroups []string `name:"oidc-allowed-groups" usage:"Membership of one of the listed groups allows access to GtS. If this is empty, all groups are allowed."` OIDCAdminGroups []string `name:"oidc-admin-groups" usage:"Membership of one of the listed groups makes someone a GtS admin"` - TracingEnabled bool `name:"tracing-enabled" usage:"Enable OTLP Tracing"` - TracingTransport string `name:"tracing-transport" usage:"grpc or http"` - TracingEndpoint string `name:"tracing-endpoint" usage:"Endpoint of your trace collector. Eg., 'localhost:4317' for gRPC, 'localhost:4318' for http"` - TracingInsecureTransport bool `name:"tracing-insecure-transport" usage:"Disable TLS for the gRPC or HTTP transport protocol"` + TracingEnabled bool `name:"tracing-enabled" usage:"Enable OTLP Tracing"` + TracingTransport string `name:"tracing-transport" usage:"grpc or http"` + TracingEndpoint string `name:"tracing-endpoint" usage:"Endpoint of your trace collector. Eg., 'localhost:4317' for gRPC, 'localhost:4318' for http"` + TracingHeaders map[string]string `name:"tracing-headers" usage:"Headers for your trace collector. Eg., 'Authorization: Bearer super-secret-token' for sending an 'Authorization' header"` + TracingInsecureTransport bool `name:"tracing-insecure-transport" usage:"Disable TLS for the gRPC or HTTP transport protocol"` MetricsEnabled bool `name:"metrics-enabled" usage:"Enable OpenTelemetry based metrics support."` MetricsAuthEnabled bool `name:"metrics-auth-enabled" usage:"Enable HTTP Basic Authentication for Prometheus metrics endpoint"` diff --git a/internal/config/defaults.go b/internal/config/defaults.go index f77c5c4560..54ea3392b9 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -120,6 +120,7 @@ var Defaults = Configuration{ TracingEnabled: false, TracingTransport: "grpc", TracingEndpoint: "", + TracingHeaders: map[string]string{}, TracingInsecureTransport: false, MetricsEnabled: false, diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go index 543292ebe5..dad23cf00b 100644 --- a/internal/config/helpers.gen.go +++ b/internal/config/helpers.gen.go @@ -2191,6 +2191,22 @@ func (st *ConfigState) SetTracingEndpoint(v string) { st.reloadToViper() } +// GetTracingHeaders safely fetches the Configuration value for state's 'TracingHeaders' field +func (st *ConfigState) GetTracingHeaders() (v map[string]string) { + st.mutex.RLock() + v = st.config.TracingHeaders + st.mutex.RUnlock() + return +} + +// SetTracingEndpoint safely sets the Configuration value for state's 'TracingHeaders' field +func (st *ConfigState) SetTracingHeaders(v map[string]string) { + st.mutex.Lock() + defer st.mutex.Unlock() + st.config.TracingHeaders = v + st.reloadToViper() +} + // TracingEndpointFlag returns the flag name for the 'TracingEndpoint' field func TracingEndpointFlag() string { return "tracing-endpoint" } diff --git a/internal/tracing/tracing.go b/internal/tracing/tracing.go index dd1e19be96..ee25e43c7d 100644 --- a/internal/tracing/tracing.go +++ b/internal/tracing/tracing.go @@ -60,6 +60,7 @@ func Initialize() error { case "grpc": opts := []otlptracegrpc.Option{ otlptracegrpc.WithEndpoint(config.GetTracingEndpoint()), + otlptracegrpc.WithHeaders(config.NewState().GetTracingHeaders()), } if insecure { opts = append(opts, otlptracegrpc.WithInsecure()) From d206cf830668ca0bb46f788d54ae743e8e88ac37 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sun, 8 Dec 2024 00:36:29 +0100 Subject: [PATCH 2/5] update test/envparsing.sh Maps are currently not supported in environment variables. See also https://github.com/spf13/viper/issues/339 --- test/envparsing.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/envparsing.sh b/test/envparsing.sh index 372c623278..a67c2fcb3e 100755 --- a/test/envparsing.sh +++ b/test/envparsing.sh @@ -190,6 +190,7 @@ EXPECT=$(cat << "EOF" "tls-certificate-key": "", "tracing-enabled": false, "tracing-endpoint": "localhost:4317", + "tracing-headers": {}, "tracing-insecure-transport": true, "tracing-transport": "grpc", "trusted-proxies": [ From 3d3d82a9313130f4896917a6c30d699aa16f485e Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sun, 8 Dec 2024 14:15:27 +0100 Subject: [PATCH 3/5] Regenerate internal/config/helpers.gen.go ``` go run ./internal/config/gen/ -out ./internal/config/helpers.gen.go ``` --- internal/config/helpers.gen.go | 50 ++++++++++++++++------------------ 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go index dad23cf00b..77223114c8 100644 --- a/internal/config/helpers.gen.go +++ b/internal/config/helpers.gen.go @@ -2,7 +2,7 @@ // GoToSocial // Copyright (C) GoToSocial Authors admin@gotosocial.org // SPDX-License-Identifier: AGPL-3.0-or-later -// +// // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or @@ -2191,6 +2191,15 @@ func (st *ConfigState) SetTracingEndpoint(v string) { st.reloadToViper() } +// TracingEndpointFlag returns the flag name for the 'TracingEndpoint' field +func TracingEndpointFlag() string { return "tracing-endpoint" } + +// GetTracingEndpoint safely fetches the value for global configuration 'TracingEndpoint' field +func GetTracingEndpoint() string { return global.GetTracingEndpoint() } + +// SetTracingEndpoint safely sets the value for global configuration 'TracingEndpoint' field +func SetTracingEndpoint(v string) { global.SetTracingEndpoint(v) } + // GetTracingHeaders safely fetches the Configuration value for state's 'TracingHeaders' field func (st *ConfigState) GetTracingHeaders() (v map[string]string) { st.mutex.RLock() @@ -2199,7 +2208,7 @@ func (st *ConfigState) GetTracingHeaders() (v map[string]string) { return } -// SetTracingEndpoint safely sets the Configuration value for state's 'TracingHeaders' field +// SetTracingHeaders safely sets the Configuration value for state's 'TracingHeaders' field func (st *ConfigState) SetTracingHeaders(v map[string]string) { st.mutex.Lock() defer st.mutex.Unlock() @@ -2207,14 +2216,14 @@ func (st *ConfigState) SetTracingHeaders(v map[string]string) { st.reloadToViper() } -// TracingEndpointFlag returns the flag name for the 'TracingEndpoint' field -func TracingEndpointFlag() string { return "tracing-endpoint" } +// TracingHeadersFlag returns the flag name for the 'TracingHeaders' field +func TracingHeadersFlag() string { return "tracing-headers" } -// GetTracingEndpoint safely fetches the value for global configuration 'TracingEndpoint' field -func GetTracingEndpoint() string { return global.GetTracingEndpoint() } +// GetTracingHeaders safely fetches the value for global configuration 'TracingHeaders' field +func GetTracingHeaders() map[string]string { return global.GetTracingHeaders() } -// SetTracingEndpoint safely sets the value for global configuration 'TracingEndpoint' field -func SetTracingEndpoint(v string) { global.SetTracingEndpoint(v) } +// SetTracingHeaders safely sets the value for global configuration 'TracingHeaders' field +func SetTracingHeaders(v map[string]string) { global.SetTracingHeaders(v) } // GetTracingInsecureTransport safely fetches the Configuration value for state's 'TracingInsecureTransport' field func (st *ConfigState) GetTracingInsecureTransport() (v bool) { @@ -3158,19 +3167,13 @@ func (st *ConfigState) SetCacheConversationLastStatusIDsMemRatio(v float64) { } // CacheConversationLastStatusIDsMemRatioFlag returns the flag name for the 'Cache.ConversationLastStatusIDsMemRatio' field -func CacheConversationLastStatusIDsMemRatioFlag() string { - return "cache-conversation-last-status-ids-mem-ratio" -} +func CacheConversationLastStatusIDsMemRatioFlag() string { return "cache-conversation-last-status-ids-mem-ratio" } // GetCacheConversationLastStatusIDsMemRatio safely fetches the value for global configuration 'Cache.ConversationLastStatusIDsMemRatio' field -func GetCacheConversationLastStatusIDsMemRatio() float64 { - return global.GetCacheConversationLastStatusIDsMemRatio() -} +func GetCacheConversationLastStatusIDsMemRatio() float64 { return global.GetCacheConversationLastStatusIDsMemRatio() } // SetCacheConversationLastStatusIDsMemRatio safely sets the value for global configuration 'Cache.ConversationLastStatusIDsMemRatio' field -func SetCacheConversationLastStatusIDsMemRatio(v float64) { - global.SetCacheConversationLastStatusIDsMemRatio(v) -} +func SetCacheConversationLastStatusIDsMemRatio(v float64) { global.SetCacheConversationLastStatusIDsMemRatio(v) } // GetCacheDomainPermissionDraftMemRation safely fetches the Configuration value for state's 'Cache.DomainPermissionDraftMemRation' field func (st *ConfigState) GetCacheDomainPermissionDraftMemRation() (v float64) { @@ -3189,19 +3192,13 @@ func (st *ConfigState) SetCacheDomainPermissionDraftMemRation(v float64) { } // CacheDomainPermissionDraftMemRationFlag returns the flag name for the 'Cache.DomainPermissionDraftMemRation' field -func CacheDomainPermissionDraftMemRationFlag() string { - return "cache-domain-permission-draft-mem-ratio" -} +func CacheDomainPermissionDraftMemRationFlag() string { return "cache-domain-permission-draft-mem-ratio" } // GetCacheDomainPermissionDraftMemRation safely fetches the value for global configuration 'Cache.DomainPermissionDraftMemRation' field -func GetCacheDomainPermissionDraftMemRation() float64 { - return global.GetCacheDomainPermissionDraftMemRation() -} +func GetCacheDomainPermissionDraftMemRation() float64 { return global.GetCacheDomainPermissionDraftMemRation() } // SetCacheDomainPermissionDraftMemRation safely sets the value for global configuration 'Cache.DomainPermissionDraftMemRation' field -func SetCacheDomainPermissionDraftMemRation(v float64) { - global.SetCacheDomainPermissionDraftMemRation(v) -} +func SetCacheDomainPermissionDraftMemRation(v float64) { global.SetCacheDomainPermissionDraftMemRation(v) } // GetCacheEmojiMemRatio safely fetches the Configuration value for state's 'Cache.EmojiMemRatio' field func (st *ConfigState) GetCacheEmojiMemRatio() (v float64) { @@ -4427,3 +4424,4 @@ func GetRequestIDHeader() string { return global.GetRequestIDHeader() } // SetRequestIDHeader safely sets the value for global configuration 'RequestIDHeader' field func SetRequestIDHeader(v string) { global.SetRequestIDHeader(v) } + From 2a81882de316a73485ddc74d748359440342b9cb Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sun, 8 Dec 2024 20:51:15 +0100 Subject: [PATCH 4/5] go fmt ./internal/config/helpers.gen.go --- internal/config/helpers.gen.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go index 77223114c8..dd49951b51 100644 --- a/internal/config/helpers.gen.go +++ b/internal/config/helpers.gen.go @@ -2,7 +2,7 @@ // GoToSocial // Copyright (C) GoToSocial Authors admin@gotosocial.org // SPDX-License-Identifier: AGPL-3.0-or-later -// +// // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or @@ -3167,13 +3167,19 @@ func (st *ConfigState) SetCacheConversationLastStatusIDsMemRatio(v float64) { } // CacheConversationLastStatusIDsMemRatioFlag returns the flag name for the 'Cache.ConversationLastStatusIDsMemRatio' field -func CacheConversationLastStatusIDsMemRatioFlag() string { return "cache-conversation-last-status-ids-mem-ratio" } +func CacheConversationLastStatusIDsMemRatioFlag() string { + return "cache-conversation-last-status-ids-mem-ratio" +} // GetCacheConversationLastStatusIDsMemRatio safely fetches the value for global configuration 'Cache.ConversationLastStatusIDsMemRatio' field -func GetCacheConversationLastStatusIDsMemRatio() float64 { return global.GetCacheConversationLastStatusIDsMemRatio() } +func GetCacheConversationLastStatusIDsMemRatio() float64 { + return global.GetCacheConversationLastStatusIDsMemRatio() +} // SetCacheConversationLastStatusIDsMemRatio safely sets the value for global configuration 'Cache.ConversationLastStatusIDsMemRatio' field -func SetCacheConversationLastStatusIDsMemRatio(v float64) { global.SetCacheConversationLastStatusIDsMemRatio(v) } +func SetCacheConversationLastStatusIDsMemRatio(v float64) { + global.SetCacheConversationLastStatusIDsMemRatio(v) +} // GetCacheDomainPermissionDraftMemRation safely fetches the Configuration value for state's 'Cache.DomainPermissionDraftMemRation' field func (st *ConfigState) GetCacheDomainPermissionDraftMemRation() (v float64) { @@ -3192,13 +3198,19 @@ func (st *ConfigState) SetCacheDomainPermissionDraftMemRation(v float64) { } // CacheDomainPermissionDraftMemRationFlag returns the flag name for the 'Cache.DomainPermissionDraftMemRation' field -func CacheDomainPermissionDraftMemRationFlag() string { return "cache-domain-permission-draft-mem-ratio" } +func CacheDomainPermissionDraftMemRationFlag() string { + return "cache-domain-permission-draft-mem-ratio" +} // GetCacheDomainPermissionDraftMemRation safely fetches the value for global configuration 'Cache.DomainPermissionDraftMemRation' field -func GetCacheDomainPermissionDraftMemRation() float64 { return global.GetCacheDomainPermissionDraftMemRation() } +func GetCacheDomainPermissionDraftMemRation() float64 { + return global.GetCacheDomainPermissionDraftMemRation() +} // SetCacheDomainPermissionDraftMemRation safely sets the value for global configuration 'Cache.DomainPermissionDraftMemRation' field -func SetCacheDomainPermissionDraftMemRation(v float64) { global.SetCacheDomainPermissionDraftMemRation(v) } +func SetCacheDomainPermissionDraftMemRation(v float64) { + global.SetCacheDomainPermissionDraftMemRation(v) +} // GetCacheEmojiMemRatio safely fetches the Configuration value for state's 'Cache.EmojiMemRatio' field func (st *ConfigState) GetCacheEmojiMemRatio() (v float64) { @@ -4424,4 +4436,3 @@ func GetRequestIDHeader() string { return global.GetRequestIDHeader() } // SetRequestIDHeader safely sets the value for global configuration 'RequestIDHeader' field func SetRequestIDHeader(v string) { global.SetRequestIDHeader(v) } - From 6c013c25cb5b7123cb49f95355e9f21864b8c8ae Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sat, 14 Dec 2024 17:27:56 +0100 Subject: [PATCH 5/5] Remove accidentally added config.NewState() --- internal/tracing/tracing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/tracing/tracing.go b/internal/tracing/tracing.go index ee25e43c7d..ec23294caf 100644 --- a/internal/tracing/tracing.go +++ b/internal/tracing/tracing.go @@ -60,7 +60,7 @@ func Initialize() error { case "grpc": opts := []otlptracegrpc.Option{ otlptracegrpc.WithEndpoint(config.GetTracingEndpoint()), - otlptracegrpc.WithHeaders(config.NewState().GetTracingHeaders()), + otlptracegrpc.WithHeaders(config.GetTracingHeaders()), } if insecure { opts = append(opts, otlptracegrpc.WithInsecure())