(chore): replace ioutil with io and os for file operations (#3761)

Switched all file operations from ioutil package to io and os packages due to the deprecation of ioutil in Go 1.16. This change contributes to the maintainability of the codebase, ensuring it's up-to-date with the current standard library. Additionally, implemented usage of filepath.Join for platform-independent file paths.
This commit is contained in:
Swapnil Nakade 2023-10-19 14:16:20 +05:30 committed by GitHub
parent e3d08a4275
commit 6b2427f1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 59 additions and 50 deletions

View File

@ -5,22 +5,23 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"github.com/gorilla/mux"
"go.uber.org/zap"
"go.signoz.io/signoz/ee/query-service/constants"
"go.signoz.io/signoz/ee/query-service/model"
"go.signoz.io/signoz/pkg/query-service/auth"
baseauth "go.signoz.io/signoz/pkg/query-service/auth"
basemodel "go.signoz.io/signoz/pkg/query-service/model"
"go.uber.org/zap"
)
func parseRequest(r *http.Request, req interface{}) error {
defer r.Body.Close()
requestBody, err := ioutil.ReadAll(r.Body)
requestBody, err := io.ReadAll(r.Body)
if err != nil {
return err
}
@ -71,7 +72,7 @@ func (ah *APIHandler) registerUser(w http.ResponseWriter, r *http.Request) {
var req *baseauth.RegisterRequest
defer r.Body.Close()
requestBody, err := ioutil.ReadAll(r.Body)
requestBody, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("received no input in api\n", err)
RespondError(w, model.BadRequest(err), nil)

View File

@ -6,13 +6,13 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.signoz.io/signoz/ee/query-service/constants"
"go.signoz.io/signoz/ee/query-service/model"
"go.uber.org/zap"
)
var C *Client
@ -51,7 +51,7 @@ func ActivateLicense(key, siteId string) (*ActivationResponse, *model.ApiError)
return nil, model.BadRequest(fmt.Errorf("unable to connect with license.signoz.io, please check your network connection"))
}
httpBody, err := ioutil.ReadAll(httpResponse.Body)
httpBody, err := io.ReadAll(httpResponse.Body)
if err != nil {
zap.S().Errorf("failed to read activation response from license.signoz.io", err)
return nil, model.BadRequest(fmt.Errorf("failed to read activation response from license.signoz.io"))
@ -91,7 +91,7 @@ func ValidateLicense(activationId string) (*ActivationResponse, *model.ApiError)
return nil, model.BadRequest(errors.Wrap(err, "unable to connect with license.signoz.io, please check your network connection"))
}
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, model.BadRequest(errors.Wrap(err, "failed to read validation response from license.signoz.io"))
}

View File

