[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"
```
This commit is contained in:
Jochen Schalanda 2024-12-07 23:35:27 +01:00
parent 8504043024
commit 56204cc2f4
9 changed files with 48 additions and 4 deletions

View file

@ -12,6 +12,9 @@ You'll need the files in [`example/tracing`][ext]. Once you have those you can r
tracing-enabled: true tracing-enabled: true
tracing-transport: "grpc" tracing-transport: "grpc"
tracing-endpoint: "localhost:4317" tracing-endpoint: "localhost:4317"
tracing-headers:
"Authorization": "Bearer super-secret-token"
"Dataset": "gotosocial"
tracing-insecure-transport: true tracing-insecure-transport: true
``` ```

View file

@ -35,6 +35,13 @@ tracing-transport: "grpc"
# Default: "" # Default: ""
tracing-endpoint: "" 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. # Bool. Disable TLS for the gRPC and HTTP transport protocols.
# Default: false # Default: false
tracing-insecure-transport: false tracing-insecure-transport: false

View file

@ -12,6 +12,9 @@ GoToSocial 内置了基于 [OpenTelemetry][otel] 的追踪功能。虽然并没
tracing-enabled: true tracing-enabled: true
tracing-transport: "grpc" tracing-transport: "grpc"
tracing-endpoint: "localhost:4317" tracing-endpoint: "localhost:4317"
tracing-headers:
"Authorization": "Bearer super-secret-token"
"Dataset": "gotosocial"
tracing-insecure-transport: true tracing-insecure-transport: true
``` ```

View file

@ -31,6 +31,11 @@ tracing-transport: "grpc"
# 默认值: "" # 默认值: ""
tracing-endpoint: "" tracing-endpoint: ""
# TODO
# 示例: {"Authorization": "Bearer super-secret-token", "Dataset": "gotosocial"}
# 默认值: {}
tracing-headers: {}
# 布尔值。禁用gRPC和HTTP传输协议的TLS。 # 布尔值。禁用gRPC和HTTP传输协议的TLS。
# 默认值: false # 默认值: false
tracing-insecure-transport: false tracing-insecure-transport: false

View file

@ -928,6 +928,13 @@ tracing-transport: "grpc"
# Default: "" # Default: ""
tracing-endpoint: "" 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. # Bool. Disable TLS for the gRPC and HTTP transport protocols.
# Default: false # Default: false
tracing-insecure-transport: false tracing-insecure-transport: false

View file

@ -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."` 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"` 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"` TracingEnabled bool `name:"tracing-enabled" usage:"Enable OTLP Tracing"`
TracingTransport string `name:"tracing-transport" usage:"grpc or http"` 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"` 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"` 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."` 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"` MetricsAuthEnabled bool `name:"metrics-auth-enabled" usage:"Enable HTTP Basic Authentication for Prometheus metrics endpoint"`

View file

@ -120,6 +120,7 @@
TracingEnabled: false, TracingEnabled: false,
TracingTransport: "grpc", TracingTransport: "grpc",
TracingEndpoint: "", TracingEndpoint: "",
TracingHeaders: map[string]string{},
TracingInsecureTransport: false, TracingInsecureTransport: false,
MetricsEnabled: false, MetricsEnabled: false,

View file

@ -2191,6 +2191,22 @@ func (st *ConfigState) SetTracingEndpoint(v string) {
st.reloadToViper() 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 // TracingEndpointFlag returns the flag name for the 'TracingEndpoint' field
func TracingEndpointFlag() string { return "tracing-endpoint" } func TracingEndpointFlag() string { return "tracing-endpoint" }

View file

@ -60,6 +60,7 @@ func Initialize() error {
case "grpc": case "grpc":
opts := []otlptracegrpc.Option{ opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(config.GetTracingEndpoint()), otlptracegrpc.WithEndpoint(config.GetTracingEndpoint()),
otlptracegrpc.WithHeaders(config.NewState().GetTracingHeaders()),
} }
if insecure { if insecure {
opts = append(opts, otlptracegrpc.WithInsecure()) opts = append(opts, otlptracegrpc.WithInsecure())