Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add esmapping-generator into jaeger binary #6327

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/esmapping-generator/generator/esmapping_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2025 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package generator

import (
"fmt"

"github.com/jaegertracing/jaeger/cmd/esmapping-generator/app"
"github.com/jaegertracing/jaeger/cmd/esmapping-generator/app/renderer"
"github.com/jaegertracing/jaeger/pkg/es"
"github.com/jaegertracing/jaeger/plugin/storage/es/mappings"
)

func GenerateMappings(options app.Options) (string, error) {
if _, err := mappings.MappingTypeFromString(options.Mapping); err != nil {
return "", fmt.Errorf("invalid mapping type '%s': please pass either 'jaeger-service' or 'jaeger-span' as the mapping type %w", options.Mapping, err)
}

parsedMapping, err := renderer.GetMappingAsString(es.TextTemplateBuilder{}, &options)
if err != nil {
return "", fmt.Errorf("failed to render mapping to string: %w", err)
}

Check warning on line 23 in cmd/esmapping-generator/generator/esmapping_generator.go

View check run for this annotation

Codecov / codecov/patch

cmd/esmapping-generator/generator/esmapping_generator.go#L22-L23

Added lines #L22 - L23 were not covered by tests

return parsedMapping, nil
}
87 changes: 87 additions & 0 deletions cmd/esmapping-generator/generator/esmapping_generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2025 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package generator

import (
"encoding/json"
"testing"

"github.com/crossdock/crossdock-go/assert"
"github.com/crossdock/crossdock-go/require"

"github.com/jaegertracing/jaeger/cmd/esmapping-generator/app"
"github.com/jaegertracing/jaeger/pkg/testutils"
)

func TestGenerateMappings(t *testing.T) {
tests := []struct {
name string
options app.Options
expectErr bool
}{
{
name: "valid jaeger-span mapping",
options: app.Options{
Mapping: "jaeger-span",
EsVersion: 7,
Shards: 5,
Replicas: 1,
IndexPrefix: "jaeger-index",
UseILM: "false",
ILMPolicyName: "jaeger-ilm-policy",
},
expectErr: false,
},
{
name: "valid jaeger-service mapping",
options: app.Options{
Mapping: "jaeger-service",
EsVersion: 7,
Shards: 5,
Replicas: 1,
IndexPrefix: "jaeger-service-index",
UseILM: "true",
ILMPolicyName: "service-ilm-policy",
},
expectErr: false,
},
{
name: "invalid mapping type",
options: app.Options{
Mapping: "invalid-mapping",
},
expectErr: true,
},
{
name: "missing mapping flag",
options: app.Options{
Mapping: "",
},
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := GenerateMappings(tt.options)
if tt.expectErr {
require.Error(t, err, "Expected an error")
} else {
require.NoError(t, err, "Did not expect an error")

var parsed map[string]interface{}
err = json.Unmarshal([]byte(result), &parsed)
require.NoError(t, err, "Expected valid JSON output")

assert.NotEmpty(t, parsed["index_patterns"], "Expected index_patterns to be present")
assert.NotEmpty(t, parsed["mappings"], "Expected mappings to be present")
assert.NotEmpty(t, parsed["settings"], "Expected settings to be present")
}
})
}
}

func TestMain(m *testing.M) {
testutils.VerifyGoLeaks(m)
}
34 changes: 4 additions & 30 deletions cmd/esmapping-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,15 @@ import (
"fmt"
"os"

"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/cmd/esmapping-generator/app"
"github.com/jaegertracing/jaeger/cmd/esmapping-generator/app/renderer"
"github.com/jaegertracing/jaeger/pkg/es"
"github.com/jaegertracing/jaeger/internal/storage/elasticsearch/mapping"
"github.com/jaegertracing/jaeger/pkg/version"
"github.com/jaegertracing/jaeger/plugin/storage/es/mappings"
)

