openreplay/backend/pkg/integrations/clients/sentry.go
Alexander 30a69893bb
New backend logs integrations (#2717)
* feat(integrations): new version of backend integrations

* feat(integrations): added ingress rule

* feat(integrations): fixed a port number

* feat(integrations): enabled ingress in values.yaml

* feat(integrations): added startup log

* feat(integrations): added extra logger for 3 of 4 backend logs integrations.

* feat(integrations): removed a logs loop call

* feat(integrations): fixed a table name

* feat(integrations): disabled extra logger

* feat(integrations): made extra logger as an option

* feat(integrations): changed contentType for logs file

* feat(integrations): bug fix

* feat(integrations): struct/string config support for datadog provider

* feat(integrations): map config support for datadog provider

* feat(integrations): removed unnecessary transformation

* feat(integrations): fixed datadog and sentry response format

* feat(integrations): added correct creds parser for sentry provider

* feat(integrations): removed unnecessary return statement

* feat(integrations): added correct creds parser for elastic search

* feat(integrations): changed elastic to elasticsearch

* feat(integrations): added correct creds parser for dynatrace

* feat(integrations): fixed an issue in query request for elasticsearch provider

* feat(integrations): made extra logger configurable by env var

* feat(integrations): removed debug logs
2024-10-31 15:28:38 +01:00

105 lines
2.4 KiB
Go

package clients
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
)
type sentryClient struct {
//
}
func NewSentryClient() Client {
return &sentryClient{}
}
type sentryConfig struct {
OrganizationSlug string `json:"organization_slug"`
ProjectSlug string `json:"project_slug"`
Token string `json:"token"`
}
type SentryEvent struct {
ID string `json:"id"`
Title string `json:"title"`
Message string `json:"message"`
Environment string `json:"environment"`
}
func (s *sentryClient) FetchSessionData(credentials interface{}, sessionID uint64) (interface{}, error) {
cfg, ok := credentials.(sentryConfig)
if !ok {
strCfg, ok := credentials.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid credentials, got: %+v", credentials)
}
cfg = sentryConfig{}
if val, ok := strCfg["organization_slug"].(string); ok {
cfg.OrganizationSlug = val
}
if val, ok := strCfg["project_slug"].(string); ok {
cfg.ProjectSlug = val
}
if val, ok := strCfg["token"].(string); ok {
cfg.Token = val
}
}
requestUrl := fmt.Sprintf("https://sentry.io/api/0/projects/%s/%s/events/", cfg.OrganizationSlug, cfg.ProjectSlug)
testCallLimit := 1
params := url.Values{}
if sessionID != 0 {
params.Add("query", fmt.Sprintf("openReplaySession.id=%d", sessionID))
} else {
params.Add("per_page", fmt.Sprintf("%d", testCallLimit))
}
requestUrl += "?" + params.Encode()
// Create a new request
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
log.Fatalf("Failed to create request: %v", err)
}
// Add Authorization header
req.Header.Set("Authorization", "Bearer "+cfg.Token)
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()
// Check if the response status is OK
if resp.StatusCode != http.StatusOK {
log.Fatalf("Failed to fetch logs, status code: %v", resp.StatusCode)
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %v", err)
}
// Parse the JSON response
var events []SentryEvent
err = json.Unmarshal(body, &events)
if err != nil {
log.Fatalf("Failed to parse JSON: %v", err)
}
if events == nil || len(events) == 0 {
return nil, fmt.Errorf("no logs found")
}
result, err := json.Marshal(events)
if err != nil {
return nil, err
}
return result, nil
}