mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-01 14:52:03 +08:00

### Summary - query-service: skip preloading non-json files under dashboards Resolves the error log: ``` {"level":"INFO","timestamp":"2025-01-29T17:37:18.635Z","caller":"dashboards/provision.go:26","msg":"Provisioning dashboard: ","filename":".gitkeep"} {"level":"ERROR","timestamp":"2025-01-29T17:37:18.635Z","caller":"dashboards/provision.go:38","msg":"Creating Dashboards: Error in unmarshalling json from file","filename":".gitkeep","error":"unexpected end of JSON input","stacktrace":"go.signoz.io/signoz/pkg/query-service/app/dashboards.readCurrentDir\n\t/home/sa_100696978840572610735/signoz/pkg/query-service/app/dashboards/provision.go:38\ngo.signoz.io/signoz/pkg/query-service/app/dashboards.LoadDashboardFiles\n\t/home/sa_100696978840572610735/signoz/pkg/query-service/app/dashboards/provision.go:79\ngo.signoz.io/signoz/pkg/query-service/app.NewAPIHandler\n\t/home/sa_100696978840572610735/signoz/pkg/query-service/app/http_handler.go:271\ngo.signoz.io/signoz/ee/query-service/app/api.NewAPIHandler\n\t/home/sa_100696978840572610735/signoz/ee/query-service/app/api/api.go:56\ngo.signoz.io/signoz/ee/query-service/app.NewServer\n\t/home/sa_100696978840572610735/signoz/ee/query-service/app/server.go:287\nmain.main\n\t/home/sa_100696978840572610735/signoz/ee/query-service/main.go:191\nruntime.main\n\t/home/sa_100696978840572610735/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.7.linux-amd64/src/runtime/proc.go:271"} ``` Signed-off-by: Prashant Shahi <prashant@signoz.io>
86 lines
2.8 KiB
Go
86 lines
2.8 KiB
Go
package dashboards
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.signoz.io/signoz/pkg/query-service/constants"
|
|
"go.signoz.io/signoz/pkg/query-service/interfaces"
|
|
"go.signoz.io/signoz/pkg/query-service/model"
|
|
)
|
|
|
|
func readCurrentDir(dir string, fm interfaces.FeatureLookup) error {
|
|
file, err := os.Open(dir)
|
|
if err != nil {
|
|
zap.L().Warn("failed opening directory", zap.Error(err))
|
|
return nil
|
|
}
|
|
defer file.Close()
|
|
|
|
list, _ := file.Readdirnames(0) // 0 to read all files and folders
|
|
for _, filename := range list {
|
|
if strings.ToLower(filepath.Ext(filename)) != ".json" {
|
|
zap.L().Debug("Skipping non-json file", zap.String("filename", filename))
|
|
continue
|
|
}
|
|
zap.L().Info("Provisioning dashboard: ", zap.String("filename", 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.L().Error("Creating Dashboards: Error in reading json fron file", zap.String("filename", filename), zap.Error(err))
|
|
continue
|
|
}
|
|
var data map[string]interface{}
|
|
err = json.Unmarshal(plan, &data)
|
|
if err != nil {
|
|
zap.L().Error("Creating Dashboards: Error in unmarshalling json from file", zap.String("filename", filename), zap.Error(err))
|
|
continue
|
|
}
|
|
err = IsPostDataSane(&data)
|
|
if err != nil {
|
|
zap.L().Info("Creating Dashboards: Error in file", zap.String("filename", filename), zap.Error(err))
|
|
continue
|
|
}
|
|
|
|
id := data["uuid"]
|
|
if id == nil {
|
|
_, apiErr := CreateDashboard(context.Background(), data, fm)
|
|
if apiErr != nil {
|
|
zap.L().Error("Creating Dashboards: Error in file", zap.String("filename", filename), zap.Error(apiErr.Err))
|
|
}
|
|
continue
|
|
}
|
|
|
|
apiErr := upsertDashboard(id.(string), data, filename, fm)
|
|
if apiErr != nil {
|
|
zap.L().Error("Creating Dashboards: Error upserting dashboard", zap.String("filename", filename), zap.Error(apiErr.Err))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func upsertDashboard(uuid string, data map[string]interface{}, filename string, fm interfaces.FeatureLookup) *model.ApiError {
|
|
_, apiErr := GetDashboard(context.Background(), uuid)
|
|
if apiErr == nil {
|
|
zap.S().Infof("Creating Dashboards: Already exists: %s\t%s", filename, "Dashboard already present in database, Updating dashboard")
|
|
_, apiErr := UpdateDashboard(context.Background(), uuid, data, fm)
|
|
return apiErr
|
|
}
|
|
|
|
zap.S().Infof("Creating Dashboards: UUID not found: %s\t%s", filename, "Dashboard not present in database, Creating dashboard")
|
|
_, apiErr = CreateDashboard(context.Background(), data, fm)
|
|
return apiErr
|
|
}
|
|
|
|
func LoadDashboardFiles(fm interfaces.FeatureLookup) error {
|
|
dashboardsPath := constants.GetOrDefaultEnv("DASHBOARDS_PATH", "./config/dashboards")
|
|
return readCurrentDir(dashboardsPath, fm)
|
|
}
|