Skip to content

Commit

Permalink
jaegertracing#2048[WIP] - Replace external dependency for rendering t…
Browse files Browse the repository at this point in the history
…emplates with text/template and golang util & Implement feedback on tests

Signed-off-by: santosh <bsantosh@thoughtworks.com>
  • Loading branch information
bhiravabhatla committed Jan 25, 2021
1 parent 667a511 commit 3696256
Show file tree
Hide file tree
Showing 25 changed files with 608 additions and 193 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ test-compile-es-scripts:

.PHONY: index-cleaner-integration-test
index-cleaner-integration-test: docker-images-elastic
# Expire tests results for storage integration tests since the environment might change
# Expire test results for storage integration tests since the environment might change
# even though the code remains the same.
go clean -testcache
bash -c "set -e; set -o pipefail; $(GOTEST) -tags index_cleaner $(STORAGE_PKGS) | $(COLORIZE)"

.PHONY: index-rollover-integration-test
index-rollover-integration-test: docker-images-elastic
# Expire tests results for storage integration tests since the environment might change
# Expire test results for storage integration tests since the environment might change
# even though the code remains the same.
go clean -testcache
bash -c "set -e; set -o pipefail; $(GOTEST) -tags index_rollover $(STORAGE_PKGS) | $(COLORIZE)"
Expand Down Expand Up @@ -225,6 +225,10 @@ 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: docker-hotrod
docker-hotrod:
GOOS=linux $(MAKE) build-examples
Expand Down Expand Up @@ -344,7 +348,7 @@ docker-images-cassandra:
@echo "Finished building jaeger-cassandra-schema =============="

.PHONY: docker-images-elastic
docker-images-elastic:
docker-images-elastic: build-templateloader
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
2 changes: 0 additions & 2 deletions cmd/opentelemetry/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/flosch/pongo2/v4 v4.0.1 h1:l/49mdkeTYSHzH9N4Iwjs0yMqELMKCsYv5PzTm37Gio=
github.com/flosch/pongo2/v4 v4.0.1/go.mod h1:B5ObFANs/36VwxxlgKpdchIJHMvHB562PW+BWPhwZD8=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
Expand Down
82 changes: 82 additions & 0 deletions cmd/templateloader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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 main

import (
"flag"
"strconv"

"go.uber.org/zap"

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

var logger, _ = zap.NewDevelopment()
var fixMappingFunc = es.FixMapping
var loadMappingFunc = es.LoadMapping
var mapping string
var err error

func main() {

mappingName := flag.String("mapping", "", "Pass name of the mapping to parse - should be either jaeger-service or jaeger-span")
esVersion := flag.Int64("esVersion", 7, "Version of ES")
shards := flag.Int64("shards", 3, "Number of shards")
replicas := flag.Int64("replicas", 3, "Number of replicas")
esPrefix := flag.String("esPrefix", "", "Prefix for ES indices")
useILM := flag.String("useILM", "false", "Set to true to enable ILM")
flag.Parse()
if !isValidOption(*mappingName) {
logger.Fatal("please pass either 'jaeger-service' or 'jaeger-span' as argument")
}
parsedMapping, err := getMappingAsString(*mappingName, *esPrefix, *useILM, *esVersion, *shards, *replicas)
if err != nil {
logger.Fatal(err.Error())
}
print(parsedMapping)

}

func getMappingAsString(mappingName, esPrefix, useILM string, esVersion, shards, replicas int64) (string, error) {

if esVersion == 7 {
enableILM, err := strconv.ParseBool(useILM)
if err != nil {
return "", err
}
mapping, err = fixMappingFunc(esTemplate.TextTemplateBuilder{}, loadMappingFunc("/"+mappingName+"-7.json"), shards, replicas, esPrefix, enableILM)
if err != nil {
return "", err
}
} else {
mapping, err = fixMappingFunc(esTemplate.TextTemplateBuilder{}, loadMappingFunc("/"+mappingName+".json"), shards, 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
}
108 changes: 108 additions & 0 deletions cmd/templateloader/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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 main

import (
"errors"
"testing"

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

func TestIsValidOption(t *testing.T) {
tests := []struct {
name string
arg string
expectedValue bool
}{{name: "span mapping", arg: "jaeger-span", expectedValue: true},
{name: "service mapping", arg: "jaeger-service", expectedValue: true},
{name: "Invalid mapping", arg: "dependency-service", expectedValue: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := isValidOption(test.arg); got != test.expectedValue {
t.Errorf("isValidOption() = %v, want %v", got, test.expectedValue)
}
})
}
}

func Test_getMappingAsString(t *testing.T) {
type args struct {
mappingName string
esPrefix string
useILM string
esVersion int64
shards int64
replicas int64
}
tests := []struct {
name string
args args
fixMappingFunc func(esTemplate.TemplateBuilder, string, int64, int64, string, bool) (string, error)
want string
wantErr bool
}{
{name: "ES version 7", args: args{"jaeger-span", "test", "true", 7, 5, 5},
fixMappingFunc: func(esTemplate.TemplateBuilder, string, int64, int64, string, bool) (string, error) {
return "ES version 7", nil
},
want: "ES version 7",
wantErr: false,
},
{name: "ES version 6", args: args{"jaeger-service", "test", "false", 6, 5, 5},
fixMappingFunc: func(esTemplate.TemplateBuilder, string, int64, int64, string, bool) (string, error) {
return "ES version 6", nil
},
want: "ES version 6",
wantErr: false,
},
{name: "Parse Error version 6", args: args{"jaeger-service", "test", "false", 6, 5, 5},
fixMappingFunc: func(esTemplate.TemplateBuilder, string, int64, int64, string, bool) (string, error) {
return "", errors.New("parse error")
},
want: "",
wantErr: true,
}, {name: "Parse Error version 7", args: args{"jaeger-service", "test", "false", 7, 5, 5},
fixMappingFunc: func(esTemplate.TemplateBuilder, string, int64, int64, string, bool) (string, error) {
return "", errors.New("parse error")
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

oldFixMappingFunc := fixMappingFunc
oldLoadMappingFunc := loadMappingFunc
defer func() {
fixMappingFunc = oldFixMappingFunc
loadMappingFunc = oldLoadMappingFunc
}()

fixMappingFunc = tt.fixMappingFunc
loadMappingFunc = func(string) string { return "test" }
got, err := getMappingAsString(tt.args.mappingName, tt.args.esPrefix, tt.args.useILM, tt.args.esVersion, tt.args.shards, tt.args.replicas)
if (err != nil) != tt.wantErr {
t.Errorf("getMappingAsString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getMappingAsString() got = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions examples/hotrod/services/frontend/gen_assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/eapache/go-resiliency v1.2.0 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/flosch/pongo2/v4 v4.0.1
github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/frankban/quicktest v1.7.3 // indirect
github.com/fsnotify/fsnotify v1.4.7
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/flosch/pongo2/v4 v4.0.1 h1:l/49mdkeTYSHzH9N4Iwjs0yMqELMKCsYv5PzTm37Gio=
github.com/flosch/pongo2/v4 v4.0.1/go.mod h1:B5ObFANs/36VwxxlgKpdchIJHMvHB562PW+BWPhwZD8=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand Down
28 changes: 11 additions & 17 deletions pkg/es/mocks/TemplateApplier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/es/mocks/TemplateBuilder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3696256

Please sign in to comment.