Skip to content

Commit

Permalink
Error on scheme in http endpoint
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
mstoykov committed Aug 19, 2024
1 parent 9fc9761 commit 98031f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 8 additions & 1 deletion pkg/opentelemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/mstoykov/envconfig"
Expand Down Expand Up @@ -217,9 +218,15 @@ func (cfg Config) Validate() error {
}

if cfg.ExporterType.String == httpExporterType {
if cfg.HTTPExporterEndpoint.String == "" {
endpoint := cfg.HTTPExporterEndpoint.String
if endpoint == "" {
return errors.New("HTTP exporter endpoint is required")
}

if strings.HasPrefix(endpoint, "http://") ||
strings.HasPrefix(endpoint, "https://") {
return errors.New("HTTP exporter endpoint must only be host and port, no scheme")
}
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions pkg/opentelemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,24 @@ func TestConfig(t *testing.T) {
},

"JSON success merge": {
jsonRaw: json.RawMessage(`{"exporterType":"http","httpExporterEndpoint":"http://localhost:5566","httpExporterURLPath":"/lorem/ipsum", "exportInterval":"15ms"}`),
jsonRaw: json.RawMessage(`{"exporterType":"http","httpExporterEndpoint":"localhost:5566","httpExporterURLPath":"/lorem/ipsum", "exportInterval":"15ms"}`),
expectedConfig: Config{
ServiceName: null.StringFrom("k6"),
ServiceVersion: null.StringFrom(k6Const.Version),
ExporterType: null.StringFrom(httpExporterType),
HTTPExporterInsecure: null.NewBool(false, true),
HTTPExporterEndpoint: null.StringFrom("http://localhost:5566"),
HTTPExporterEndpoint: null.StringFrom("localhost:5566"),
HTTPExporterURLPath: null.StringFrom("/lorem/ipsum"),
GRPCExporterInsecure: null.NewBool(false, true), // default
GRPCExporterEndpoint: null.StringFrom("localhost:4317"), // default
ExportInterval: types.NullDurationFrom(15 * time.Millisecond),
FlushInterval: types.NullDurationFrom(1 * time.Second),
},
},
"no scheme in http exporter": {
jsonRaw: json.RawMessage(`{"exporterType":"http","httpExporterEndpoint":"http://localhost:5566","httpExporterURLPath":"/lorem/ipsum", "exportInterval":"15ms"}`),
err: `config: HTTP exporter endpoint must only be host and port, no scheme`,
},

"early error env": {
env: map[string]string{"K6_OTEL_GRPC_EXPORTER_ENDPOINT": "else", "K6_OTEL_EXPORT_INTERVAL": "4something"},
Expand Down

0 comments on commit 98031f1

Please sign in to comment.