func main() {
logger, _ := zap.NewDevelopment()
options := app.Options{}
command := &cobra.Command{
Use: "jaeger-esmapping-generator",
Short: "Jaeger esmapping-generator prints rendered mappings as string",
Long: `Jaeger esmapping-generator renders passed templates with provided values and prints rendered output to stdout`,
Run: func(_ *cobra.Command, _ /* args */ []string) {
if _, err := mappings.MappingTypeFromString(options.Mapping); err != nil {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
logger.Fatal("please pass either 'jaeger-service' or 'jaeger-span' as argument")
}

parsedMapping, err := renderer.GetMappingAsString(es.TextTemplateBuilder{}, &options)
if err != nil {
logger.Fatal(err.Error())
}
print(parsedMapping)
},
}

options.AddFlags(command)

command.AddCommand(version.Command())
esmappingsCmd := mapping.Command()
esmappingsCmd.AddCommand(version.Command())

if err := command.Execute(); err != nil {
if err := esmappingsCmd.Execute(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/jaeger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/jaegertracing/jaeger/cmd/internal/docs"
"github.com/jaegertracing/jaeger/cmd/jaeger/internal"
"github.com/jaegertracing/jaeger/internal/storage/elasticsearch/mapping"
"github.com/jaegertracing/jaeger/pkg/config"
"github.com/jaegertracing/jaeger/pkg/version"
)
Expand All @@ -19,6 +20,7 @@ func main() {
command := internal.Command()
command.AddCommand(version.Command())
command.AddCommand(docs.Command(v))
command.AddCommand(mapping.Command())
config.AddFlags(
v,
command,
Expand Down
33 changes: 33 additions & 0 deletions internal/storage/elasticsearch/mapping/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2025 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package mapping

import (
"fmt"
"log"

"github.com/spf13/cobra"

"github.com/jaegertracing/jaeger/cmd/esmapping-generator/app"
"github.com/jaegertracing/jaeger/cmd/esmapping-generator/generator"
)

func Command() *cobra.Command {
options := app.Options{}
command := &cobra.Command{
Use: "elasticsearch-mappings",
Short: "Jaeger esmapping-generator prints rendered mappings as string",
Long: "Jaeger esmapping-generator renders passed templates with provided values and prints rendered output to stdout",
Run: func(_ *cobra.Command, _ /* args */ []string) {
result, err := generator.GenerateMappings(options)
if err != nil {
log.Fatalf("Error generating mappings: %v", err)
}

Check warning on line 26 in internal/storage/elasticsearch/mapping/command.go

View check run for this annotation

Codecov / codecov/patch

internal/storage/elasticsearch/mapping/command.go#L25-L26

Added lines #L25 - L26 were not covered by tests
fmt.Println(result)
},
}
options.AddFlags(command)

return command
}
51 changes: 51 additions & 0 deletions internal/storage/elasticsearch/mapping/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2025 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package mapping

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"

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

func TestCommandExecute(t *testing.T) {
cmd := Command()

// TempFile to capture output
tempFile, err := os.CreateTemp("", "command-output-*.txt")
require.NoError(t, err)
defer os.Remove(tempFile.Name())

// Redirect stdout to the TempFile
oldStdout := os.Stdout
os.Stdout = tempFile
defer func() { os.Stdout = oldStdout }()

err = cmd.ParseFlags([]string{
"--mapping=jaeger-span",
"--es-version=7",
"--shards=5",
"--replicas=1",
"--index-prefix=jaeger-index",
"--use-ilm=false",
"--ilm-policy-name=jaeger-ilm-policy",
})
require.NoError(t, err)
require.NoError(t, cmd.Execute())

output, err := os.ReadFile(tempFile.Name())
require.NoError(t, err)

var jsonOutput map[string]any
err = json.Unmarshal(output, &jsonOutput)
require.NoError(t, err, "Output should be valid JSON")
}

func TestMain(m *testing.M) {
testutils.VerifyGoLeaks(m)
}
Loading