Skip to content

Commit

Permalink
Add the print-config subcommand (#5200)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Resolves Issue #5168
 <!-- Example: Resolves #123 -->

## Description of the changes
- Create a print-config CLI subcommand to print the resolved
configurations in the path /cmd/internal/config. It iterates through the
settings with viper, returning all the keys that contain a value and
printing whether that value is default or not.
- Create a --all flag that when called prints all settings, whether they
have values or not.

Below is an example of the output without a all flag:
``` bash 
go run cmd/all-in-one/main.go print-config
2024/02/15 13:55:52 maxprocs: Leaving GOMAXPROCS=16: CPU quota undefined
2024/02/15 13:55:52 application version: git-commit=, git-version=, build-date=
-------------------------------------------------------------------------------
| Configuration Option Name                          Value      Source        |
-------------------------------------------------------------------------------
| admin.http.host-port                               :14269     default       |
| admin.http.tls.enabled                             false      default       |
| collector.enable-span-size-metrics                 false      default       |
| collector.grpc-server.host-port                    :14250     default       |
| collector.grpc-server.max-connection-age           0s         default       |
| collector.grpc-server.max-connection-age-grace     0s         default       |
| collector.grpc-server.max-message-size             4194304    default       |
| collector.grpc.tls.enabled                         false      default       |
| collector.http-server.host-port                    :14268     default       |
| collector.http-server.idle-timeout                 0s         default       |
| collector.http-server.read-header-timeout          2s         default       |
| collector.http-server.read-timeout                 0s         default       |
| collector.http.tls.enabled                         false      default       |
| collector.num-workers                              50         default       |
| collector.otlp.enabled                             true       default       |
| collector.otlp.grpc.max-connection-age             0s         default       |
| collector.otlp.grpc.max-connection-age-grace       0s         default       |
| collector.otlp.grpc.max-message-size               4194304    default       |
| collector.otlp.grpc.tls.enabled                    false      default       |
| collector.otlp.grpc.tls.reload-interval            0s         default       |
| collector.otlp.http.idle-timeout                   0s         default       |
| collector.otlp.http.read-header-timeout            2s         default       |
| collector.otlp.http.read-timeout                   0s         default       |
| collector.otlp.http.tls.enabled                    false      default       |
| collector.otlp.http.tls.reload-interval            0s         default       |
| collector.queue-size                               2000       default       |
| collector.queue-size-memory                        0          default       |
| collector.zipkin.keep-alive                        true       default       |
| collector.zipkin.tls.enabled                       false      default       |
| dir                                                ./         default       |
| downsampling.ratio                                 1          default       |
| format                                             md         default       |
| http-server.host-port                              :5778      default       |
| log-level                                          info       default       |
| memory.max-traces                                  0          default       |
| metrics-backend                                    prometheus default       |
| metrics-http-route                                 /metrics   default       |
| multi-tenancy.enabled                              false      default       |
| multi-tenancy.header                               x-tenant   default       |
| processor.jaeger-binary.server-host-port           :6832      default       |
| processor.jaeger-binary.server-max-packet-size     65000      default       |
| processor.jaeger-binary.server-queue-size          1000       default       |
| processor.jaeger-binary.server-socket-buffer-size  0          default       |
| processor.jaeger-binary.workers                    10         default       |
| processor.jaeger-compact.server-host-port          :6831      default       |
| processor.jaeger-compact.server-max-packet-size    65000      default       |
| processor.jaeger-compact.server-queue-size         1000       default       |
| processor.jaeger-compact.server-socket-buffer-size 0          default       |
| processor.jaeger-compact.workers                   10         default       |
| processor.zipkin-compact.server-host-port          :5775      default       |
| processor.zipkin-compact.server-max-packet-size    65000      default       |
| processor.zipkin-compact.server-queue-size         1000       default       |
| processor.zipkin-compact.server-socket-buffer-size 0          default       |
| processor.zipkin-compact.workers                   10         default       |
| query.base-path                                    /          default       |
| query.bearer-token-propagation                     false      default       |
| query.enable-tracing                               false      default       |
| query.grpc-server.host-port                        :16685     default       |
| query.grpc.tls.enabled                             false      default       |
| query.http-server.host-port                        :16686     default       |
| query.http.tls.enabled                             false      default       |
| query.log-static-assets-access                     false      default       |
| query.max-clock-skew-adjustment                    0s         default       |
| reporter.grpc.discovery.min-peers                  3          default       |
| reporter.grpc.retry.max                            3          default       |
| reporter.grpc.tls.enabled                          false      default       |
| reporter.grpc.tls.skip-host-verify                 false      default       |
| reporter.type                                      grpc       default       |
| sampling.strategies-reload-interval                0s         default       |
| span-storage.type                                  memory     user-assigned |
| status.http.host-port                              :14269     default       |
-------------------------------------------------------------------------------
```

## How was this change tested?
- I tested this function by adding some variables using the viper
library., calling the CLI command to check them and comparing them in
both key and value.


## Checklist
- [X] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [X] I have signed all commits
- [X] I have added unit tests for the new functionality
- [X] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: Guilherme Mafra da Costa <[email protected]>
Signed-off-by: Guilherme Mafra <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
gmafrac and yurishkuro authored Feb 19, 2024
1 parent cdb8b3f commit 73835c4
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/all-in-one/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/internal/docs"
"github.com/jaegertracing/jaeger/cmd/internal/env"
"github.com/jaegertracing/jaeger/cmd/internal/flags"
"github.com/jaegertracing/jaeger/cmd/internal/printconfig"
"github.com/jaegertracing/jaeger/cmd/internal/status"
queryApp "github.com/jaegertracing/jaeger/cmd/query/app"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
Expand Down Expand Up @@ -229,6 +230,7 @@ by default uses only in-memory database.`,
command.AddCommand(env.Command())
command.AddCommand(docs.Command(v))
command.AddCommand(status.Command(v, ports.CollectorAdminHTTP))
command.AddCommand(printconfig.Command(v))

config.AddFlags(
v,
Expand Down
2 changes: 2 additions & 0 deletions cmd/collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/internal/docs"
"github.com/jaegertracing/jaeger/cmd/internal/env"
cmdFlags "github.com/jaegertracing/jaeger/cmd/internal/flags"
"github.com/jaegertracing/jaeger/cmd/internal/printconfig"
"github.com/jaegertracing/jaeger/cmd/internal/status"
"github.com/jaegertracing/jaeger/internal/metrics/expvar"
"github.com/jaegertracing/jaeger/internal/metrics/fork"
Expand Down Expand Up @@ -142,6 +143,7 @@ func main() {
command.AddCommand(env.Command())
command.AddCommand(docs.Command(v))
command.AddCommand(status.Command(v, ports.CollectorAdminHTTP))
command.AddCommand(printconfig.Command(v))

config.AddFlags(
v,
Expand Down
2 changes: 2 additions & 0 deletions cmd/ingester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/internal/docs"
"github.com/jaegertracing/jaeger/cmd/internal/env"
"github.com/jaegertracing/jaeger/cmd/internal/flags"
"github.com/jaegertracing/jaeger/cmd/internal/printconfig"
"github.com/jaegertracing/jaeger/cmd/internal/status"
"github.com/jaegertracing/jaeger/pkg/config"
"github.com/jaegertracing/jaeger/pkg/metrics"
Expand Down Expand Up @@ -102,6 +103,7 @@ func main() {
command.AddCommand(env.Command())
command.AddCommand(docs.Command(v))
command.AddCommand(status.Command(v, ports.IngesterAdminHTTP))
command.AddCommand(printconfig.Command(v))

config.AddFlags(
v,
Expand Down
76 changes: 76 additions & 0 deletions cmd/internal/printconfig/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package printconfig

import (
"fmt"
"sort"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func printDivider(cmd *cobra.Command, n int) {
fmt.Fprint(cmd.OutOrStdout(), strings.Repeat("-", n), "\n")
}

func printConfigurations(cmd *cobra.Command, v *viper.Viper, includeEmpty bool) {
keys := v.AllKeys()
sort.Strings(keys)

maxKeyLength, maxValueLength := len("Configuration Option Name"), len("Value")
maxSourceLength := len("user-assigned")
for _, key := range keys {
value := v.GetString(key)
if len(key) > maxKeyLength {
maxKeyLength = len(key)
}
if len(value) > maxValueLength {
maxValueLength = len(value)
}
}
maxRowLength := maxKeyLength + maxValueLength + maxSourceLength + 6

printDivider(cmd, maxRowLength)
fmt.Fprintf(cmd.OutOrStdout(),
"| %-*s %-*s %-*s |\n",
maxKeyLength, "Configuration Option Name",
maxValueLength, "Value",
maxSourceLength, "Source")
printDivider(cmd, maxRowLength)

for _, key := range keys {
value := v.GetString(key)
source := "default"
if v.IsSet(key) {
source = "user-assigned"
}

if includeEmpty || value != "" {
fmt.Fprintf(cmd.OutOrStdout(),
"| %-*s %-*s %-*s |\n",
maxKeyLength, key,
maxValueLength, value,
maxSourceLength, source)
}
}
printDivider(cmd, maxRowLength)
}

func Command(v *viper.Viper) *cobra.Command {
allFlag := true
cmd := &cobra.Command{
Use: "print-config",
Short: "Print names and values of configuration options",
Long: "Print names and values of configuration options, distinguishing between default and user-assigned values",
RunE: func(cmd *cobra.Command, args []string) error {
printConfigurations(cmd, v, allFlag)
return nil
},
}
cmd.Flags().BoolVarP(&allFlag, "all", "a", false, "Print all configuration options including those with empty values")

return cmd
}
128 changes: 128 additions & 0 deletions cmd/internal/printconfig/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package printconfig

import (
"bytes"
"flag"
"testing"
"time"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/jaegertracing/jaeger/pkg/config"
"github.com/jaegertracing/jaeger/pkg/config/tlscfg"
"github.com/jaegertracing/jaeger/pkg/tenancy"
)

const (
testPluginBinary = "test-plugin.binary"
testPluginConfigurationFile = "test-plugin.configuration-file"
testPluginLogLevel = "test-plugin.log-level"
testRemotePrefix = "test-remote"
testRemoteServer = testRemotePrefix + ".server"
testRemoteConnectionTimeout = testRemotePrefix + ".connection-timeout"
defaultTestPluginLogLevel = "warn"
defaultTestConnectionTimeout = time.Duration(5 * time.Second)
)

func addFlags(flagSet *flag.FlagSet) {
tlscfg.ClientFlagsConfig{
Prefix: "test",
}.AddFlags(flagSet)

flagSet.String(testPluginBinary, "", "")
flagSet.String(testPluginConfigurationFile, "", "")
flagSet.String(testPluginLogLevel, defaultTestPluginLogLevel, "")
flagSet.String(testRemoteServer, "", "")
flagSet.Duration(testRemoteConnectionTimeout, defaultTestConnectionTimeout, "")
}

func setConfig(t *testing.T) *viper.Viper {
v, command := config.Viperize(addFlags, tenancy.AddFlags)
err := command.ParseFlags([]string{
"--test-plugin.binary=noop-test-plugin",
"--test-plugin.configuration-file=config.json",
"--test-plugin.log-level=debug",
"--multi-tenancy.header=x-scope-orgid",
})

require.NoError(t, err)

return v
}

func runPrintConfigCommand(v *viper.Viper, t *testing.T, allFlag bool) string {
buf := new(bytes.Buffer)
printCmd := Command(v)
printCmd.SetOut(buf)

if allFlag {
err := printCmd.Flags().Set("all", "true")
require.NoError(t, err, "printCmd.Flags() returned the error %v", err)
}

_, err := printCmd.ExecuteC()
require.NoError(t, err, "printCmd.ExecuteC() returned the error %v", err)

return buf.String()
}

func TestAllFlag(t *testing.T) {
expected := `-----------------------------------------------------------------
| Configuration Option Name Value Source |
-----------------------------------------------------------------
| multi-tenancy.enabled false default |
| multi-tenancy.header x-scope-orgid user-assigned |
| multi-tenancy.tenants default |
| test-plugin.binary noop-test-plugin user-assigned |
| test-plugin.configuration-file config.json user-assigned |
| test-plugin.log-level debug user-assigned |
| test-remote.connection-timeout 5s default |
| test-remote.server default |
| test.tls.ca default |
| test.tls.cert default |
| test.tls.enabled false default |
| test.tls.key default |
| test.tls.server-name default |
| test.tls.skip-host-verify false default |
-----------------------------------------------------------------
`

v := setConfig(t)
actual := runPrintConfigCommand(v, t, true)
assert.Equal(t, expected, actual)
}

func TestPrintConfigCommand(t *testing.T) {
expected := `-----------------------------------------------------------------
| Configuration Option Name Value Source |
-----------------------------------------------------------------
| multi-tenancy.enabled false default |
| multi-tenancy.header x-scope-orgid user-assigned |
| test-plugin.binary noop-test-plugin user-assigned |
| test-plugin.configuration-file config.json user-assigned |
| test-plugin.log-level debug user-assigned |
| test-remote.connection-timeout 5s default |
| test.tls.enabled false default |
| test.tls.skip-host-verify false default |
-----------------------------------------------------------------
`
v := setConfig(t)
actual := runPrintConfigCommand(v, t, false)
assert.Equal(t, expected, actual)
}
1 change: 0 additions & 1 deletion cmd/jaeger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func main() {
command := internal.Command()
command.AddCommand(version.Command())
command.AddCommand(docs.Command(v))

config.AddFlags(
v,
command,
Expand Down
2 changes: 2 additions & 0 deletions cmd/query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/internal/docs"
"github.com/jaegertracing/jaeger/cmd/internal/env"
"github.com/jaegertracing/jaeger/cmd/internal/flags"
"github.com/jaegertracing/jaeger/cmd/internal/printconfig"
"github.com/jaegertracing/jaeger/cmd/internal/status"
"github.com/jaegertracing/jaeger/cmd/query/app"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
Expand Down Expand Up @@ -144,6 +145,7 @@ func main() {
command.AddCommand(env.Command())
command.AddCommand(docs.Command(v))
command.AddCommand(status.Command(v, ports.QueryAdminHTTP))
command.AddCommand(printconfig.Command(v))

config.AddFlags(
v,
Expand Down
2 changes: 2 additions & 0 deletions cmd/remote-storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/internal/docs"
"github.com/jaegertracing/jaeger/cmd/internal/env"
"github.com/jaegertracing/jaeger/cmd/internal/flags"
"github.com/jaegertracing/jaeger/cmd/internal/printconfig"
"github.com/jaegertracing/jaeger/cmd/internal/status"
"github.com/jaegertracing/jaeger/cmd/remote-storage/app"
"github.com/jaegertracing/jaeger/pkg/config"
Expand Down Expand Up @@ -105,6 +106,7 @@ func main() {
command.AddCommand(env.Command())
command.AddCommand(docs.Command(v))
command.AddCommand(status.Command(v, ports.QueryAdminHTTP))
command.AddCommand(printconfig.Command(v))

config.AddFlags(
v,
Expand Down

0 comments on commit 73835c4

Please sign in to comment.