From 0317d19cae05d8e4088035a0419cec19f5087fe7 Mon Sep 17 00:00:00 2001 From: Raj Date: Thu, 30 Jan 2025 15:14:03 +0530 Subject: [PATCH] chore: some more cleanup --- ee/query-service/app/api/cloudIntegrations.go | 139 +++++++++++------- 1 file changed, 85 insertions(+), 54 deletions(-) diff --git a/ee/query-service/app/api/cloudIntegrations.go b/ee/query-service/app/api/cloudIntegrations.go index 41380a6126..cf13c596ae 100644 --- a/ee/query-service/app/api/cloudIntegrations.go +++ b/ee/query-service/app/api/cloudIntegrations.go @@ -39,7 +39,7 @@ func (ah *APIHandler) CloudIntegrationsGenerateConnectionParams(w http.ResponseW } if license == nil { - // nothing to return if no license found. + zap.L().Info("no cloud connection params can be deduced since no license was found") ah.Respond(w, CloudIntegrationConnectionParamsResponse{}) return } @@ -71,14 +71,75 @@ func (ah *APIHandler) CloudIntegrationsGenerateConnectionParams(w http.ResponseW } result.IngestionKey = ingestionKey + + } else { + zap.L().Info("ingestion key can't be deduced since no gateway url has been configured") } ah.Respond(w, result) } +func getIngestionUrlAndSigNozAPIUrl(licenseKey string) ( + string, string, *basemodel.ApiError, +) { + url := fmt.Sprintf( + "%s%s", + strings.TrimSuffix(constants.ZeusURL, "/"), + "/v2/deployments/me", + ) + + type deploymentResponse struct { + Status string `json:"status"` + Error string `json:"error"` + Data struct { + Name string `json:"name"` + + ClusterInfo struct { + Region struct { + DNS string `json:"dns"` + } `json:"region"` + } `json:"cluster"` + } `json:"data"` + } + + resp, apiErr := requestAndParseResponse[deploymentResponse]( + url, map[string]string{"X-Signoz-Cloud-Api-Key": licenseKey}, nil, + ) + + if apiErr != nil { + return "", "", basemodel.WrapApiError( + apiErr, "couldn't query for deployment info", + ) + } + + if resp.Status != "success" { + return "", "", basemodel.InternalError(fmt.Errorf( + "couldn't query for deployment info: status: %s, error: %s", + resp.Status, resp.Error, + )) + } + + regionDns := resp.Data.ClusterInfo.Region.DNS + deploymentName := resp.Data.Name + + if len(regionDns) < 1 || len(deploymentName) < 1 { + // Fail early if actual response structure and expectation here ever diverge + return "", "", basemodel.InternalError(fmt.Errorf( + "deployment info response not in expected shape. couldn't determine region dns and deployment name", + )) + } + + ingestionUrl := fmt.Sprintf("https://ingest.%s", regionDns) + + signozApiUrl := fmt.Sprintf("https://%s.%s", deploymentName, regionDns) + + return ingestionUrl, signozApiUrl, nil +} + type ingestionKey struct { Name string `json:"name"` Value string `json:"value"` + // other attributes from gateway response not included here since they are not being used. } type ingestionKeysSearchResponse struct { @@ -93,7 +154,9 @@ type createIngestionKeyResponse struct { Error string `json:"error"` } -func getOrCreateCloudProviderIngestionKey(gatewayUrl string, licenseKey string, cloudProvider string) (string, *basemodel.ApiError) { +func getOrCreateCloudProviderIngestionKey( + gatewayUrl string, licenseKey string, cloudProvider string, +) (string, *basemodel.ApiError) { cloudProviderKeyName := fmt.Sprintf("%s-integration", cloudProvider) // see if the key already exists @@ -119,11 +182,17 @@ func getOrCreateCloudProviderIngestionKey(gatewayUrl string, licenseKey string, for _, k := range searchResult.Data { if k.Name == cloudProviderKeyName { + if len(k.Value) < 1 { + // Fail early if actual response structure and expectation here ever diverge + return "", basemodel.InternalError(fmt.Errorf( + "ingestion keys search response not as expected", + )) + } + return k.Value, nil } } - // create a key and return it if one doesn't already exist zap.L().Info( "no existing ingestion key found for cloud integration, creating a new one", zap.String("cloudProvider", cloudProvider), @@ -148,7 +217,15 @@ func getOrCreateCloudProviderIngestionKey(gatewayUrl string, licenseKey string, )) } - return createKeyResult.Data.Value, nil + ingestionKey := createKeyResult.Data.Value + if len(ingestionKey) < 1 { + // Fail early if actual response structure and expectation here ever diverge + return "", basemodel.InternalError(fmt.Errorf( + "ingestion key creation response not as expected", + )) + } + + return ingestionKey, nil } func requestGateway[ResponseType any]( @@ -167,65 +244,19 @@ func requestGateway[ResponseType any]( return requestAndParseResponse[ResponseType](reqUrl, headers, payload) } -func getIngestionUrlAndSigNozAPIUrl(licenseKey string) (string, string, *basemodel.ApiError) { - url := fmt.Sprintf( - "%s%s", - strings.TrimSuffix(constants.ZeusURL, "/"), - "/v2/deployments/me", - ) - - type deploymentResponse struct { - Status string `json:"status"` - Error string `json:"error"` - Data struct { - Name string `json:"name"` - ClusterInfo struct { - Region struct { - DNS string `json:"dns"` - } `json:"region"` - } `json:"cluster"` - } `json:"data"` - } - - resp, apiErr := requestAndParseResponse[deploymentResponse]( - url, map[string]string{"X-Signoz-Cloud-Api-Key": licenseKey}, nil, - ) - - if apiErr != nil { - return "", "", basemodel.WrapApiError( - apiErr, "couldn't query for deployment info", - ) - } - - if resp.Status != "success" { - return "", "", basemodel.InternalError(fmt.Errorf( - "couldn't query for deployment info: status: %s, error: %s", - resp.Status, resp.Error, - )) - } - - regionDns := resp.Data.ClusterInfo.Region.DNS - ingestionUrl := fmt.Sprintf("https://ingest.%s", regionDns) - - deploymentName := resp.Data.Name - signozApiUrl := fmt.Sprintf("https://%s.%s", deploymentName, regionDns) - - return ingestionUrl, signozApiUrl, nil -} - func requestAndParseResponse[ResponseType any]( url string, headers map[string]string, payload any, ) (*ResponseType, *basemodel.ApiError) { + reqMethod := http.MethodGet var reqBody io.Reader - if payload != nil { reqMethod = http.MethodPost bodyJson, err := json.Marshal(payload) if err != nil { return nil, basemodel.InternalError(fmt.Errorf( - "couldn't serialize payload to JSON: %w", err, + "couldn't serialize request payload to JSON: %w", err, )) } reqBody = bytes.NewBuffer([]byte(bodyJson)) @@ -248,14 +279,14 @@ func requestAndParseResponse[ResponseType any]( response, err := client.Do(req) if err != nil { - return nil, basemodel.InternalError(fmt.Errorf("couldn't request gateway: %w", err)) + return nil, basemodel.InternalError(fmt.Errorf("couldn't make request: %w", err)) } defer response.Body.Close() respBody, err := io.ReadAll(response.Body) if err != nil { - return nil, basemodel.InternalError(fmt.Errorf("couldn't read gateway response: %w", err)) + return nil, basemodel.InternalError(fmt.Errorf("couldn't read response: %w", err)) } var resp ResponseType