Skip to content

Commit

Permalink
jaegertracing#2048[WIP] - Implement feedback on CLI - use cobra & uni…
Browse files Browse the repository at this point in the history
…t tests

Signed-off-by: santosh <bsantosh@thoughtworks.com>
  • Loading branch information
bhiravabhatla committed Jan 25, 2021
1 parent 1a888aa commit 9bec1a4
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 166 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,13 @@ build-tracegen:
build-anonymizer:
$(GOBUILD) -o ./cmd/anonymizer/anonymizer-$(GOOS)-$(GOARCH) ./cmd/anonymizer/main.go

.PHONY: build-templateloader
build-templateloader:
GOOS=linux GOARCH=amd64 $(GOBUILD) -o ./plugin/storage/es/templateloader ./cmd/templateloader/main.go
.PHONY: build-templatizer
build-templatizer:
$(GOBUILD) -o ./plugin/storage/es/templatizer-$(GOOS)-$(GOARCH) ./cmd/templatizer/main.go

.PHONY: build-templatizer-linux
build-templatizer-linux:
GOOS=linux GOARCH=amd64 $(GOBUILD) -o ./plugin/storage/es/templatizer ./cmd/templatizer/main.go

.PHONY: docker-hotrod
docker-hotrod:
Expand Down Expand Up @@ -348,7 +352,7 @@ docker-images-cassandra:
@echo "Finished building jaeger-cassandra-schema =============="

.PHONY: docker-images-elastic
docker-images-elastic: build-templateloader
docker-images-elastic: build-templatizer-linux
docker build -t $(DOCKER_NAMESPACE)/jaeger-es-index-cleaner:${DOCKER_TAG} plugin/storage/es
docker build -t $(DOCKER_NAMESPACE)/jaeger-es-rollover:${DOCKER_TAG} plugin/storage/es -f plugin/storage/es/Dockerfile.rollover
@echo "Finished building jaeger-es-indices-clean =============="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter/exporterhelper"

esTemplate "github.com/jaegertracing/jaeger/pkg/es"
"github.com/jaegertracing/jaeger/plugin/storage/es"
)

