Skip to content

Commit

Permalink
feat: search-add-debug-log-and-fail-on-update-mapping (#875)
Browse files Browse the repository at this point in the history
Co-authored-by: David Ragot <[email protected]>
  • Loading branch information
Dav-14 and David Ragot authored Nov 17, 2023
1 parent 8ea9455 commit bdbc971
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
3 changes: 3 additions & 0 deletions components/operator/internal/modules/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func SearchEnvVars(rc ReconciliationConfig) ContainerEnv {
} else {
env = env.Append(Env("ES_INDICES", stackv1beta3.DefaultESIndex))
}
if rc.Configuration.Spec.Services.Search.Debug {
env = env.Append(Env("DEBUG", "true"))
}

return env
}
Expand Down
23 changes: 16 additions & 7 deletions components/search/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/opensearch-project/opensearch-go"
"github.com/opensearch-project/opensearch-go/opensearchtransport"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -112,17 +113,25 @@ func newOpensearchClient(openSearchServiceHost string) (*opensearch.Client, erro
InsecureSkipVerify: true,
}

config := opensearch.Config{
Addresses: []string{viper.GetString(openSearchSchemeFlag) + "://" + openSearchServiceHost},
Transport: otelhttp.NewTransport(httpTransport),
Username: viper.GetString(openSearchUsernameFlag),
Password: viper.GetString(openSearchPasswordFlag),
}

if viper.GetBool(app.DebugFlag) {
httpTransport = httpclient.NewDebugHTTPTransport(httpTransport)
config.Logger = &opensearchtransport.JSONLogger{
Output: os.Stdout,
EnableRequestBody: true,
EnableResponseBody: true,
}
} else {
config.UseResponseCheckOnly = true
}

return opensearch.NewClient(opensearch.Config{
Addresses: []string{viper.GetString(openSearchSchemeFlag) + "://" + openSearchServiceHost},
Transport: otelhttp.NewTransport(httpTransport),
Username: viper.GetString(openSearchUsernameFlag),
Password: viper.GetString(openSearchPasswordFlag),
UseResponseCheckOnly: true,
})
return opensearch.NewClient(config)
}

func opensearchClientModule(openSearchServiceHost string, loadMapping bool, esIndex string) fx.Option {
Expand Down
7 changes: 6 additions & 1 deletion components/search/cmd/update_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"github.com/formancehq/search/pkg/searchengine"
"github.com/formancehq/stack/libs/go-libs/service"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewUpdateMapping() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "update-mapping",
Short: "Update ElasticSearch mapping",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -30,4 +31,8 @@ func NewUpdateMapping() *cobra.Command {
return searchengine.UpdateMapping(cmd.Context(), client, esIndex)
},
}

cmd.Flags().Bool(service.DebugFlag, false, "debug mode")

return cmd
}
18 changes: 14 additions & 4 deletions components/search/pkg/searchengine/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
_ "embed"
"encoding/json"
"fmt"

"github.com/opensearch-project/opensearch-go"
"github.com/opensearch-project/opensearch-go/opensearchapi"
Expand All @@ -29,17 +30,26 @@ func CreateIndex(ctx context.Context, client *opensearch.Client, index string) e
}

func UpdateMapping(ctx context.Context, client *opensearch.Client, index string) error {
indexCreateBody, err := GetIndexDefinition()
updateMapping, err := json.Marshal(getMapping())
if err != nil {
return err
}

_, err = client.Indices.PutMapping(
bytes.NewReader(indexCreateBody),
res, err := client.Indices.PutMapping(
bytes.NewReader(updateMapping),
client.Indices.PutMapping.WithContext(ctx),
client.Indices.PutMapping.WithIndex(index),
)
return err

if err != nil {
return err
}

if res.IsError() {
return fmt.Errorf("request ended with status : %s", res.Status())
}

return nil
}

func GetIndexDefinition() ([]byte, error) {
Expand Down

1 comment on commit bdbc971

@vercel
Copy link

@vercel vercel bot commented on bdbc971 Nov 17, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.