Skip to content

Commit

Permalink
Merge branch 'main' into elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
yurishkuro committed Feb 27, 2024
2 parents 915e83f + 8a5f824 commit 278d49a
Show file tree
Hide file tree
Showing 18 changed files with 269 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ coverage:
project:
default:
enabled: yes
target: 95%
# Temporarily lower threshold below 95%
# Tacked in https://github.com/jaegertracing/jaeger/issues/5194
target: 94.4%
patch:
default:
enabled: yes
Expand Down
File renamed without changes.
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
23 changes: 21 additions & 2 deletions cmd/anonymizer/app/anonymizer/anonymizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package anonymizer

import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
Expand Down Expand Up @@ -57,6 +58,8 @@ type Anonymizer struct {
lock sync.Mutex
mapping mapping
options Options
cancel context.CancelFunc
wg sync.WaitGroup
}

// Options represents the various options with which the anonymizer can be configured.
Expand All @@ -70,6 +73,7 @@ type Options struct {
// New creates new Anonymizer. The mappingFile stores the mapping from original to
// obfuscated strings, in case later investigations require looking at the original traces.
func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
ctx, cancel := context.WithCancel(context.Background())
a := &Anonymizer{
mappingFile: mappingFile,
logger: logger,
Expand All @@ -78,6 +82,7 @@ func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
Operations: make(map[string]string),
},
options: options,
cancel: cancel,
}
if _, err := os.Stat(filepath.Clean(mappingFile)); err == nil {
dat, err := os.ReadFile(filepath.Clean(mappingFile))
Expand All @@ -88,14 +93,28 @@ func New(mappingFile string, options Options, logger *zap.Logger) *Anonymizer {
logger.Fatal("Cannot unmarshal previous mapping", zap.Error(err))
}
}
a.wg.Add(1)
go func() {
for range time.NewTicker(10 * time.Second).C {
a.SaveMapping()
defer a.wg.Done()
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
a.SaveMapping()
case <-ctx.Done():
return
}
}
}()
return a
}

func (a *Anonymizer) Stop() {
a.cancel()
a.wg.Wait()
}

// SaveMapping writes the mapping from original to obfuscated strings to a file.
// It is called by the anonymizer itself periodically, and should be called at
// the end of the extraction run.
Expand Down
2 changes: 2 additions & 0 deletions cmd/anonymizer/app/anonymizer/anonymizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestNew(t *testing.T) {

file, err := os.CreateTemp(tempDir, "mapping.json")
require.NoError(t, err)
defer file.Close()

_, err = file.Write([]byte(`
{
Expand All @@ -97,6 +98,7 @@ func TestNew(t *testing.T) {
require.NoError(t, err)

anonymizer := New(file.Name(), Options{}, nopLogger)
defer anonymizer.Stop()
assert.NotNil(t, anonymizer)
}

Expand Down
14 changes: 14 additions & 0 deletions cmd/anonymizer/app/writer/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package writer

import (
"testing"

"github.com/jaegertracing/jaeger/pkg/testutils"
)

func TestMain(m *testing.M) {
testutils.VerifyGoLeaks(m)
}
1 change: 1 addition & 0 deletions cmd/anonymizer/app/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,6 @@ func (w *Writer) Close() {
w.capturedFile.Close()
w.anonymizedFile.WriteString("\n]\n")
w.anonymizedFile.Close()
w.anonymizer.Stop()
w.anonymizer.SaveMapping()
}
3 changes: 2 additions & 1 deletion cmd/anonymizer/app/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func TestNew(t *testing.T) {
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
}
_, err := New(config, nopLogger)
writer, err := New(config, nopLogger)
require.NoError(t, err)
defer writer.Close()
})

t.Run("CapturedFile does not exist", func(t *testing.T) {
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
Loading

0 comments on commit 278d49a

Please sign in to comment.