@ -5,10 +5,9 @@ import (
"context"
"database/sql"
"encoding/json"
"math"
"fmt"
"io/ioutil"
"io"
"math"
"math/rand"
"net/http"
"os"
@ -42,6 +41,8 @@ import (
"github.com/jmoiron/sqlx"
promModel "github.com/prometheus/common/model"
"go.uber.org/zap"
"go.signoz.io/signoz/pkg/query-service/app/logs"
"go.signoz.io/signoz/pkg/query-service/app/services"
"go.signoz.io/signoz/pkg/query-service/constants"
@ -51,7 +52,6 @@ import (
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
"go.signoz.io/signoz/pkg/query-service/telemetry"
"go.signoz.io/signoz/pkg/query-service/utils"
"go.uber.org/zap"
)
const (
@ -329,15 +329,15 @@ func (r *ClickHouseReader) Start(readerReady chan bool) {
// call query service to do this
// channels, apiErrorObj := r.GetChannels()
//if apiErrorObj != nil {
// if apiErrorObj != nil {
// zap.S().Errorf("Not able to read channels from DB")
//}
//for _, channel := range *channels {
//apiErrorObj = r.LoadChannel(&channel)
//if apiErrorObj != nil {
// }
// for _, channel := range *channels {
// apiErrorObj = r.LoadChannel(&channel)
// if apiErrorObj != nil {
// zap.S().Errorf("Not able to load channel with id=%d loaded from DB", channel.Id, channel.Data)
//}
//}
// }
// }
<-cancel
@ -426,7 +426,7 @@ func (r *ClickHouseReader) LoadChannel(channel *model.ChannelItem) *model.ApiErr
return &model.ApiError{Typ: model.ErrorInternal, Err: err}
}
if response.StatusCode > 299 {
responseData, _ := ioutil.ReadAll(response.Body)
responseData, _ := io.ReadAll(response.Body)
err := fmt.Errorf("Error in getting 2xx response in API call to alertmanager/v1/receivers\n Status: %s \n Data: %s", response.Status, string(responseData))
zap.S().Error(err)

View File

@ -3,12 +3,13 @@ package dashboards
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"go.uber.org/zap"
"go.signoz.io/signoz/pkg/query-service/constants"
"go.signoz.io/signoz/pkg/query-service/interfaces"
"go.uber.org/zap"
)
func readCurrentDir(dir string, fm interfaces.FeatureLookup) error {
@ -22,7 +23,10 @@ func readCurrentDir(dir string, fm interfaces.FeatureLookup) error {
list, _ := file.Readdirnames(0) // 0 to read all files and folders
for _, filename := range list {
zap.S().Info("Provisioning dashboard: ", filename)
plan, err := ioutil.ReadFile(dir + "/" + filename)
// using filepath.Join for platform specific path creation
// which is equivalent to "dir+/+filename" (on unix based systems) but cleaner
plan, err := os.ReadFile(filepath.Join(dir, filename))
if err != nil {
zap.S().Errorf("Creating Dashboards: Error in reading json fron file: %s\t%s", filename, err)
continue

View File

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strconv"
@ -20,6 +19,7 @@ import (
jsoniter "github.com/json-iterator/go"
_ "github.com/mattn/go-sqlite3"
"github.com/prometheus/prometheus/promql"
"go.signoz.io/signoz/pkg/query-service/agentConf"
"go.signoz.io/signoz/pkg/query-service/app/dashboards"
"go.signoz.io/signoz/pkg/query-service/app/explorer"
@ -37,6 +37,9 @@ import (
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
querytemplate "go.signoz.io/signoz/pkg/query-service/utils/queryTemplate"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.signoz.io/signoz/pkg/query-service/app/logparsingpipeline"
"go.signoz.io/signoz/pkg/query-service/dao"
am "go.signoz.io/signoz/pkg/query-service/integrations/alertManager"
@ -46,8 +49,6 @@ import (
"go.signoz.io/signoz/pkg/query-service/rules"
"go.signoz.io/signoz/pkg/query-service/telemetry"
"go.signoz.io/signoz/pkg/query-service/version"
"go.uber.org/multierr"
"go.uber.org/zap"
)
type status string
@ -981,7 +982,7 @@ func (aH *APIHandler) saveAndReturn(w http.ResponseWriter, r *http.Request, sign
func (aH *APIHandler) createDashboardsTransform(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
var importData model.GrafanaJSON
@ -1024,7 +1025,7 @@ func (aH *APIHandler) createDashboards(w http.ResponseWriter, r *http.Request) {
func (aH *APIHandler) testRule(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("Error in getting req body in test rule API\n", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -1067,7 +1068,7 @@ func (aH *APIHandler) patchRule(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("msg: error in getting req body of patch rule API\n", "\t error:", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -1088,7 +1089,7 @@ func (aH *APIHandler) editRule(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("msg: error in getting req body of edit rule API\n", "\t error:", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -1139,7 +1140,7 @@ func (aH *APIHandler) listChannels(w http.ResponseWriter, r *http.Request) {
func (aH *APIHandler) testChannel(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("Error in getting req body of testChannel API\n", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -1166,7 +1167,7 @@ func (aH *APIHandler) editChannel(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("Error in getting req body of editChannel API\n", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -1194,7 +1195,7 @@ func (aH *APIHandler) editChannel(w http.ResponseWriter, r *http.Request) {
func (aH *APIHandler) createChannel(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("Error in getting req body of createChannel API\n", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -1241,7 +1242,7 @@ func (aH *APIHandler) getAlerts(w http.ResponseWriter, r *http.Request) {
func (aH *APIHandler) createRule(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("Error in getting req body for create rule API\n", err)
RespondError(w, &model.ApiError{Typ: model.ErrorBadData, Err: err}, nil)
@ -2065,7 +2066,7 @@ func (aH *APIHandler) patchUserFlag(w http.ResponseWriter, r *http.Request) {
// read input into user flag
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
zap.S().Errorf("failed read user flags from http request for userId ", userId, "with error: ", err)
RespondError(w, model.BadRequestStr("received user flags in invalid format"), nil)

View File

@ -2,7 +2,7 @@ package otelconfig
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/knadh/koanf/parsers/yaml"
@ -11,7 +11,7 @@ import (
)
func TestServiceConfig(t *testing.T) {
yamlFile, err := ioutil.ReadFile("./testdata/service.yaml")
yamlFile, err := os.ReadFile("./testdata/service.yaml")
if err != nil {
fmt.Printf("yamlFile.Get err #%v ", err)
t.Fail()

View File

@ -2,7 +2,7 @@ package signozio
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"time"
@ -51,7 +51,7 @@ func FetchDynamicConfigs() (map[string]Config, *model.ApiError) {
return DefaultConfig, nil
}
httpBody, err := ioutil.ReadAll(httpResponse.Body)
httpBody, err := io.ReadAll(httpResponse.Body)
if err != nil {
return DefaultConfig, nil
}

View File

@ -2,7 +2,7 @@ package telemetry
import (
"context"
"io/ioutil"
"io"
"math/rand"
"net/http"
"os"
@ -12,12 +12,13 @@ import (
"time"
ph "github.com/posthog/posthog-go"
"gopkg.in/segmentio/analytics-go.v3"
"go.signoz.io/signoz/pkg/query-service/constants"
"go.signoz.io/signoz/pkg/query-service/interfaces"
"go.signoz.io/signoz/pkg/query-service/model"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
"go.signoz.io/signoz/pkg/query-service/version"
"gopkg.in/segmentio/analytics-go.v3"
)
const (
@ -248,7 +249,7 @@ func getOutboundIP() string {
defer resp.Body.Close()
if err == nil {
ipBody, err := ioutil.ReadAll(resp.Body)
ipBody, err := io.ReadAll(resp.Body)
if err == nil {
ip = ipBody
}

View File

@ -4,11 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.signoz.io/signoz/pkg/query-service/auth"
"go.signoz.io/signoz/pkg/query-service/model"
)
@ -19,7 +20,7 @@ func invite(t *testing.T, email string) *model.InviteResponse {
require.NoError(t, err)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var inviteResp model.InviteResponse
@ -48,7 +49,7 @@ func register(email, password, token string) (string, error) {
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
b, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
@ -75,7 +76,7 @@ func login(email, password, refreshToken string) (*model.LoginResponse, error) {
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read body")
}

View File

@ -3,12 +3,13 @@ package tests
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.signoz.io/signoz/pkg/query-service/model"
)
@ -35,7 +36,7 @@ func setTTL(table, coldStorage, toColdTTL, deleteTTL string, jwtToken string) ([
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return b, err
}
@ -59,7 +60,7 @@ func TestListDisks(t *testing.T) {
require.NoError(t, err)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `[{"name":"default","type":"local"}, {"name":"s3","type":"s3"}]`, string(b))
}
@ -134,7 +135,7 @@ func getTTL(t *testing.T, table string, jwtToken string) *model.GetTTLResponseIt
require.NoError(t, err)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
res := &model.GetTTLResponseItem{}