diff --git a/components/operator/internal/modules/search.go b/components/operator/internal/modules/search.go index 00c9370bf8..1f15e11446 100644 --- a/components/operator/internal/modules/search.go +++ b/components/operator/internal/modules/search.go @@ -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 } diff --git a/components/search/cmd/serve.go b/components/search/cmd/serve.go index fcc1110e3e..99504f5114 100644 --- a/components/search/cmd/serve.go +++ b/components/search/cmd/serve.go @@ -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" @@ -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 { diff --git a/components/search/cmd/update_mapping.go b/components/search/cmd/update_mapping.go index 2d8bb6035e..4c29ffe6a7 100644 --- a/components/search/cmd/update_mapping.go +++ b/components/search/cmd/update_mapping.go @@ -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 { @@ -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 } diff --git a/components/search/pkg/searchengine/mapping.go b/components/search/pkg/searchengine/mapping.go index 6633f2d69d..b1f2e91fbc 100644 --- a/components/search/pkg/searchengine/mapping.go +++ b/components/search/pkg/searchengine/mapping.go @@ -5,6 +5,7 @@ import ( "context" _ "embed" "encoding/json" + "fmt" "github.com/opensearch-project/opensearch-go" "github.com/opensearch-project/opensearch-go/opensearchapi" @@ -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) {