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

Fix Elasticsearch version in ES OTEL writer #2409

Merged
merged 2 commits into from
Aug 24, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func new(ctx context.Context, config *Config, params component.ExporterCreatePar
return nil, err
}
if config.Primary.IsCreateIndexTemplates() {
spanMapping, serviceMapping := es.GetSpanServiceMappings(esCfg.GetNumShards(), esCfg.GetNumReplicas(), esCfg.GetVersion())
spanMapping, serviceMapping := es.GetSpanServiceMappings(esCfg.GetNumShards(), esCfg.GetNumReplicas(), uint(w.esClientVersion()))
if err = w.CreateTemplates(ctx, spanMapping, serviceMapping); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,7 @@ type bulkItem struct {
// isService indicates that this bulk operation is for service index
isService bool
}

func (w *esSpanWriter) esClientVersion() int {
return w.client.MajorVersion()
}
11 changes: 7 additions & 4 deletions cmd/opentelemetry/app/internal/esclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type ElasticsearchClient interface {
Search(ctx context.Context, query SearchBody, size int, indices ...string) (*SearchResponse, error)
// MultiSearch searches data via /_msearch
MultiSearch(ctx context.Context, queries []SearchBody) (*MultiSearchResponse, error)

// Major version returns major ES version
MajorVersion() int
}

// BulkResponse is a response returned by Elasticsearch Bulk API
Expand Down Expand Up @@ -172,20 +175,20 @@ func NewElasticsearchClient(params config.Configuration, logger *zap.Logger) (El
if err != nil {
return nil, err
}
if params.GetVersion() == 0 {
esVersion := int(params.GetVersion())
if esVersion == 0 {
esPing := elasticsearchPing{
username: params.Username,
password: params.Password,
roundTripper: roundTripper,
}
esVersion, err := esPing.getVersion(params.Servers[0])
esVersion, err = esPing.getVersion(params.Servers[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to check length of .Servers before dereferencing? It does appear that right now it's impossible to pass Servers with length 0 into this method, but the code that prevents that is fairly distant from this.

Might be better if this method protects itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 I am will submit a follow-up PR to fix that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
return nil, err
}
logger.Info("Elasticsearch detected", zap.Int("version", esVersion))
params.Version = uint(esVersion)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ES version is derived here in the client factory method and assigned to the config. However the config is passed by value, hence the changes are not reflected after this function. Then the wrong value of the version was used by the called to create index template.

Instead of overriding the config it is cleaner to expose the version on the client.

}
return newElasticsearchClient(int(params.Version), clientConfig{
return newElasticsearchClient(esVersion, clientConfig{
DiscoverNotesOnStartup: params.Sniffer,
Addresses: params.Servers,
Username: params.Username,
Expand Down
4 changes: 4 additions & 0 deletions cmd/opentelemetry/app/internal/esclient/es6client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,7 @@ func (es *elasticsearch6Client) MultiSearch(ctx context.Context, queries []Searc
}
return r, nil
}

func (es *elasticsearch6Client) MajorVersion() int {
return 6
}
6 changes: 6 additions & 0 deletions cmd/opentelemetry/app/internal/esclient/es6client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ func TestES6MultiSearch(t *testing.T) {
return newElasticsearch6Client(clientConfig{}, tripper)
})
}

func TestES6Version(t *testing.T) {
c, err := newElasticsearch6Client(clientConfig{}, nil)
require.NoError(t, err)
assert.Equal(t, 6, c.MajorVersion())
}
4 changes: 4 additions & 0 deletions cmd/opentelemetry/app/internal/esclient/es7client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (es *elasticsearch7Client) MultiSearch(ctx context.Context, queries []Searc
return convertMultiSearchResponse(r), nil
}

func (es *elasticsearch7Client) MajorVersion() int {
return 7
}

func convertMultiSearchResponse(response *es7multiSearchResponse) *MultiSearchResponse {
mResponse := &MultiSearchResponse{}
for _, r := range response.Responses {
Expand Down
6 changes: 6 additions & 0 deletions cmd/opentelemetry/app/internal/esclient/es7client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ func TestES7MultiSearch(t *testing.T) {
return newElasticsearch7Client(clientConfig{}, tripper)
})
}

func TestES7Version(t *testing.T) {
c, err := newElasticsearch7Client(clientConfig{}, nil)
require.NoError(t, err)
assert.Equal(t, 7, c.MajorVersion())
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,7 @@ func (m *mockClient) Search(ctx context.Context, query esclient.SearchBody, size
func (m mockClient) MultiSearch(ctx context.Context, queries []esclient.SearchBody) (*esclient.MultiSearchResponse, error) {
panic("implement me")
}

func (m *mockClient) MajorVersion() int {
panic("implement me")
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,7 @@ func (m *mockClient) Search(ctx context.Context, query esclient.SearchBody, size
func (m *mockClient) MultiSearch(ctx context.Context, queries []esclient.SearchBody) (*esclient.MultiSearchResponse, error) {
return m.multiSearchResponse, nil
}

func (m *mockClient) MajorVersion() int {
panic("implement me")
}