Expand All @@ -31,8 +32,8 @@ func newExporter(ctx context.Context, config *Config, params component.ExporterC
return nil, err
}
if config.Primary.IsCreateIndexTemplates() {
spanMapping, serviceMapping, e := es.GetSpanServiceMappings(esCfg.GetNumShards(), esCfg.GetNumReplicas(), uint(w.esClientVersion()), esCfg.GetIndexPrefix(), esCfg.GetUseILM())
if e != nil {
spanMapping, serviceMapping, err := es.GetSpanServiceMappings(esTemplate.TextTemplateBuilder{}, esCfg.GetNumShards(), esCfg.GetNumReplicas(), uint(w.esClientVersion()), esCfg.GetIndexPrefix(), esCfg.GetUseILM())
if err != nil {
return nil, err
}
if err = w.CreateTemplates(ctx, spanMapping, serviceMapping); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/opentelemetry/app/internal/esclient"
"github.com/jaegertracing/jaeger/cmd/opentelemetry/app/internal/reader/es/esdependencyreader"
"github.com/jaegertracing/jaeger/cmd/opentelemetry/app/internal/reader/es/esspanreader"
esTemplate "github.com/jaegertracing/jaeger/pkg/es"
"github.com/jaegertracing/jaeger/pkg/es/config"
"github.com/jaegertracing/jaeger/pkg/testutils"
"github.com/jaegertracing/jaeger/plugin/storage/es"
Expand Down Expand Up @@ -104,7 +105,7 @@ func (s *IntegrationTest) initSpanstore(allTagsAsFields bool) error {
return err
}
esVersion := uint(w.esClientVersion())
spanMapping, serviceMapping, err := es.GetSpanServiceMappings(numShards, numReplicas, esVersion, "", false)
spanMapping, serviceMapping, err := es.GetSpanServiceMappings(esTemplate.TextTemplateBuilder{}, numShards, numReplicas, esVersion, "", false)
if err != nil {
return err
}
Expand Down
82 changes: 0 additions & 82 deletions cmd/templateloader/main.go

This file was deleted.

81 changes: 81 additions & 0 deletions cmd/templatizer/app/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// 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 app

import (
"github.com/spf13/cobra"
)

// Options represent configurable parameters for jaeger-templatizer
type Options struct {
Mapping string
EsVersion int64
Shards int64
Replicas int64
EsPrefix string
UseILM string // using string as util is being used in python and using bool leads to type issues.
}

const (
mappingFlag = "mapping"
esVersionFlag = "esVersion"
shardsFlag = "shards"
replicasFlag = "replicas"
esPrefixFlag = "esPrefix"
useILMFlag = "useILM"
)

// AddFlags adds flags for templatizer main program
func (o *Options) AddFlags(command *cobra.Command) {
command.Flags().StringVarP(
&o.Mapping,
mappingFlag,
"m",
"",
"Pass either jaeger-span or jaeger-service")
command.Flags().Int64VarP(
&o.EsVersion,
esVersionFlag,
"v",
7,
"the major Elasticsearch version")
command.Flags().Int64VarP(
&o.Shards,
shardsFlag,
"s",
5,
"the number of shards per index in Elasticsearch")
command.Flags().Int64VarP(
&o.Replicas,
replicasFlag,
"r",
1,
"the number of replicas per index in Elasticsearch")
command.Flags().StringVarP(
&o.EsPrefix,
esPrefixFlag,
"e",
"",
"specifies index prefix")
command.Flags().StringVarP(
&o.UseILM,
useILMFlag,
"u",
"false",
"set to true to use ILM for managing lifecycle of jaeger indices")

// mark mapping flag as mandatory
command.MarkFlagRequired(mappingFlag)
}
57 changes: 57 additions & 0 deletions cmd/templatizer/app/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// 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 app

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestOptionsWithDefaultFlags(t *testing.T) {
o := Options{}
c := cobra.Command{}
o.AddFlags(&c)

assert.Equal(t, "", o.Mapping)
assert.Equal(t, int64(7), o.EsVersion)
assert.Equal(t, int64(5), o.Shards)
assert.Equal(t, int64(1), o.Replicas)
assert.Equal(t, "", o.EsPrefix)
assert.Equal(t, "false", o.UseILM)
}

func TestOptionsWithFlags(t *testing.T) {
o := Options{}
c := cobra.Command{}

o.AddFlags(&c)
c.ParseFlags([]string{
"--mapping=jaeger-span",
"--esVersion=6",
"--shards=5",
"--replicas=1",
"--esPrefix=test",
"--useILM=true",
})

assert.Equal(t, "jaeger-span", o.Mapping)
assert.Equal(t, int64(6), o.EsVersion)
assert.Equal(t, int64(5), o.Shards)
assert.Equal(t, int64(1), o.Replicas)
assert.Equal(t, "test", o.EsPrefix)
assert.Equal(t, "true", o.UseILM)
}
59 changes: 59 additions & 0 deletions cmd/templatizer/app/renderer/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2021 The Jaeger Authors.
//
// 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 renderer

import (
"strconv"

"github.com/jaegertracing/jaeger/cmd/templatizer/app"
esTemplate "github.com/jaegertracing/jaeger/pkg/es"
"github.com/jaegertracing/jaeger/plugin/storage/es"
)

var fixMappingFunc = es.FixMapping
var loadMappingFunc = es.LoadMapping
var mapping string
var err error

func GetMappingAsString(opt *app.Options) (string, error) {

if opt.EsVersion == 7 {
enableILM, err := strconv.ParseBool(opt.UseILM)
if err != nil {
return "", err
}
mapping, err = fixMappingFunc(esTemplate.TextTemplateBuilder{}, loadMappingFunc("/"+opt.Mapping+"-7.json"), opt.Shards, opt.Replicas, opt.EsPrefix, enableILM)
if err != nil {
return "", err
}
} else {
mapping, err = fixMappingFunc(esTemplate.TextTemplateBuilder{}, loadMappingFunc("/"+opt.Mapping+".json"), opt.Shards, opt.Replicas, "", false)
if err != nil {
return "", err
}
}
return mapping, nil

}

func IsValidOption(val string) bool {
allowedValues := []string{"jaeger-span", "jaeger-service"}
for _, value := range allowedValues {
if val == value {
return true
}
}
return false
}
Loading

0 comments on commit 9bec1a4

Please sign in to comment.