Skip to content

Commit ee8f42a

Browse files
feat: allow overriding service name in tracer, meter, and logger config (#2883)
* feat: allow overriding service name in tracer, meter, and logger config * fix: add empty-value fallback for service name in telemetry providers * feat: add service_name fields to permify config output * fix: register service_name flags in config command to prevent panic
1 parent 6203786 commit ee8f42a

10 files changed

Lines changed: 178 additions & 85 deletions

File tree

docs/setting-up/configuration.mdx

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,24 @@ Real time logs of authorization. Permify uses [zerolog] as a logger.
199199
```
200200
├── logger
201201
├── level
202+
├── service_name
202203
```
203204

204205
#### Glossary
205206

206-
| Required | Argument | Default | Description |
207-
|----------|----------|---------|--------------------------------------------------|
208-
| [x] | level | info | logger levels: `error`, `warn`, `info` , `debug` |
209-
| [x] | output | text | logger output: `json`, `text` |
207+
| Required | Argument | Default | Description |
208+
|----------|--------------|---------|----------------------------------------------------------|
209+
| [x] | level | info | logger levels: `error`, `warn`, `info` , `debug` |
210+
| [x] | output | text | logger output: `json`, `text` |
211+
| [ ] | service_name | permify | Override the service name reported by the log exporter. |
210212

211213
#### ENV
212214

213-
| Argument | ENV | Type |
214-
|------------|--------------------|--------|
215-
| log-level | PERMIFY_LOG_LEVEL | string |
216-
| log-output | PERMIFY_LOG_OUTPUT | string |
215+
| Argument | ENV | Type |
216+
|-------------------|---------------------------|--------|
217+
| log-level | PERMIFY_LOG_LEVEL | string |
218+
| log-output | PERMIFY_LOG_OUTPUT | string |
219+
| log-service-name | PERMIFY_LOG_SERVICE_NAME | string |
217220

218221
</Accordion>
219222

@@ -330,6 +333,7 @@ authorization when using Permify.
330333
| ├── enabled
331334
| ├── insecure
332335
| ├── urlpath
336+
| ├── service_name
333337
```
334338

335339
#### Glossary
@@ -340,17 +344,19 @@ authorization when using Permify.
340344
| [x] | endpoint | - | export uri for tracing data. |
341345
| [ ] | enabled | false | switch option for tracing. |
342346
| [ ] | urlpath | | allows one to override the default URL path for otlp, used for sending traces. If unset, default ("/v1/traces") will be used. |
343-
| [ ] | insecure | false | Whether to use HTTP instead of HTTPs for exporting the traces. |
347+
| [ ] | insecure | false | Whether to use HTTP instead of HTTPs for exporting the traces. |
348+
| [ ] | service_name | permify | Override the service name reported by the tracing exporter. |
344349

345350
#### ENV
346351

347-
| Argument | ENV | Type |
348-
|-----------------|-------------------------|---------|
349-
| tracer-enabled | PERMIFY_TRACER_ENABLED | boolean |
350-
| tracer-exporter | PERMIFY_TRACER_EXPORTER | string |
351-
| tracer-endpoint | PERMIFY_TRACER_ENDPOINT | string |
352-
| tracer-urlpath | PERMIFY_TRACER_URL_PATH | string |
353-
| tracer-insecure | PERMIFY_TRACER_INSECURE | boolean |
352+
| Argument | ENV | Type |
353+
|----------------------|------------------------------|---------|
354+
| tracer-enabled | PERMIFY_TRACER_ENABLED | boolean |
355+
| tracer-exporter | PERMIFY_TRACER_EXPORTER | string |
356+
| tracer-endpoint | PERMIFY_TRACER_ENDPOINT | string |
357+
| tracer-urlpath | PERMIFY_TRACER_URL_PATH | string |
358+
| tracer-insecure | PERMIFY_TRACER_INSECURE | boolean |
359+
| tracer-service-name | PERMIFY_TRACER_SERVICE_NAME | string |
354360

355361
</Accordion>
356362

@@ -370,25 +376,28 @@ os, arch.
370376
| ├── enabled
371377
| ├── insecure
372378
| ├── urlpath
379+
| ├── service_name
373380
```
374381

375382
#### Glossary
376383

377384
| Required | Argument | Default | Description |
378385
|----------|----------|---------|--------------------------------------------------------------|
379386
| [x] | exporter | - | [otlp](https://opentelemetry.io/docs/collector/) is default. |
380-
| [x] | endpoint | - | export uri for metric observation |
381-
| [ ] | enabled | true | switch option for meter tracing. |
387+
| [x] | endpoint | - | export uri for metric observation |
388+
| [ ] | enabled | true | switch option for meter tracing. |
389+
| [ ] | service_name | permify | Override the service name reported by the metrics exporter. |
382390

383391
#### ENV
384392

385-
| Argument | ENV | Type |
386-
|----------------|------------------------|---------|
387-
| meter-enabled | PERMIFY_METER_ENABLED | boolean |
388-
| meter-exporter | PERMIFY_METER_EXPORTER | string |
389-
| meter-endpoint | PERMIFY_METER_ENDPOINT | string |
390-
| meter-urlpath | PERMIFY_METER_URL_PATH | string |
391-
| meter-insecure | PERMIFY_METER_INSECURE | boolean |
393+
| Argument | ENV | Type |
394+
|---------------------|-----------------------------|---------|
395+
| meter-enabled | PERMIFY_METER_ENABLED | boolean |
396+
| meter-exporter | PERMIFY_METER_EXPORTER | string |
397+
| meter-endpoint | PERMIFY_METER_ENDPOINT | string |
398+
| meter-urlpath | PERMIFY_METER_URL_PATH | string |
399+
| meter-insecure | PERMIFY_METER_INSECURE | boolean |
400+
| meter-service-name | PERMIFY_METER_SERVICE_NAME | string |
392401

393402
</Accordion>
394403

example.config.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ tracer:
4545
exporter: zipkin
4646
endpoint: http://localhost:9411/api/v2/spans
4747
enabled: false
48+
service_name: permify
4849

4950
# The meter section enables or disables metrics collection and sets the
5051
# exporter and endpoint for the collected metrics.
5152
meter:
5253
exporter: otlp
5354
endpoint: localhost:4318
5455
enabled: false
56+
service_name: permify
5557

5658
# The service section sets various service-level settings, including whether
5759
# or not to use a circuit breaker, and cache sizes for schema, permission,
@@ -79,16 +81,16 @@ database:
7981
engine: postgres
8082
uri: postgres://user:password@host:5432/db_name
8183
auto_migrate: false
82-
max_connections: 20 # Maximum number of connections in the pool (maps to pgxpool MaxConns)
83-
max_open_connections: 20 # Deprecated: use max_connections instead. Kept for backward compatibility.
84-
max_idle_connections: 1 # Deprecated: use min_connections instead. Kept for backward compatibility (maps to MinConnections if min_connections is not set).
85-
min_connections: 0 # Minimum number of connections in the pool (maps to pgxpool MinConns). If 0 and max_idle_connections is set, max_idle_connections will be used.
86-
min_idle_connections: 0 # Minimum idle connections (maps to pgxpool MinIdleConns). Must be explicitly set if needed (not set in old code).
84+
max_connections: 20 # Maximum number of connections in the pool (maps to pgxpool MaxConns)
85+
max_open_connections: 20 # Deprecated: use max_connections instead. Kept for backward compatibility.
86+
max_idle_connections: 1 # Deprecated: use min_connections instead. Kept for backward compatibility (maps to MinConnections if min_connections is not set).
87+
min_connections: 0 # Minimum number of connections in the pool (maps to pgxpool MinConns). If 0 and max_idle_connections is set, max_idle_connections will be used.
88+
min_idle_connections: 0 # Minimum idle connections (maps to pgxpool MinIdleConns). Must be explicitly set if needed (not set in old code).
8789
max_connection_lifetime: 300s
8890
max_connection_idle_time: 60s
89-
health_check_period: 0s # Use pgxpool default (1 minute) if 0
90-
max_connection_lifetime_jitter: 0s # Will default to 20% of max_connection_lifetime if 0
91-
connect_timeout: 0s # Use pgx default (no timeout) if 0
91+
health_check_period: 0s # Use pgxpool default (1 minute) if 0
92+
max_connection_lifetime_jitter: 0s # Will default to 20% of max_connection_lifetime if 0
93+
connect_timeout: 0s # Use pgx default (no timeout) if 0
9294
max_data_per_write: 1_000
9395
max_retries: 10
9496
watch_buffer_size: 100

internal/config/config.go

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -89,38 +89,41 @@ type (
8989

9090
// Log contains configuration for logging.
9191
Log struct {
92-
Level string `mapstructure:"level"` // Logging level
93-
Output string `mapstructure:"output"` // Logging output format, e.g., text, json
94-
Enabled bool `mapstructure:"enabled"` // Whether logging collection is enabled
95-
Exporter string `mapstructure:"exporter"` // Exporter for log data
96-
Endpoint string `mapstructure:"endpoint"` // Endpoint for the log exporter
97-
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
98-
Urlpath string `mapstructure:"urlpath"` // Path for the log exporter, if not defined /v1/logs will be used
99-
Headers []string `mapstructure:"headers"`
100-
Protocol string `mapstructure:"protocol"` // Protocol for the log exporter, http or grpc
92+
Level string `mapstructure:"level"` // Logging level
93+
Output string `mapstructure:"output"` // Logging output format, e.g., text, json
94+
Enabled bool `mapstructure:"enabled"` // Whether logging collection is enabled
95+
Exporter string `mapstructure:"exporter"` // Exporter for log data
96+
Endpoint string `mapstructure:"endpoint"` // Endpoint for the log exporter
97+
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
98+
Urlpath string `mapstructure:"urlpath"` // Path for the log exporter, if not defined /v1/logs will be used
99+
Headers []string `mapstructure:"headers"`
100+
Protocol string `mapstructure:"protocol"` // Protocol for the log exporter, http or grpc
101+
ServiceName string `mapstructure:"service_name"` // Override the service name reported by the exporter (default: "permify")
101102
}
102103

103104
// Tracer contains configuration for distributed tracing.
104105
Tracer struct {
105-
Enabled bool `mapstructure:"enabled"` // Whether tracing collection is enabled
106-
Exporter string `mapstructure:"exporter"` // Exporter for tracing data
107-
Endpoint string `mapstructure:"endpoint"` // Endpoint for the tracing exporter
108-
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
109-
Urlpath string `mapstructure:"urlpath"` // Path for the tracing exporter, if not defined /v1/trace will be used
110-
Headers []string `mapstructure:"headers"`
111-
Protocol string `mapstructure:"protocol"` // Protocol for the tracing exporter, http or grpc
106+
Enabled bool `mapstructure:"enabled"` // Whether tracing collection is enabled
107+
Exporter string `mapstructure:"exporter"` // Exporter for tracing data
108+
Endpoint string `mapstructure:"endpoint"` // Endpoint for the tracing exporter
109+
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
110+
Urlpath string `mapstructure:"urlpath"` // Path for the tracing exporter, if not defined /v1/trace will be used
111+
Headers []string `mapstructure:"headers"`
112+
Protocol string `mapstructure:"protocol"` // Protocol for the tracing exporter, http or grpc
113+
ServiceName string `mapstructure:"service_name"` // Override the service name reported by the exporter (default: "permify")
112114
}
113115

114116
// Meter contains configuration for metrics collection and reporting.
115117
Meter struct {
116-
Enabled bool `mapstructure:"enabled"` // Whether metrics collection is enabled
117-
Exporter string `mapstructure:"exporter"` // Exporter for metrics data
118-
Endpoint string `mapstructure:"endpoint"` // Endpoint for the metrics exporter
119-
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
120-
Urlpath string `mapstructure:"urlpath"` // Path for the metrics exporter, if not defined /v1/metrics will be used
121-
Headers []string `mapstructure:"headers"`
122-
Interval int `mapstructure:"interval"`
123-
Protocol string `mapstructure:"protocol"` // Protocol for the metrics exporter, http or grpc
118+
Enabled bool `mapstructure:"enabled"` // Whether metrics collection is enabled
119+
Exporter string `mapstructure:"exporter"` // Exporter for metrics data
120+
Endpoint string `mapstructure:"endpoint"` // Endpoint for the metrics exporter
121+
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
122+
Urlpath string `mapstructure:"urlpath"` // Path for the metrics exporter, if not defined /v1/metrics will be used
123+
Headers []string `mapstructure:"headers"`
124+
Interval int `mapstructure:"interval"`
125+
Protocol string `mapstructure:"protocol"` // Protocol for the metrics exporter, http or grpc
126+
ServiceName string `mapstructure:"service_name"` // Override the service name reported by the exporter (default: "permify")
124127
}
125128

126129
// Service contains configuration for various service-level features.
@@ -308,24 +311,27 @@ func DefaultConfig() *Config {
308311
Enabled: false,
309312
},
310313
Log: Log{
311-
Level: "info",
312-
Enabled: false,
313-
Exporter: "otlp",
314-
Headers: []string{},
315-
Protocol: "http",
314+
Level: "info",
315+
Enabled: false,
316+
Exporter: "otlp",
317+
Headers: []string{},
318+
Protocol: "http",
319+
ServiceName: "permify",
316320
},
317321
Tracer: Tracer{
318-
Enabled: false,
319-
Headers: []string{},
320-
Protocol: "http",
322+
Enabled: false,
323+
Headers: []string{},
324+
Protocol: "http",
325+
ServiceName: "permify",
321326
},
322327
Meter: Meter{
323-
Enabled: false,
324-
Exporter: "otlp",
325-
Endpoint: "telemetry.permify.co",
326-
Headers: []string{},
327-
Interval: 300,
328-
Protocol: "http",
328+
Enabled: false,
329+
Exporter: "otlp",
330+
Endpoint: "telemetry.permify.co",
331+
Headers: []string{},
332+
Interval: 300,
333+
Protocol: "http",
334+
ServiceName: "permify",
329335
},
330336
Service: Service{
331337
CircuitBreaker: false,

internal/config/config_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,39 @@ logger:
207207
assert.Equal(t, "debug", cfg.Log.Level) // Log level
208208
}
209209

210+
func TestDefaultConfig_ServiceName(t *testing.T) {
211+
cfg := DefaultConfig()
212+
assert.Equal(t, "permify", cfg.Tracer.ServiceName)
213+
assert.Equal(t, "permify", cfg.Meter.ServiceName)
214+
assert.Equal(t, "permify", cfg.Log.ServiceName)
215+
}
216+
217+
func TestNewConfigWithFile_ServiceName(t *testing.T) {
218+
configContent := []byte(`
219+
tracer:
220+
service_name: "new-service-name"
221+
meter:
222+
service_name: "new-service-name"
223+
logger:
224+
service_name: "new-service-name"
225+
`)
226+
227+
tmpDir, err := os.MkdirTemp("", "service-name-config-test")
228+
require.NoError(t, err)
229+
defer os.RemoveAll(tmpDir)
230+
231+
tmpFile := filepath.Join(tmpDir, "config.yaml")
232+
err = os.WriteFile(tmpFile, configContent, 0o666)
233+
require.NoError(t, err)
234+
235+
cfg, err := NewConfigWithFile(tmpFile)
236+
assert.NoError(t, err)
237+
assert.NotNil(t, cfg)
238+
assert.Equal(t, "new-service-name", cfg.Tracer.ServiceName)
239+
assert.Equal(t, "new-service-name", cfg.Meter.ServiceName)
240+
assert.Equal(t, "new-service-name", cfg.Log.ServiceName)
241+
}
242+
210243
// TestNewConfigWithFile_InvalidConfig tests invalid config handling
211244
func TestNewConfigWithFile_InvalidConfig(t *testing.T) { // Test invalid config
212245
configContent := []byte(`

0 commit comments

Comments
 (0)