Skip to content

Commit da02b0e

Browse files
lvlcn-tpuffitos
andauthored
refactor: exporter config (#214)
* refactor: exporter config * refactor: add common exporter config struct instead of multiple returns * docs: add missing docs for exporter config Signed-off-by: lvlcn-t <[email protected]> * chore: simplified if guard Merged the two ifs into one. * docs: add certpath hint Signed-off-by: lvlcn-t <[email protected]> --------- Signed-off-by: lvlcn-t <[email protected]> Co-authored-by: Bruno Bressi <[email protected]>
1 parent 52d6213 commit da02b0e

File tree

3 files changed

+67
-47
lines changed

3 files changed

+67
-47
lines changed

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ The binary is available for several distributions. To install the binary, use a
8181
Replace `${RELEASE_VERSION}` with the desired release version:
8282

8383
```sh
84-
curl https://github.com/caas-team/sparrow/releases/download/v${RELEASE_VERSION}/sparrow_${RELEASE_VERSION}_linux_amd64.tar.gz -Lo sparrow.tar.gz
85-
curl https://github.com/caas-team/sparrow/releases/download/v${RELEASE_VERSION}/sparrow_${RELEASE_VERSION}_checksums.txt -Lo checksums.txt
84+
export RELEASE_VERSION=0.5.0
8685
```
8786

88-
For example, for release `v0.3.1`:
87+
Download the binary:
8988

9089
```sh
91-
curl https://github.com/caas-team/sparrow/releases/download/v0.3.1/sparrow_0.3.1_linux_amd64.tar.gz -Lo sparrow.tar.gz
92-
curl https://github.com/caas-team/sparrow/releases/download/v0.3.1/sparrow_0.3.1_checksums.txt -Lo checksums.txt
90+
curl https://github.com/caas-team/sparrow/releases/download/v${RELEASE_VERSION}/sparrow_${RELEASE_VERSION}_linux_amd64.tar.gz -Lo sparrow.tar.gz
91+
curl https://github.com/caas-team/sparrow/releases/download/v${RELEASE_VERSION}/sparrow_${RELEASE_VERSION}_checksums.txt -Lo checksums.txt
9392
```
9493

9594
Extract the binary:
@@ -291,9 +290,13 @@ telemetry:
291290
# The token to use for authentication.
292291
# If the exporter does not require a token, this can be left empty.
293292
token: ""
294-
# The path to the tls certificate to use.
295-
# To disable tls, either set this to an empty string or set it to insecure.
296-
certPath: ""
293+
# Configures tls for the telemetry exporter
294+
tls:
295+
# Enable or disable TLS
296+
enabled: true
297+
# The path to the tls certificate to use.
298+
# Only required if your otel endpoint uses custom TLS certificates
299+
certPath: ""
297300
```
298301

299302
#### Loader
@@ -644,17 +647,21 @@ Replace `<sparrow_instance_address>` with the actual address of your `sparrow` i
644647

645648
The `sparrow` supports exporting telemetry data using the OpenTelemetry Protocol (OTLP). This allows users to choose their preferred telemetry provider and collector. The following configuration options are available for setting up telemetry:
646649

647-
| Field | Type | Description |
648-
| ---------- | -------- | ------------------------------------------------------------------------ |
649-
| `exporter` | `string` | The telemetry exporter to use. Options: `grpc`, `http`, `stdout`, `noop` |
650-
| `url` | `string` | The address to export telemetry to |
651-
| `token` | `string` | The token to use for authentication |
652-
| `certPath` | `string` | The path to the TLS certificate to use |
650+
| Field | Type | Description |
651+
| -------------- | -------- | --------------------------------------------------------------------------- |
652+
| `enabled` | `bool` | Whether to enable telemetry. Default: `false` |
653+
| `exporter` | `string` | The telemetry exporter to use. Options: `grpc`, `http`, `stdout`, `noop` |
654+
| `url` | `string` | The address to export telemetry to. |
655+
| `token` | `string` | The token to use for authentication. |
656+
| `tls.enabled` | `bool` | Enable or disable TLS. |
657+
| `tls.certPath` | `string` | The path to the TLS certificate to use. Only required if custom TLS is used |
653658

654659
For example, to export telemetry data using OTLP via gRPC, you can add the following configuration to your [startup configuration](#startup):
655660

656661
```yaml
657662
telemetry:
663+
# Whether to enable telemetry. (default: false)
664+
enabled: true
658665
# The telemetry exporter to use.
659666
# Options:
660667
# grpc: Exports telemetry using OTLP via gRPC.

pkg/sparrow/metrics/config.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ type Config struct {
3434
// Url is the Url of the collector to which the traces are exported
3535
Url string `yaml:"url" mapstructure:"url"`
3636
// Token is the token used to authenticate with the collector
37-
Token string `yaml:"token" mapstructure:"token"`
38-
Tls TLSConfig `yaml:"tls" mapstructure:"tls"`
37+
Token string `yaml:"token" mapstructure:"token"`
38+
// TLS holds the tls configuration
39+
TLS TLSConfig `yaml:"tls" mapstructure:"tls"`
3940
}
4041

4142
type TLSConfig struct {
42-
// CertPath is the path to the tls certificate file
43+
// Enabled is a flag to enable or disable the tls
44+
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
45+
// CertPath is the path to the tls certificate file.
46+
// This is only required if the otel backend uses custom TLS certificates.
4347
CertPath string `yaml:"certPath" mapstructure:"certPath"`
44-
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
4548
}
4649

4750
func (c *Config) Validate(ctx context.Context) error {
@@ -51,11 +54,9 @@ func (c *Config) Validate(ctx context.Context) error {
5154
return err
5255
}
5356

54-
if c.Exporter.IsExporting() {
55-
if c.Url == "" {
56-
log.ErrorContext(ctx, "Url is required for otlp exporter", "exporter", c.Exporter)
57-
return fmt.Errorf("url is required for otlp exporter %q", c.Exporter)
58-
}
57+
if c.Exporter.IsExporting() && c.Url == "" {
58+
log.ErrorContext(ctx, "Url is required for otlp exporter", "exporter", c.Exporter)
59+
return fmt.Errorf("url is required for otlp exporter %q", c.Exporter)
5960
}
6061
return nil
6162
}

pkg/sparrow/metrics/exporter.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,44 +91,44 @@ func (e Exporter) Create(ctx context.Context, config *Config) (sdktrace.SpanExpo
9191

9292
// newHTTPExporter creates a new HTTP exporter
9393
func newHTTPExporter(ctx context.Context, config *Config) (sdktrace.SpanExporter, error) {
94-
headers, tlsCfg, err := getCommonConfig(config)
94+
cfg, err := newExporterConfig(config)
9595
if err != nil {
9696
return nil, err
9797
}
9898

9999
opts := []otlptracehttp.Option{
100100
otlptracehttp.WithEndpoint(config.Url),
101-
otlptracehttp.WithHeaders(headers),
101+
otlptracehttp.WithHeaders(cfg.headers),
102102
}
103-
if config.Tls.Enabled {
104-
if tlsCfg != nil {
105-
opts = append(opts, otlptracehttp.WithTLSClientConfig(tlsCfg))
106-
}
107-
} else {
103+
if !config.TLS.Enabled {
108104
opts = append(opts, otlptracehttp.WithInsecure())
105+
return otlptracehttp.New(ctx, opts...)
106+
}
107+
if cfg.tls != nil {
108+
opts = append(opts, otlptracehttp.WithTLSClientConfig(cfg.tls))
109109
}
110110

111111
return otlptracehttp.New(ctx, opts...)
112112
}
113113

114114
// newGRPCExporter creates a new gRPC exporter
115115
func newGRPCExporter(ctx context.Context, config *Config) (sdktrace.SpanExporter, error) {
116-
headers, tlsCfg, err := getCommonConfig(config)
116+
cfg, err := newExporterConfig(config)
117117
if err != nil {
118118
return nil, err
119119
}
120120

121121
opts := []otlptracegrpc.Option{
122122
otlptracegrpc.WithEndpoint(config.Url),
123-
otlptracegrpc.WithHeaders(headers),
123+
otlptracegrpc.WithHeaders(cfg.headers),
124124
}
125125

126-
if !config.Tls.Enabled {
126+
if !config.TLS.Enabled {
127127
opts = append(opts, otlptracegrpc.WithInsecure())
128128
return otlptracegrpc.New(ctx, opts...)
129129
}
130-
if tlsCfg != nil {
131-
opts = append(opts, otlptracegrpc.WithTLSCredentials(credentials.NewTLS(tlsCfg)))
130+
if cfg.tls != nil {
131+
opts = append(opts, otlptracegrpc.WithTLSCredentials(credentials.NewTLS(cfg.tls)))
132132
}
133133

134134
return otlptracegrpc.New(ctx, opts...)
@@ -144,22 +144,36 @@ func newNoopExporter(_ context.Context, _ *Config) (sdktrace.SpanExporter, error
144144
return nil, nil
145145
}
146146

147-
// getCommonConfig returns the common configuration for the exporters
148-
func getCommonConfig(config *Config) (map[string]string, *tls.Config, error) {
149-
headers := make(map[string]string)
147+
// exporterConfig contains the common configuration for the exporters
148+
type exporterConfig struct {
149+
// headers contains the headers to send with spans
150+
headers map[string]string
151+
// tls contains the TLS configuration for the exporter
152+
tls *tls.Config
153+
}
154+
155+
// newExporterConfig returns the common configuration for the exporters
156+
func newExporterConfig(config *Config) (exporterConfig, error) {
157+
headers := map[string]string{}
150158
if config.Token != "" {
151159
headers["Authorization"] = fmt.Sprintf("Bearer %s", config.Token)
152160
}
153161

154-
if config.Tls.Enabled {
155-
tlsCfg, err := getTLSConfig(config.Tls.CertPath)
162+
if config.TLS.Enabled {
163+
tlsCfg, err := getTLSConfig(config.TLS.CertPath)
156164
if err != nil {
157-
return nil, nil, fmt.Errorf("failed to create TLS configuration: %w", err)
165+
return exporterConfig{}, fmt.Errorf("failed to create TLS configuration: %w", err)
158166
}
159-
return headers, tlsCfg, nil
167+
return exporterConfig{
168+
headers: headers,
169+
tls: tlsCfg,
170+
}, nil
160171
}
161172

162-
return headers, nil, nil
173+
return exporterConfig{
174+
headers: headers,
175+
tls: nil,
176+
}, nil
163177
}
164178

165179
// FileOpener is the function used to open a file
@@ -172,7 +186,7 @@ var openFile FileOpener = func() FileOpener {
172186
}
173187
}()
174188

175-
func getTLSConfig(certFile string) (conf *tls.Config, err error) {
189+
func getTLSConfig(certFile string) (*tls.Config, error) {
176190
if certFile == "" {
177191
return nil, nil
178192
}
@@ -182,9 +196,7 @@ func getTLSConfig(certFile string) (conf *tls.Config, err error) {
182196
return nil, fmt.Errorf("failed to open certificate file: %w", err)
183197
}
184198
defer func() {
185-
if cErr := file.Close(); cErr != nil {
186-
err = errors.Join(err, cErr)
187-
}
199+
err = errors.Join(err, file.Close())
188200
}()
189201

190202
b, err := io.ReadAll(file)

0 commit comments

Comments
 (0)