From dcea79cef3c23f7807bf1144806e0e89901e01b4 Mon Sep 17 00:00:00 2001 From: Vishal Sharma Date: Mon, 9 Oct 2023 21:06:01 +0530 Subject: [PATCH] feat: ingestion key management (#3699) --- pkg/query-service/app/http_handler.go | 2 ++ pkg/query-service/app/ingestion_key.go | 33 ++++++++++++++++++ pkg/query-service/app/parser.go | 8 +++++ pkg/query-service/dao/interface.go | 4 +++ pkg/query-service/dao/sqlite/connection.go | 8 +++++ pkg/query-service/dao/sqlite/ingestion.go | 39 ++++++++++++++++++++++ pkg/query-service/model/db.go | 10 ++++++ 7 files changed, 104 insertions(+) create mode 100644 pkg/query-service/app/ingestion_key.go create mode 100644 pkg/query-service/dao/sqlite/ingestion.go diff --git a/pkg/query-service/app/http_handler.go b/pkg/query-service/app/http_handler.go index 9045967077..0f6b7f3090 100644 --- a/pkg/query-service/app/http_handler.go +++ b/pkg/query-service/app/http_handler.go @@ -377,6 +377,8 @@ func (aH *APIHandler) RegisterRoutes(router *mux.Router, am *AuthMiddleware) { router.HandleFunc("/api/v1/settings/ttl", am.ViewAccess(aH.getTTL)).Methods(http.MethodGet) router.HandleFunc("/api/v1/settings/apdex", am.AdminAccess(aH.setApdexSettings)).Methods(http.MethodPost) router.HandleFunc("/api/v1/settings/apdex", am.ViewAccess(aH.getApdexSettings)).Methods(http.MethodGet) + router.HandleFunc("/api/v1/settings/ingestion_key", am.AdminAccess(aH.insertIngestionKey)).Methods(http.MethodPost) + router.HandleFunc("/api/v1/settings/ingestion_key", am.ViewAccess(aH.getIngestionKeys)).Methods(http.MethodGet) router.HandleFunc("/api/v1/metric_meta", am.ViewAccess(aH.getLatencyMetricMetadata)).Methods(http.MethodGet) diff --git a/pkg/query-service/app/ingestion_key.go b/pkg/query-service/app/ingestion_key.go new file mode 100644 index 0000000000..036d3a3032 --- /dev/null +++ b/pkg/query-service/app/ingestion_key.go @@ -0,0 +1,33 @@ +package app + +import ( + "context" + "net/http" + + "go.signoz.io/signoz/pkg/query-service/dao" + "go.signoz.io/signoz/pkg/query-service/model" +) + +func (aH *APIHandler) insertIngestionKey(w http.ResponseWriter, r *http.Request) { + req, err := parseInsertIngestionKeyRequest(r) + if aH.HandleError(w, err, http.StatusBadRequest) { + return + } + + if err := dao.DB().InsertIngestionKey(context.Background(), req); err != nil { + RespondError(w, &model.ApiError{Err: err, Typ: model.ErrorInternal}, nil) + return + } + + aH.WriteJSON(w, r, map[string]string{"data": "ingestion key added successfully"}) +} + +func (aH *APIHandler) getIngestionKeys(w http.ResponseWriter, r *http.Request) { + ingestionKeys, err := dao.DB().GetIngestionKeys(context.Background()) + if err != nil { + RespondError(w, &model.ApiError{Err: err, Typ: model.ErrorInternal}, nil) + return + } + + aH.WriteJSON(w, r, ingestionKeys) +} diff --git a/pkg/query-service/app/parser.go b/pkg/query-service/app/parser.go index a957ef85e7..4e6350b70e 100644 --- a/pkg/query-service/app/parser.go +++ b/pkg/query-service/app/parser.go @@ -734,6 +734,14 @@ func parseSetApdexScoreRequest(r *http.Request) (*model.ApdexSettings, error) { return &req, nil } +func parseInsertIngestionKeyRequest(r *http.Request) (*model.IngestionKey, error) { + var req model.IngestionKey + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + return nil, err + } + return &req, nil +} + func parseRegisterRequest(r *http.Request) (*auth.RegisterRequest, error) { var req auth.RegisterRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { diff --git a/pkg/query-service/dao/interface.go b/pkg/query-service/dao/interface.go index 068c8d167d..9f99d67c74 100644 --- a/pkg/query-service/dao/interface.go +++ b/pkg/query-service/dao/interface.go @@ -35,6 +35,8 @@ type Queries interface { GetApdexSettings(ctx context.Context, services []string) ([]model.ApdexSettings, *model.ApiError) + GetIngestionKeys(ctx context.Context) ([]model.IngestionKey, *model.ApiError) + PrecheckLogin(ctx context.Context, email, sourceUrl string) (*model.PrecheckResponse, model.BaseApiError) } @@ -62,4 +64,6 @@ type Mutations interface { UpdateUserGroup(ctx context.Context, userId, groupId string) *model.ApiError SetApdexSettings(ctx context.Context, set *model.ApdexSettings) *model.ApiError + + InsertIngestionKey(ctx context.Context, ingestionKey *model.IngestionKey) *model.ApiError } diff --git a/pkg/query-service/dao/sqlite/connection.go b/pkg/query-service/dao/sqlite/connection.go index dd113a2863..f79d67a122 100644 --- a/pkg/query-service/dao/sqlite/connection.go +++ b/pkg/query-service/dao/sqlite/connection.go @@ -78,6 +78,14 @@ func InitDB(dataSourceName string) (*ModelDaoSqlite, error) { threshold FLOAT NOT NULL, exclude_status_codes TEXT NOT NULL ); + CREATE TABLE IF NOT EXISTS ingestion_keys ( + key_id TEXT PRIMARY KEY, + name TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + ingestion_key TEXT NOT NULL, + ingestion_url TEXT NOT NULL, + data_region TEXT NOT NULL + ); ` _, err = db.Exec(table_schema) diff --git a/pkg/query-service/dao/sqlite/ingestion.go b/pkg/query-service/dao/sqlite/ingestion.go new file mode 100644 index 0000000000..79d021e176 --- /dev/null +++ b/pkg/query-service/dao/sqlite/ingestion.go @@ -0,0 +1,39 @@ +package sqlite + +import ( + "context" + + "go.signoz.io/signoz/pkg/query-service/model" +) + +func (mds *ModelDaoSqlite) GetIngestionKeys(ctx context.Context) ([]model.IngestionKey, *model.ApiError) { + ingestion_keys := []model.IngestionKey{} + err := mds.db.Select(&ingestion_keys, `SELECT * FROM ingestion_keys`) + + if err != nil { + return nil, &model.ApiError{Typ: model.ErrorInternal, Err: err} + } + return ingestion_keys, nil +} + +func (mds *ModelDaoSqlite) InsertIngestionKey(ctx context.Context, ingestion_key *model.IngestionKey) *model.ApiError { + _, err := mds.db.ExecContext(ctx, ` + INSERT INTO ingestion_keys ( + ingestion_key, + name, + key_id, + ingestion_url, + data_region + ) VALUES ( + ?, + ?, + ?, + ?, + ? + )`, ingestion_key.IngestionKey, ingestion_key.Name, ingestion_key.KeyId, ingestion_key.IngestionURL, ingestion_key.DataRegion) + if err != nil { + return &model.ApiError{Typ: model.ErrorInternal, Err: err} + } + + return nil +} diff --git a/pkg/query-service/model/db.go b/pkg/query-service/model/db.go index f1d7817fc7..2968bf6606 100644 --- a/pkg/query-service/model/db.go +++ b/pkg/query-service/model/db.go @@ -4,6 +4,7 @@ import ( "database/sql/driver" "encoding/json" "fmt" + "time" ) type Organization struct { @@ -42,6 +43,15 @@ type ApdexSettings struct { ExcludeStatusCodes string `json:"excludeStatusCodes" db:"exclude_status_codes"` // sqlite doesn't support array type } +type IngestionKey struct { + KeyId string `json:"keyId" db:"key_id"` + Name string `json:"name" db:"name"` + CreatedAt time.Time `json:"createdAt" db:"created_at"` + IngestionKey string `json:"ingestionKey" db:"ingestion_key"` + IngestionURL string `json:"ingestionURL" db:"ingestion_url"` + DataRegion string `json:"dataRegion" db:"data_region"` +} + type UserFlag map[string]string func (uf UserFlag) Value() (driver.Value, error) {