Skip to content

Commit

Permalink
chore: some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-k-singh committed Jan 30, 2025
1 parent c37859e commit d786bca
Showing 1 changed file with 85 additions and 54 deletions.
139 changes: 85 additions & 54 deletions ee/query-service/app/api/cloudIntegrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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](
Expand All @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit d786bca

Please sign in to comment.