mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-07-31 22:12:00 +08:00

* chore: refactor: inject sqlx.DB into opamp.initDB instead of DB file name * chore: reorganize test utils a little * chore: add test validating pipelines for installed integrations show up in pipelines list * chore: get basic integration pipelines testcase passing * chore: reconcile experimental changes with latest state of develop * chore: add integration test for reordering of pipelines * chore: marker for integration pipelines using Id * chore: hookup propagation of installed integration pipelines by opamp * chore: add util for mapping slices * chore: add support for reordering integration pipelines * chore: exclude user saved integration pipelines if no longer installed * chore: flesh out rest of intgeration pipelines scenarios * chore: handle scenario when an integration is installed before any pipelines exist * chore: notify agentConf of update after uninstalling an integration * chore: some minor cleanup * chore: some more cleanup * chore: update ee server for changed controllers * chore: some more cleanup * chore: change builtin integration id prefix to avoid using colons that break yaml * chore: update builtin integration id in test
338 lines
8.6 KiB
Go
338 lines
8.6 KiB
Go
package integrations
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jmoiron/sqlx"
|
|
"go.signoz.io/signoz/pkg/query-service/app/dashboards"
|
|
"go.signoz.io/signoz/pkg/query-service/app/logparsingpipeline"
|
|
"go.signoz.io/signoz/pkg/query-service/model"
|
|
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
|
|
"go.signoz.io/signoz/pkg/query-service/rules"
|
|
"go.signoz.io/signoz/pkg/query-service/utils"
|
|
)
|
|
|
|
type IntegrationAuthor struct {
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
HomePage string `json:"homepage"`
|
|
}
|
|
type IntegrationSummary struct {
|
|
Id string `json:"id"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"` // A short description
|
|
|
|
Author IntegrationAuthor `json:"author"`
|
|
|
|
Icon string `json:"icon"`
|
|
}
|
|
|
|
type IntegrationAssets struct {
|
|
Logs LogsAssets `json:"logs"`
|
|
Dashboards []dashboards.Dashboard `json:"dashboards"`
|
|
|
|
Alerts []rules.PostableRule `json:"alerts"`
|
|
}
|
|
|
|
type LogsAssets struct {
|
|
Pipelines []logparsingpipeline.PostablePipeline `json:"pipelines"`
|
|
}
|
|
|
|
type IntegrationConfigStep struct {
|
|
Title string `json:"title"`
|
|
Instructions string `json:"instructions"`
|
|
}
|
|
|
|
type DataCollectedForIntegration struct {
|
|
Logs []CollectedLogAttribute `json:"logs"`
|
|
Metrics []CollectedMetric `json:"metrics"`
|
|
}
|
|
|
|
type CollectedLogAttribute struct {
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
type CollectedMetric struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Unit string `json:"unit"`
|
|
}
|
|
|
|
type SignalConnectionStatus struct {
|
|
LastReceivedTsMillis int64 `json:"last_received_ts_ms"` // epoch milliseconds
|
|
LastReceivedFrom string `json:"last_received_from"` // resource identifier
|
|
}
|
|
|
|
type IntegrationConnectionStatus struct {
|
|
Logs *SignalConnectionStatus `json:"logs"`
|
|
Metrics *SignalConnectionStatus `json:"metrics"`
|
|
}
|
|
|
|
type IntegrationConnectionTests struct {
|
|
Logs *v3.FilterSet `json:"logs"`
|
|
|
|
// TODO(Raj): Add connection tests for other signals.
|
|
}
|
|
|
|
type IntegrationDetails struct {
|
|
IntegrationSummary
|
|
|
|
Categories []string `json:"categories"`
|
|
Overview string `json:"overview"` // markdown
|
|
Configuration []IntegrationConfigStep `json:"configuration"`
|
|
DataCollected DataCollectedForIntegration `json:"data_collected"`
|
|
Assets IntegrationAssets `json:"assets"`
|
|
|
|
ConnectionTests *IntegrationConnectionTests `json:"connection_tests"`
|
|
}
|
|
|
|
type IntegrationsListItem struct {
|
|
IntegrationSummary
|
|
IsInstalled bool `json:"is_installed"`
|
|
}
|
|
|
|
type InstalledIntegration struct {
|
|
IntegrationId string `json:"integration_id" db:"integration_id"`
|
|
Config InstalledIntegrationConfig `json:"config_json" db:"config_json"`
|
|
InstalledAt time.Time `json:"installed_at" db:"installed_at"`
|
|
}
|
|
type InstalledIntegrationConfig map[string]interface{}
|
|
|
|
type Integration struct {
|
|
IntegrationDetails
|
|
Installation *InstalledIntegration `json:"installation"`
|
|
}
|
|
|
|
type Manager struct {
|
|
availableIntegrationsRepo AvailableIntegrationsRepo
|
|
installedIntegrationsRepo InstalledIntegrationsRepo
|
|
}
|
|
|
|
func NewManager(db *sqlx.DB) (*Manager, error) {
|
|
iiRepo, err := NewInstalledIntegrationsSqliteRepo(db)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(
|
|
"could not init sqlite DB for installed integrations: %w", err,
|
|
)
|
|
}
|
|
|
|
return &Manager{
|
|
availableIntegrationsRepo: &BuiltInIntegrations{},
|
|
installedIntegrationsRepo: iiRepo,
|
|
}, nil
|
|
}
|
|
|
|
type IntegrationsFilter struct {
|
|
IsInstalled *bool
|
|
}
|
|
|
|
func (m *Manager) ListIntegrations(
|
|
ctx context.Context,
|
|
filter *IntegrationsFilter,
|
|
// Expected to have pagination over time.
|
|
) ([]IntegrationsListItem, *model.ApiError) {
|
|
available, apiErr := m.availableIntegrationsRepo.list(ctx)
|
|
if apiErr != nil {
|
|
return nil, model.WrapApiError(
|
|
apiErr, "could not fetch available integrations",
|
|
)
|
|
}
|
|
|
|
installed, apiErr := m.installedIntegrationsRepo.list(ctx)
|
|
if apiErr != nil {
|
|
return nil, model.WrapApiError(
|
|
apiErr, "could not fetch installed integrations",
|
|
)
|
|
}
|
|
installedIds := []string{}
|
|
for _, ii := range installed {
|
|
installedIds = append(installedIds, ii.IntegrationId)
|
|
}
|
|
|
|
result := []IntegrationsListItem{}
|
|
for _, ai := range available {
|
|
result = append(result, IntegrationsListItem{
|
|
IntegrationSummary: ai.IntegrationSummary,
|
|
IsInstalled: slices.Contains(installedIds, ai.Id),
|
|
})
|
|
}
|
|
|
|
if filter != nil {
|
|
if filter.IsInstalled != nil {
|
|
filteredResult := []IntegrationsListItem{}
|
|
for _, r := range result {
|
|
if r.IsInstalled == *filter.IsInstalled {
|
|
filteredResult = append(filteredResult, r)
|
|
}
|
|
}
|
|
result = filteredResult
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (m *Manager) GetIntegration(
|
|
ctx context.Context,
|
|
integrationId string,
|
|
) (*Integration, *model.ApiError) {
|
|
integrationDetails, apiErr := m.getIntegrationDetails(
|
|
ctx, integrationId,
|
|
)
|
|
if apiErr != nil {
|
|
return nil, apiErr
|
|
}
|
|
|
|
installation, apiErr := m.getInstalledIntegration(
|
|
ctx, integrationId,
|
|
)
|
|
if apiErr != nil {
|
|
return nil, apiErr
|
|
}
|
|
|
|
return &Integration{
|
|
IntegrationDetails: *integrationDetails,
|
|
Installation: installation,
|
|
}, nil
|
|
}
|
|
|
|
func (m *Manager) GetIntegrationConnectionTests(
|
|
ctx context.Context,
|
|
integrationId string,
|
|
) (*IntegrationConnectionTests, *model.ApiError) {
|
|
integrationDetails, apiErr := m.getIntegrationDetails(
|
|
ctx, integrationId,
|
|
)
|
|
if apiErr != nil {
|
|
return nil, apiErr
|
|
}
|
|
return integrationDetails.ConnectionTests, nil
|
|
}
|
|
|
|
func (m *Manager) InstallIntegration(
|
|
ctx context.Context,
|
|
integrationId string,
|
|
config InstalledIntegrationConfig,
|
|
) (*IntegrationsListItem, *model.ApiError) {
|
|
integrationDetails, apiErr := m.getIntegrationDetails(ctx, integrationId)
|
|
if apiErr != nil {
|
|
return nil, apiErr
|
|
}
|
|
|
|
_, apiErr = m.installedIntegrationsRepo.upsert(
|
|
ctx, integrationId, config,
|
|
)
|
|
if apiErr != nil {
|
|
return nil, model.WrapApiError(
|
|
apiErr, "could not insert installed integration",
|
|
)
|
|
}
|
|
|
|
return &IntegrationsListItem{
|
|
IntegrationSummary: integrationDetails.IntegrationSummary,
|
|
IsInstalled: true,
|
|
}, nil
|
|
}
|
|
|
|
func (m *Manager) UninstallIntegration(
|
|
ctx context.Context,
|
|
integrationId string,
|
|
) *model.ApiError {
|
|
return m.installedIntegrationsRepo.delete(ctx, integrationId)
|
|
}
|
|
|
|
// Helpers.
|
|
func (m *Manager) getIntegrationDetails(
|
|
ctx context.Context,
|
|
integrationId string,
|
|
) (*IntegrationDetails, *model.ApiError) {
|
|
if len(strings.TrimSpace(integrationId)) < 1 {
|
|
return nil, model.BadRequest(fmt.Errorf(
|
|
"integrationId is required",
|
|
))
|
|
}
|
|
|
|
ais, apiErr := m.availableIntegrationsRepo.get(
|
|
ctx, []string{integrationId},
|
|
)
|
|
if apiErr != nil {
|
|
return nil, model.WrapApiError(apiErr, fmt.Sprintf(
|
|
"could not fetch integration: %s", integrationId,
|
|
))
|
|
}
|
|
|
|
integrationDetails, wasFound := ais[integrationId]
|
|
if !wasFound {
|
|
return nil, model.NotFoundError(fmt.Errorf(
|
|
"could not find integration: %s", integrationId,
|
|
))
|
|
}
|
|
return &integrationDetails, nil
|
|
}
|
|
|
|
func (m *Manager) getInstalledIntegration(
|
|
ctx context.Context,
|
|
integrationId string,
|
|
) (*InstalledIntegration, *model.ApiError) {
|
|
iis, apiErr := m.installedIntegrationsRepo.get(
|
|
ctx, []string{integrationId},
|
|
)
|
|
if apiErr != nil {
|
|
return nil, model.WrapApiError(apiErr, fmt.Sprintf(
|
|
"could not fetch installed integration: %s", integrationId,
|
|
))
|
|
}
|
|
|
|
installation, wasFound := iis[integrationId]
|
|
if !wasFound {
|
|
return nil, nil
|
|
}
|
|
return &installation, nil
|
|
}
|
|
|
|
func (m *Manager) GetPipelinesForInstalledIntegrations(
|
|
ctx context.Context,
|
|
) ([]logparsingpipeline.Pipeline, *model.ApiError) {
|
|
installations, apiErr := m.installedIntegrationsRepo.list(ctx)
|
|
if apiErr != nil {
|
|
return nil, apiErr
|
|
}
|
|
|
|
installedIds := utils.MapSlice(installations, func(i InstalledIntegration) string {
|
|
return i.IntegrationId
|
|
})
|
|
installedIntegrations, apiErr := m.availableIntegrationsRepo.get(ctx, installedIds)
|
|
if apiErr != nil {
|
|
return nil, apiErr
|
|
}
|
|
|
|
pipelines := []logparsingpipeline.Pipeline{}
|
|
for _, ii := range installedIntegrations {
|
|
for _, p := range ii.Assets.Logs.Pipelines {
|
|
pp := logparsingpipeline.Pipeline{
|
|
// Alias is used for identifying integration pipelines. Id can't be used for this
|
|
// since versioning while saving pipelines requires a new id for each version
|
|
// to avoid altering history when pipelines are edited/reordered etc
|
|
Alias: AliasForIntegrationPipeline(ii.Id, p.Alias),
|
|
Id: uuid.NewString(),
|
|
OrderId: p.OrderId,
|
|
Enabled: p.Enabled,
|
|
Name: p.Name,
|
|
Description: &p.Description,
|
|
Filter: p.Filter,
|
|
Config: p.Config,
|
|
}
|
|
pipelines = append(pipelines, pp)
|
|
}
|
|
}
|
|
|
|
return pipelines, nil
|
|
}
|