Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add ElasticSearch logs (#17394)
Vincent Huang05bd94738d9e
diff --git a/backend/plugin/db/elasticsearch/elasticsearch.go b/backend/plugin/db/elasticsearch/elasticsearch.goindex 687169a3c2..a02df758ad 100644--- a/backend/plugin/db/elasticsearch/elasticsearch.go+++ b/backend/plugin/db/elasticsearch/elasticsearch.go@@ -419,7 +419,12 @@ func (d *Driver) QueryConn(_ context.Context, _ *sql.Conn, statement string, _ drespBytes, err := io.ReadAll(resp.Body)if err != nil {- return err+ return errors.Wrap(err, "failed to read response body")+ }++ // Check HTTP status code for errors (4xx, 5xx)+ if resp.StatusCode >= 400 {+ return errors.Errorf("request failed with status code %d: %s", resp.StatusCode, string(respBytes))}// structure results.diff --git a/backend/plugin/db/elasticsearch/role.go b/backend/plugin/db/elasticsearch/role.goindex f9bd87d042..311bdef8c2 100644--- a/backend/plugin/db/elasticsearch/role.go+++ b/backend/plugin/db/elasticsearch/role.go@@ -10,6 +10,7 @@ import ("strings""github.com/elastic/go-elasticsearch/v7/esapi"+ "github.com/pkg/errors""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -31,7 +32,13 @@ func (d *Driver) getInstanceRoles() ([]*storepb.InstanceRole, error) {bytes, err = io.ReadAll(resp.Body)if err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to read OpenSearch users response body")+ }++ // Check HTTP status code+ if resp.StatusCode != http.StatusOK {+ // Include response body for debugging+ return nil, errors.Errorf("failed to get OpenSearch users: unexpected status code %d: %s", resp.StatusCode, string(bytes))}} else if d.typedClient != nil {resp, err := esapi.SecurityGetUserRequest{Pretty: true}.Do(context.Background(), d.typedClient)@@ -41,7 +48,7 @@ func (d *Driver) getInstanceRoles() ([]*storepb.InstanceRole, error) {bytes, err = readBytesAndClose(resp)if err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to get Elasticsearch users")}} else {resp, err := d.basicAuthClient.Do("GET", []byte("/_security/user"), nil)@@ -52,14 +59,25 @@ func (d *Driver) getInstanceRoles() ([]*storepb.InstanceRole, error) {bytes, err = io.ReadAll(resp.Body)if err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to read Elasticsearch users response body")+ }++ // Check HTTP status code+ if resp.StatusCode != http.StatusOK {+ // Include response body for debugging+ return nil, errors.Errorf("failed to get Elasticsearch users: unexpected status code %d: %s", resp.StatusCode, string(bytes))}}var instanceRoles []*storepb.InstanceRoleresults := map[string]UserResult{}if err := json.Unmarshal(bytes, &results); err != nil {- slog.Error("failed list es roles", log.BBError(err))+ // Include response body to help debug parsing issues+ bodyPreview := string(bytes)+ if len(bodyPreview) > 500 {+ bodyPreview = bodyPreview[:500] + "..."+ }+ slog.Error("failed to parse users response", log.BBError(err), slog.String("response", bodyPreview))return instanceRoles, nil}@@ -91,7 +109,7 @@ func (d *Driver) getUserPrivileges(usrName string) (string, error) {bytes, err := readBytesAndClose(resp)if err != nil {- return "", err+ return "", errors.Wrap(err, "failed to get user privileges")}return string(bytes), nil}diff --git a/backend/plugin/db/elasticsearch/sync.go b/backend/plugin/db/elasticsearch/sync.goindex b7c0390039..6f119d7bb7 100644--- a/backend/plugin/db/elasticsearch/sync.go+++ b/backend/plugin/db/elasticsearch/sync.go@@ -83,13 +83,24 @@ func (d *Driver) getVersion() (string, error) {bytes, err := io.ReadAll(resp.Body)if err != nil {- return "", err+ return "", errors.Wrap(err, "failed to read response body")+ }++ // Check HTTP status code+ if resp.StatusCode != http.StatusOK {+ // Include response body for debugging+ return "", errors.Errorf("unexpected status code %d: %s", resp.StatusCode, string(bytes))}var result VersionResulterr = json.Unmarshal(bytes, &result)if err != nil {- return "", err+ // Include response body to help debug parsing issues+ bodyPreview := string(bytes)+ if len(bodyPreview) > 500 {+ bodyPreview = bodyPreview[:500] + "..."+ }+ return "", errors.Wrapf(err, "failed to parse version response: %s", bodyPreview)}return result.Version.Number, nil@@ -141,12 +152,17 @@ func (d *Driver) getIndices() ([]*storepb.TableMetadata, error) {bytes, err := readBytesAndClose(res)if err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to list Elasticsearch indices")}var results []IndicesResultif err := json.Unmarshal(bytes, &results); err != nil {- return nil, err+ // Include response body to help debug parsing issues+ bodyPreview := string(bytes)+ if len(bodyPreview) > 500 {+ bodyPreview = bodyPreview[:500] + "..."+ }+ return nil, errors.Wrapf(err, "failed to parse Elasticsearch indices response: %s", bodyPreview)}for _, m := range results {@@ -180,12 +196,23 @@ func (d *Driver) getIndices() ([]*storepb.TableMetadata, error) {bytes, err := io.ReadAll(resp.Body)if err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to read indices response body")+ }++ // Check HTTP status code+ if resp.StatusCode != http.StatusOK {+ // Include response body for debugging+ return nil, errors.Errorf("failed to list indices: unexpected status code %d: %s", resp.StatusCode, string(bytes))}var results []IndicesResultif err := json.Unmarshal(bytes, &results); err != nil {- return nil, err+ // Include response body to help debug parsing issues+ bodyPreview := string(bytes)+ if len(bodyPreview) > 500 {+ bodyPreview = bodyPreview[:500] + "..."+ }+ return nil, errors.Wrapf(err, "failed to parse indices response: %s", bodyPreview)}for _, m := range results {@@ -238,13 +265,20 @@ func unitConversion(sizeWithUnit string) (int64, error) {func readBytesAndClose(anyResp any) ([]byte, error) {var body io.ReadCloser- // get body closer.+ var statusCode int+ var isError bool++ // get body closer and status code.switch resp := anyResp.(type) {case *http.Response:body = resp.Body+ statusCode = resp.StatusCode+ isError = resp.StatusCode >= 400case *esapi.Response:body = resp.Body+ statusCode = resp.StatusCode+ isError = resp.IsError()default:return nil, errors.New("not supported response type")@@ -253,10 +287,15 @@ func readBytesAndClose(anyResp any) ([]byte, error) {// read bytes.bytes, err := io.ReadAll(body)if err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to read response body")}if err := body.Close(); err != nil {- return nil, err+ return nil, errors.Wrap(err, "failed to close response body")+ }++ // Check for error status codes+ if isError {+ return bytes, errors.Errorf("request failed with status code %d: %s", statusCode, string(bytes))}return bytes, nil