Skip to content

Commit

Permalink
Adding header flag
Browse files Browse the repository at this point in the history
  • Loading branch information
krzko committed Aug 2, 2022
1 parent 9cbc94f commit d4aef93
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 4 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ USAGE:
otelgen [global options] command [command options] [arguments...]

VERSION:
v0.0.1
v0.0.2

COMMANDS:
metrics, m Generate metrics
Expand All @@ -67,6 +67,7 @@ COMMANDS:

GLOBAL OPTIONS:
--duration value, -d value duration in seconds (default: 0)
--header value additional headers in 'key=value' format (accepts multiple inputs)
--help, -h show help (default: false)
--insecure, -i whether to enable client transport security (default: false)
--log-level value log level used by the logger, one of: debug, info, warn, error (default: "info")
Expand Down Expand Up @@ -110,6 +111,15 @@ $ otelgen --otel-exporter-otlp-endpoint otelcol.foo.bar:443 --duration 30 metric
{"level":"info","ts":1658746751.791806,"caller":"cli/metrics_counter.go:79","msg":"stopping the exporter"}
```

If you need to pass in additional HTTP headers to allow for authentication to vendor backends, simply utilise the `--header key=value` flag. The unit is a slice of headers so it accepts multiple headers during invocation. Such as:

```sh
$ otelgen --otel-exporter-otlp-endpoint api.vendor.xyz:443 \
--header 'x-auth=xxxxxx' \
--header 'x-dataset=xxxxxx' \
metrics counter
```

### Traces

The `otelgen traces` command supports two types of traces, `single` and `multi`, the difference being, sometimes you just want to send a single trace to validate a configuration. **Multi** will allow you configure the `duration` and `rate`.
Expand Down Expand Up @@ -155,6 +165,15 @@ $ otelgen --otel-exporter-otlp-endpoint otelcol.foo.bar:443 --duration 10 --rate
...
```

If you need to pass in additional HTTP headers to allow for authentication to vendor backends, simply utilise the `--header key=value` flag. The unit is a slice of headers so it accepts multiple headers during invocation. Such as:

```sh
$ otelgen --otel-exporter-otlp-endpoint api.vendor.xyz:443 \
--header 'x-auth=xxxxxx' \
--header 'x-dataset=xxxxxx' \
traces single
```

## Acknowledgements

This tool was developed in a short amount of time due to the awesome idea of the following sources:
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ func getGlobalFlags() []cli.Flag {
Usage: "duration in seconds",
Value: 0,
}),
altsrc.NewStringSliceFlag(&cli.StringSliceFlag{
Name: "header",
// Aliases: []string{"h"},
Usage: "additional headers in 'key=value' format",
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "insecure",
Usage: "whether to enable client transport security",
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -62,6 +64,22 @@ func generateMetricsCounterAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_counter_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -63,6 +65,22 @@ func generateMetricsCounterObserverAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_counter_observer_advanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -62,6 +64,22 @@ func generateMetricsCounterObserverAdvancedAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_counter_with_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -63,6 +65,22 @@ func generateMetricsCounterWithLabelsAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_gauge_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -63,6 +65,22 @@ func generateMetricsGaugeObserverAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -62,6 +64,22 @@ func generateMetricsHistogramAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_up_down_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -62,6 +64,22 @@ func generateMetricsUpDownCounterAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/metrics_up_down_counter_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -63,6 +65,22 @@ func generateMetricsUpDownCounterObserverAction(c *cli.Context) error {
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithInsecure())
}

if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlpmetricgrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlpmetrichttp.WithHeaders(headers))
}

var exp *otlpmetric.Exporter
if c.String("protocol") == "http" {
logger.Info("starting HTTP exporter")
Expand Down
19 changes: 16 additions & 3 deletions internal/cli/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"errors"
"fmt"
"strings"
"time"

grpcZap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
Expand Down Expand Up @@ -76,9 +78,20 @@ func genTracesCommand() *cli.Command {
httpExpOpt = append(httpExpOpt, otlptracehttp.WithInsecure())
}

if len(tracesCfg.Headers) > 0 {
grpcExpOpt = append(grpcExpOpt, otlptracegrpc.WithHeaders(tracesCfg.Headers))
httpExpOpt = append(httpExpOpt, otlptracehttp.WithHeaders(tracesCfg.Headers))
if len(c.StringSlice("header")) > 0 {
headers := make(map[string]string)
logger.Debug("Header count", zap.Int("headers", len(c.StringSlice("header"))))
for _, h := range c.StringSlice("header") {
kv := strings.SplitN(h, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=value")
}
logger.Debug("key=value", zap.String(kv[0], kv[1]))
(headers)[kv[0]] = kv[1]

}
grpcExpOpt = append(grpcExpOpt, otlptracegrpc.WithHeaders(headers))
httpExpOpt = append(httpExpOpt, otlptracehttp.WithHeaders(headers))
}

var exp *otlptrace.Exporter
Expand Down

0 comments on commit d4aef93

Please sign in to comment.