Skip to content

Commit

Permalink
Add error source, remove some impossible errors (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
njvrzm authored Dec 3, 2024
1 parent a011656 commit 288643e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 92 deletions.
14 changes: 4 additions & 10 deletions pkg/opensearch/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func NewClient(ctx context.Context, ds *backend.DataSourceInstanceSettings, http

version, err := ExtractVersion(jsonData.Get("version"))
if err != nil {
return nil, fmt.Errorf("version is required, err=%v", err)
return nil, backend.DownstreamError(fmt.Errorf("version is required, err=%v", err))
}

flavor := jsonData.Get("flavor").MustString(string(OpenSearch))

timeField, err := jsonData.Get("timeField").String()
if err != nil {
return nil, fmt.Errorf("time field name is required, err=%v", err)
return nil, backend.DownstreamError(fmt.Errorf("time field name is required, err=%v", err))
}

logLevelField := jsonData.Get("logLevelField").MustString()
Expand All @@ -133,15 +133,9 @@ func NewClient(ctx context.Context, ds *backend.DataSourceInstanceSettings, http
return nil, err
}

indices, err := ip.GetIndices(timeRange)
if err != nil {
return nil, err
}
indices := ip.GetIndices(timeRange)

index, err := ip.GetPPLIndex()
if err != nil {
return nil, err
}
index := ip.GetPPLIndex()

clientLog.Info("Creating new client", "version", version.String(), "timeField", timeField, "indices", strings.Join(indices, ", "), "PPL index", index)

Expand Down
20 changes: 10 additions & 10 deletions pkg/opensearch/client/index_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (
)

type indexPattern interface {
GetIndices(timeRange *backend.TimeRange) ([]string, error)
GetPPLIndex() (string, error)
GetIndices(timeRange *backend.TimeRange) []string
GetPPLIndex() string
}

func NewIndexPattern(interval string, pattern string) (indexPattern, error) {
Expand All @@ -35,13 +35,13 @@ type staticIndexPattern struct {
indexName string
}

func (ip *staticIndexPattern) GetIndices(timeRange *backend.TimeRange) ([]string, error) {
return []string{ip.indexName}, nil
func (ip *staticIndexPattern) GetIndices(_ *backend.TimeRange) []string {
return []string{ip.indexName}
}

// PPL static index pattern returns the indexName string
func (ip *staticIndexPattern) GetPPLIndex() (string, error) {
return ip.indexName, nil
func (ip *staticIndexPattern) GetPPLIndex() string {
return ip.indexName
}

type intervalGenerator interface {
Expand Down Expand Up @@ -79,7 +79,7 @@ func newDynamicIndexPattern(interval, pattern string) (*dynamicIndexPattern, err
}, nil
}

func (ip *dynamicIndexPattern) GetIndices(timeRange *backend.TimeRange) ([]string, error) {
func (ip *dynamicIndexPattern) GetIndices(timeRange *backend.TimeRange) []string {
from := timeRange.From
to := timeRange.To
intervals := ip.intervalGenerator.Generate(from, to)
Expand All @@ -89,13 +89,13 @@ func (ip *dynamicIndexPattern) GetIndices(timeRange *backend.TimeRange) ([]strin
indices = append(indices, formatDate(t, ip.pattern))
}

return indices, nil
return indices
}

// PPL currently does not support multi-indexing through lists, so a wildcard
// pattern is used to match all patterns and relies on the time range filter
// to filter out the incorrect indices.
func (ip *dynamicIndexPattern) GetPPLIndex() (string, error) {
func (ip *dynamicIndexPattern) GetPPLIndex() string {
index := ""

if strings.HasPrefix(ip.pattern, "[") {
Expand All @@ -105,7 +105,7 @@ func (ip *dynamicIndexPattern) GetPPLIndex() (string, error) {
parts := strings.Split(strings.TrimRight(ip.pattern, "]"), "[")
index = "*" + parts[1]
}
return index, nil
return index
}

type hourlyInterval struct{}
Expand Down
Loading

0 comments on commit 288643e

Please sign in to comment.