Skip to content

Commit

Permalink
chore(server): replace Config fields references with method calls (#1957
Browse files Browse the repository at this point in the history
)
  • Loading branch information
schoren committed Feb 9, 2023
1 parent 36e5f08 commit 3814d3a
Show file tree
Hide file tree
Showing 24 changed files with 490 additions and 471 deletions.
6 changes: 2 additions & 4 deletions cli/go.mod
Expand Up @@ -2,22 +2,19 @@ module github.com/kubeshop/tracetest/cli

go 1.18

replace github.com/kubeshop/tracetest/server => ../server

require (
github.com/alexeyco/simpletable v1.0.0
github.com/compose-spec/compose-go v1.5.1
github.com/cucumber/ci-environment/go v0.0.0-20220915001957-711b1c82415f
github.com/denisbrodbeck/machineid v1.0.1
github.com/fluidtruck/deepcopy v1.0.0
github.com/joho/godotenv v1.3.0
github.com/kubeshop/tracetest/server v0.0.0
github.com/kubeshop/tracetest/server v0.0.0-20230208220354-63c9594b2160
github.com/pterm/pterm v0.12.46
github.com/segmentio/analytics-go/v3 v3.2.1
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.13.0
github.com/stretchr/testify v1.8.0
go.opentelemetry.io/collector v0.60.0
go.uber.org/zap v1.23.0
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -77,6 +74,7 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.opentelemetry.io/collector v0.60.0 // indirect
go.opentelemetry.io/collector/pdata v0.60.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.35.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cli/go.sum
Expand Up @@ -314,6 +314,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubeshop/tracetest/server v0.0.0-20230208220354-63c9594b2160 h1:vSWsqho5dJxAJmrgKc1RClvjD4UpP9TlUNyCkiGkOnM=
github.com/kubeshop/tracetest/server v0.0.0-20230208220354-63c9594b2160/go.mod h1:fytz7/+EWBsIBNfS7MKrdDRQ2Dq4Ck+P2Z3jzvpDFY8=
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
Expand Down
52 changes: 52 additions & 0 deletions cli/installer/config.yaml.tpl
@@ -0,0 +1,52 @@
postgresConnString: "{{ .psql }}"

poolingConfig:
maxWaitTimeForTrace: 2m
retryDelay: 3s

googleAnalytics:
enabled: {{ .analyticsEnabled }}

demo:
enabled: [{{ .enabledDemos }}]
endpoints:
pokeshopHttp: {{ .pokeshopHttp }}
pokeshopGrpc: {{ .pokeshopGrpc }}
otelFrontend: {{ .otelFrontend }}
otelProductCatalog: {{ .otelProductCatalog }}
otelCart: {{ .otelCart }}
otelCheckout: {{ .otelCheckout }}

experimentalFeatures: []
{{ if eq .installBackend "true" }}
telemetry:
dataStores:{{ if eq .backendType "jaeger" }}
jaeger:
type: jaeger
jaeger:
endpoint: {{ .backendEndpoint }}
tls:
insecure: {{ .backendInsecure }}{{ end}}{{ if eq .backendType "tempo" }}
tempo:
type: tempo
tempo:
endpoint: {{ .backendEndpoint }}
tls:
insecure: {{ .backendInsecure }}{{ end}}{{ if eq .backendType "opensearch" }}
opensearch:
type: opensearch
opensearch:
addresses: {{ .backendAddresses }}
index: {{ .backendIndex }}{{ end}}{{ if eq .backendType "signalfx" }}
signalfx:
type: signalfx
signalfx:
token: {{ .backendToken }}
realm: {{ .backendRealm }}{{ end}}{{ if eq .backendType "otlp" }}
otlp:
type: otlp{{ end}}

server:
telemetry:
dataStore: {{ .backendType }}
{{end}}
9 changes: 2 additions & 7 deletions cli/installer/configuration.go
Expand Up @@ -27,12 +27,7 @@ func (c configuration) set(key string, value interface{}) {
}

func (c configuration) get(key string) interface{} {
v, exists := c.db[key]
if !exists {
c.ui.Panic(fmt.Errorf("config key %s not exists", key))
}

return v
return c.db[key]
}

func (c configuration) Bool(key string) bool {
Expand All @@ -47,7 +42,7 @@ func (c configuration) Bool(key string) bool {
func (c configuration) String(key string) string {
s, ok := c.get(key).(string)
if !ok {
c.ui.Panic(fmt.Errorf("config key %s is not a string", key))
return ""
}

return s
Expand Down
178 changes: 32 additions & 146 deletions cli/installer/tracetest.go
@@ -1,14 +1,14 @@
package installer

import (
"bytes"
_ "embed"
"html/template"

"fmt"
"strings"

cliUI "github.com/kubeshop/tracetest/cli/ui"
serverConfig "github.com/kubeshop/tracetest/server/config"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtls"
"gopkg.in/yaml.v3"
)

func configureDemoApp(conf configuration, ui cliUI.UI) configuration {
Expand Down Expand Up @@ -72,28 +72,10 @@ func configureBackend(conf configuration, ui cliUI.UI) configuration {
return conf
}

func getTracetestConfigFileContents(psql string, ui cliUI.UI, config configuration) []byte {
sc := serverConfig.Config{
PostgresConnString: psql,
PoolingConfig: serverConfig.PoolingConfig{
MaxWaitTimeForTrace: "2m",
RetryDelay: "3s",
},
GA: serverConfig.GoogleAnalytics{
Enabled: config.Bool("tracetest.analytics"),
},
}

sc.Telemetry = telemetryConfig(ui, config)

if config.Bool("tracetest.backend.install") {
sc.Server = serverConfig.ServerConfig{
Telemetry: serverConfig.ServerTelemetryConfig{
DataStore: config.String("tracetest.backend.type"),
},
}
}
//go:embed config.yaml.tpl
var configTemplate string

func getTracetestConfigFileContents(psql string, ui cliUI.UI, config configuration) []byte {
enabledDemos := []string{}
if config.Bool("demo.enable.pokeshop") {
enabledDemos = append(enabledDemos, "pokeshop")
Expand All @@ -102,131 +84,35 @@ func getTracetestConfigFileContents(psql string, ui cliUI.UI, config configurati
enabledDemos = append(enabledDemos, "otel")
}

sc.Demo = serverConfig.Demo{
Enabled: enabledDemos,
Endpoints: serverConfig.DemoEndpoints{
PokeshopHttp: config.String("demo.endpoint.pokeshop.http"),
PokeshopGrpc: config.String("demo.endpoint.pokeshop.grpc"),
OtelFrontend: config.String("demo.endpoint.otel.frontend"),
OtelProductCatalog: config.String("demo.endpoint.otel.product_catalog"),
OtelCart: config.String("demo.endpoint.otel.cart"),
OtelCheckout: config.String("demo.endpoint.otel.checkout"),
},
vals := map[string]string{
"installBackend": fmt.Sprintf("%t", config.Bool("tracetest.backend.install")),
"psql": psql,
"analyticsEnabled": fmt.Sprintf("%t", config.Bool("tracetest.analytics")),

"enabledDemos": strings.Join(enabledDemos, ", "),
"pokeshopHttp": config.String("demo.endpoint.pokeshop.http"),
"pokeshopGrpc": config.String("demo.endpoint.pokeshop.grpc"),
"otelFrontend": config.String("demo.endpoint.otel.frontend"),
"otelProductCatalog": config.String("demo.endpoint.otel.product_catalog"),
"otelCart": config.String("demo.endpoint.otel.cart"),
"otelCheckout": config.String("demo.endpoint.otel.checkout"),

"backendType": config.String("tracetest.backend.type"),
"backendEndpoint": config.String("tracetest.backend.endpoint.query"),
"backendInsecure": config.String("tracetest.backend.tls.insecure"),
"backendAddresses": config.String("tracetest.backend.addresses"),
"backendIndex": config.String("tracetest.backend.index"),
"backendToken": config.String("tracetest.backend.token"),
"backendRealm": config.String("tracetest.backend.realm"),
}

out, err := yaml.Marshal(sc)
tpl, err := template.New("page").Parse(configTemplate)
if err != nil {
ui.Exit(fmt.Errorf("cannot marshal tracetest config file: %w", err).Error())
}

if config.Bool("tracetest.backend.install") {
out, err = fixConfigs(out)
if err != nil {
ui.Exit(fmt.Errorf("cannot fix tracertest config: %w", err).Error())
}
}

return out
}

func fixConfigs(conf []byte) ([]byte, error) {
encoded := msa{}

err := yaml.Unmarshal(conf, &encoded)
if err != nil {
return nil, fmt.Errorf("cannot decode mapstructure: %w", err)
}

ds := encoded["telemetry"].(msa)["datastores"].(msa)

var (
target msa
key string
)

if d, ok := ds["jaeger"]; ok {
target = d.(msa)["jaeger"].(msa)
key = "jaeger"
} else if d, ok := ds["tempo"]; ok {
target = d.(msa)["tempo"].(msa)
key = "tempo"
} else {
// we only need to fix tempo/jaeger
return yaml.Marshal(encoded)
ui.Panic(fmt.Errorf("cannot parse config template: %w", err))
}

target["tls"] = target["tlssetting"]
delete(target, "tlssetting")

encoded["telemetry"].(msa)["datastores"].(msa)[key].(msa)[key] = target

return yaml.Marshal(encoded)
}

func telemetryConfig(ui cliUI.UI, conf configuration) serverConfig.Telemetry {
if conf.Bool("tracetest.backend.install") {
return serverConfig.Telemetry{
DataStores: dataStoreConfig(ui, conf),
Exporters: map[string]serverConfig.TelemetryExporterOption{},
}
}

return serverConfig.Telemetry{
DataStores: map[string]serverConfig.TracingBackendDataStoreConfig{},
Exporters: map[string]serverConfig.TelemetryExporterOption{},
}
}

func dataStoreConfig(ui cliUI.UI, conf configuration) map[string]serverConfig.TracingBackendDataStoreConfig {
dstype := conf.String("tracetest.backend.type")
var c serverConfig.TracingBackendDataStoreConfig
switch dstype {
case "jaeger":
c = serverConfig.TracingBackendDataStoreConfig{
Type: dstype,
Jaeger: configgrpc.GRPCClientSettings{
Endpoint: conf.String("tracetest.backend.endpoint.query"),
TLSSetting: configtls.TLSClientSetting{
Insecure: conf.Bool("tracetest.backend.tls.insecure"),
},
},
}
case "tempo":
c = serverConfig.TracingBackendDataStoreConfig{
Type: dstype,
Tempo: configgrpc.GRPCClientSettings{
Endpoint: conf.String("tracetest.backend.endpoint"),
TLSSetting: configtls.TLSClientSetting{
Insecure: conf.Bool("tracetest.backend.tls.insecure"),
},
},
}
case "opensearch":
c = serverConfig.TracingBackendDataStoreConfig{
Type: dstype,
OpenSearch: serverConfig.ElasticSearchDataStoreConfig{
Addresses: strings.Split(conf.String("tracetest.backend.addresses"), ","),
Index: conf.String("tracetest.backend.index"),
},
}
case "signalfx":
c = serverConfig.TracingBackendDataStoreConfig{
Type: dstype,
SignalFX: serverConfig.SignalFXDataStoreConfig{
Token: conf.String("tracetest.backend.token"),
Realm: conf.String("tracetest.backend.realm"),
},
}
case "otlp":
c = serverConfig.TracingBackendDataStoreConfig{
Type: dstype,
}
default:
ui.Panic(fmt.Errorf("unsupported dataStore type %s", dstype))
}

return map[string]serverConfig.TracingBackendDataStoreConfig{
dstype: c,
}
out := &bytes.Buffer{}
tpl.Execute(out, vals)

return out.Bytes()
}
3 changes: 1 addition & 2 deletions go.work.sum
Expand Up @@ -16,7 +16,6 @@ github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8V
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
Expand Down Expand Up @@ -61,7 +60,6 @@ github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJ
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/prometheus/statsd_exporter v0.22.7/go.mod h1:N/TevpjkIh9ccs6nuzY3jQn9dFqnUakOjnEuMPJJJnI=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/shirou/gopsutil/v3 v3.22.8/go.mod h1:s648gW4IywYzUfE/KjXxUsqrqx/T2xO5VqOXxONeRfI=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/stvp/go-udp-testing v0.0.0-20201019212854-469649b16807/go.mod h1:7jxmlfBCDBXRzr0eAQJ48XC1hBu1np4CS5+cHEYfwpc=
Expand All @@ -80,6 +78,7 @@ go.opentelemetry.io/contrib/zpages v0.34.0/go.mod h1:zuVCe4eoOREH+liRJLCtGITqL3N
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
go.opentelemetry.io/otel/exporters/otlp v0.20.0 h1:PTNgq9MRmQqqJY0REVbZFvwkYOA85vbdQU/nVfxDyqg=
go.opentelemetry.io/otel/exporters/prometheus v0.31.0/go.mod h1:QarXIB8L79IwIPoNgG3A6zNvBgVmcppeFogV1d8612s=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.3.0 h1:Kte45gGM12Ks0pZng7Pi+IFlbbeY287ZpGX0s0G9al8=
go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A=
go.opentelemetry.io/otel/sdk v1.9.0/go.mod h1:AEZc8nt5bd2F7BC24J5R0mrjYnpEgYHyTcM/vrSple4=
go.opentelemetry.io/otel/sdk/metric v0.31.0/go.mod h1:fl0SmNnX9mN9xgU6OLYLMBMrNAsaZQi7qBwprwO3abk=
Expand Down

0 comments on commit 3814d3a

Please sign in to comment.