mirror of
https://github.com/hanzoai/o11y.git
synced 2026-11-02 14:03:38 +00:00
Re-sync the entire SigNoz-derived Go tree to a clean fork of latest SigNoz main (upstream 3e6339019), eliminating the version-skew that broke `go build ./...` (mixed-era packages, ~14 imported-but-absent packages, partial signoz->o11y / clickhouse->datastore rebrands, self-import cycle, duplicate package decls). - pkg/ + cmd/ wholesale replaced with latest SigNoz; imports rewritten github.com/SigNoz/signoz/ -> github.com/hanzoai/o11y/. - Adopt SigNoz's CURRENT structure (resourcefilter->telemetryresourcefilter, integrationtypes->cloudintegrationtypes, roletypes/grammar/times folded in upstream); old paths dropped. - Fork deps repathed to hanzoai + bumped: signoz-otel-collector v0.144.6, clickhouse-go-mock v0.14.1, govaluate v0.1.0; expr-lang/expr => hanzoai/expr v1.17.8 (replace). go.mod reconciled from latest SigNoz; go.sum re-tidied. - ee/ + cmd/enterprise stripped — o11y stays MIT-only; community edition imports no ee packages. - Preserved o11y additions: module path github.com/hanzoai/o11y, mount.go (cloud.Register/zip mount adapter, HIP-0106), NOTICE + LICENSE (SigNoz §4c attribution retained; upstream pkg files are headerless per SigNoz convention). - Reverted o11y cosmetic renames (o11y*/datastore*) and experimental originals (authz/iamauthz, billing, zapreceiver, zapmetricreceiver) to SigNoz canonical to kill the skew; snapshots retained in git history for re-layering. go build ./... = exit 0. go vet clean except benign "unreachable code" in generated ANTLR parser files (inherited from upstream).
345 lines
9.5 KiB
Go
345 lines
9.5 KiB
Go
package nfmanagertest
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/hanzoai/o11y/pkg/types/alertmanagertypes"
|
|
"github.com/prometheus/common/model"
|
|
)
|
|
|
|
// MockNotificationManager is a simple mock implementation of NotificationManager.
|
|
type MockNotificationManager struct {
|
|
configs map[string]*alertmanagertypes.NotificationConfig
|
|
routes map[string]*alertmanagertypes.RoutePolicy
|
|
routesByName map[string][]*alertmanagertypes.RoutePolicy
|
|
errors map[string]error
|
|
}
|
|
|
|
// NewMock creates a new mock notification manager.
|
|
func NewMock() *MockNotificationManager {
|
|
return &MockNotificationManager{
|
|
configs: make(map[string]*alertmanagertypes.NotificationConfig),
|
|
routes: make(map[string]*alertmanagertypes.RoutePolicy),
|
|
routesByName: make(map[string][]*alertmanagertypes.RoutePolicy),
|
|
errors: make(map[string]error),
|
|
}
|
|
}
|
|
|
|
func getKey(orgId string, ruleId string) string {
|
|
return orgId + ":" + ruleId
|
|
}
|
|
|
|
func (m *MockNotificationManager) GetNotificationConfig(orgID string, ruleID string) (*alertmanagertypes.NotificationConfig, error) {
|
|
key := getKey(orgID, ruleID)
|
|
if err := m.errors[key]; err != nil {
|
|
return nil, err
|
|
}
|
|
if config := m.configs[key]; config != nil {
|
|
return config, nil
|
|
}
|
|
|
|
notificationConfig := alertmanagertypes.GetDefaultNotificationConfig()
|
|
return ¬ificationConfig, nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) SetNotificationConfig(orgID string, ruleID string, config *alertmanagertypes.NotificationConfig) error {
|
|
key := getKey(orgID, ruleID)
|
|
if err := m.errors[key]; err != nil {
|
|
return err
|
|
}
|
|
m.configs[key] = config
|
|
return nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) DeleteNotificationConfig(orgID string, ruleID string) error {
|
|
key := getKey(orgID, ruleID)
|
|
if err := m.errors[key]; err != nil {
|
|
return err
|
|
}
|
|
delete(m.configs, key)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) SetMockConfig(orgID, ruleID string, config *alertmanagertypes.NotificationConfig) {
|
|
key := getKey(orgID, ruleID)
|
|
m.configs[key] = config
|
|
}
|
|
|
|
func (m *MockNotificationManager) SetMockError(orgID, ruleID string, err error) {
|
|
key := getKey(orgID, ruleID)
|
|
m.errors[key] = err
|
|
}
|
|
|
|
func (m *MockNotificationManager) ClearMockData() {
|
|
m.configs = make(map[string]*alertmanagertypes.NotificationConfig)
|
|
m.routes = make(map[string]*alertmanagertypes.RoutePolicy)
|
|
m.routesByName = make(map[string][]*alertmanagertypes.RoutePolicy)
|
|
m.errors = make(map[string]error)
|
|
}
|
|
|
|
func (m *MockNotificationManager) HasConfig(orgID, ruleID string) bool {
|
|
key := getKey(orgID, ruleID)
|
|
_, exists := m.configs[key]
|
|
return exists
|
|
}
|
|
|
|
// Route Policy CRUD
|
|
|
|
func (m *MockNotificationManager) CreateRoutePolicy(ctx context.Context, orgID string, route *alertmanagertypes.RoutePolicy) error {
|
|
key := getKey(orgID, "create_route")
|
|
if err := m.errors[key]; err != nil {
|
|
return err
|
|
}
|
|
|
|
if route == nil {
|
|
return errors.NewInvalidInputf(errors.CodeInvalidInput, "route cannot be nil")
|
|
}
|
|
|
|
if err := route.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
routeKey := getKey(orgID, route.ID.StringValue())
|
|
m.routes[routeKey] = route
|
|
nameKey := getKey(orgID, route.Name)
|
|
m.routesByName[nameKey] = append(m.routesByName[nameKey], route)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) CreateRoutePolicies(ctx context.Context, orgID string, routes []*alertmanagertypes.RoutePolicy) error {
|
|
key := getKey(orgID, "create_routes")
|
|
if err := m.errors[key]; err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(routes) == 0 {
|
|
return errors.NewInvalidInputf(errors.CodeInvalidInput, "routes cannot be empty")
|
|
}
|
|
for i, route := range routes {
|
|
if route == nil {
|
|
return errors.NewInvalidInputf(errors.CodeInvalidInput, "route at index %d cannot be nil", i)
|
|
}
|
|
if err := route.Validate(); err != nil {
|
|
return errors.WrapInvalidInputf(err, errors.CodeInvalidInput, "route at index %d", i)
|
|
}
|
|
}
|
|
for _, route := range routes {
|
|
if err := m.CreateRoutePolicy(ctx, orgID, route); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) GetRoutePolicyByID(ctx context.Context, orgID string, routeID string) (*alertmanagertypes.RoutePolicy, error) {
|
|
key := getKey(orgID, "get_route")
|
|
if err := m.errors[key]; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if routeID == "" {
|
|
return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "routeID cannot be empty")
|
|
}
|
|
|
|
routeKey := getKey(orgID, routeID)
|
|
route, exists := m.routes[routeKey]
|
|
if !exists {
|
|
return nil, errors.NewNotFoundf(errors.CodeNotFound, "route with ID %s not found", routeID)
|
|
}
|
|
|
|
return route, nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) GetAllRoutePolicies(ctx context.Context, orgID string) ([]*alertmanagertypes.RoutePolicy, error) {
|
|
key := getKey(orgID, "get_all_routes")
|
|
if err := m.errors[key]; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if orgID == "" {
|
|
return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "orgID cannot be empty")
|
|
}
|
|
|
|
var routes []*alertmanagertypes.RoutePolicy
|
|
for routeKey, route := range m.routes {
|
|
if route.OrgID == orgID {
|
|
routes = append(routes, route)
|
|
}
|
|
_ = routeKey
|
|
}
|
|
|
|
return routes, nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) DeleteRoutePolicy(ctx context.Context, orgID string, routeID string) error {
|
|
key := getKey(orgID, "delete_route")
|
|
if err := m.errors[key]; err != nil {
|
|
return err
|
|
}
|
|
|
|
if routeID == "" {
|
|
return errors.NewInvalidInputf(errors.CodeInvalidInput, "routeID cannot be empty")
|
|
}
|
|
|
|
routeKey := getKey(orgID, routeID)
|
|
route, exists := m.routes[routeKey]
|
|
if !exists {
|
|
return errors.NewNotFoundf(errors.CodeNotFound, "route with ID %s not found", routeID)
|
|
}
|
|
delete(m.routes, routeKey)
|
|
|
|
nameKey := getKey(orgID, route.Name)
|
|
if nameRoutes, exists := m.routesByName[nameKey]; exists {
|
|
var filtered []*alertmanagertypes.RoutePolicy
|
|
for _, r := range nameRoutes {
|
|
if r.ID.StringValue() != routeID {
|
|
filtered = append(filtered, r)
|
|
}
|
|
}
|
|
if len(filtered) == 0 {
|
|
delete(m.routesByName, nameKey)
|
|
} else {
|
|
m.routesByName[nameKey] = filtered
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) DeleteAllRoutePoliciesByName(ctx context.Context, orgID string, name string) error {
|
|
key := getKey(orgID, "delete_routes_by_name")
|
|
if err := m.errors[key]; err != nil {
|
|
return err
|
|
}
|
|
|
|
if orgID == "" {
|
|
return errors.NewInvalidInputf(errors.CodeInvalidInput, "orgID cannot be empty")
|
|
}
|
|
|
|
if name == "" {
|
|
return errors.NewInvalidInputf(errors.CodeInvalidInput, "name cannot be empty")
|
|
}
|
|
|
|
nameKey := getKey(orgID, name)
|
|
routes, exists := m.routesByName[nameKey]
|
|
if !exists {
|
|
return nil // No routes to delete
|
|
}
|
|
|
|
for _, route := range routes {
|
|
routeKey := getKey(orgID, route.ID.StringValue())
|
|
delete(m.routes, routeKey)
|
|
}
|
|
|
|
delete(m.routesByName, nameKey)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) GetRoutePoliciesByChannel(ctx context.Context, orgID string, channelName string) ([]*alertmanagertypes.RoutePolicy, error) {
|
|
if orgID == "" {
|
|
return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "orgID cannot be empty")
|
|
}
|
|
|
|
var matched []*alertmanagertypes.RoutePolicy
|
|
for _, route := range m.routes {
|
|
if route.OrgID != orgID {
|
|
continue
|
|
}
|
|
for _, ch := range route.Channels {
|
|
if ch == channelName {
|
|
matched = append(matched, route)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return matched, nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) Match(ctx context.Context, orgID string, ruleID string, set model.LabelSet) ([]string, error) {
|
|
key := getKey(orgID, ruleID)
|
|
if err := m.errors[key]; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
config, err := m.GetNotificationConfig(orgID, ruleID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var expressionRoutes []*alertmanagertypes.RoutePolicy
|
|
if config.UsePolicy {
|
|
for _, route := range m.routes {
|
|
if route.OrgID == orgID && route.ExpressionKind == alertmanagertypes.PolicyBasedExpression {
|
|
expressionRoutes = append(expressionRoutes, route)
|
|
}
|
|
}
|
|
} else {
|
|
nameKey := getKey(orgID, ruleID)
|
|
if routes, exists := m.routesByName[nameKey]; exists {
|
|
expressionRoutes = routes
|
|
}
|
|
}
|
|
|
|
var matchedChannels []string
|
|
for _, route := range expressionRoutes {
|
|
if m.evaluateExpr(route.Expression, set) {
|
|
matchedChannels = append(matchedChannels, route.Channels...)
|
|
}
|
|
}
|
|
|
|
return matchedChannels, nil
|
|
}
|
|
|
|
func (m *MockNotificationManager) evaluateExpr(expression string, labelSet model.LabelSet) bool {
|
|
ruleID, ok := labelSet["ruleId"]
|
|
if !ok {
|
|
return false
|
|
}
|
|
if strings.Contains(expression, `ruleId in ["ruleId-OtherAlert", "ruleId-TestingAlert"]`) {
|
|
return ruleID == "ruleId-OtherAlert" || ruleID == "ruleId-TestingAlert"
|
|
}
|
|
if strings.Contains(expression, `ruleId in ["ruleId-HighLatency", "ruleId-HighErrorRate"]`) {
|
|
return ruleID == "ruleId-HighLatency" || ruleID == "ruleId-HighErrorRate"
|
|
}
|
|
if strings.Contains(expression, `ruleId == "ruleId-HighLatency"`) {
|
|
return ruleID == "ruleId-HighLatency"
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Helper methods for testing
|
|
|
|
func (m *MockNotificationManager) SetMockRoute(orgID string, route *alertmanagertypes.RoutePolicy) {
|
|
routeKey := getKey(orgID, route.ID.StringValue())
|
|
m.routes[routeKey] = route
|
|
|
|
nameKey := getKey(orgID, route.Name)
|
|
m.routesByName[nameKey] = append(m.routesByName[nameKey], route)
|
|
}
|
|
|
|
func (m *MockNotificationManager) SetMockRouteError(orgID, operation string, err error) {
|
|
key := getKey(orgID, operation)
|
|
m.errors[key] = err
|
|
}
|
|
|
|
func (m *MockNotificationManager) ClearMockRoutes() {
|
|
m.routes = make(map[string]*alertmanagertypes.RoutePolicy)
|
|
m.routesByName = make(map[string][]*alertmanagertypes.RoutePolicy)
|
|
}
|
|
|
|
func (m *MockNotificationManager) GetRouteCount() int {
|
|
return len(m.routes)
|
|
}
|
|
|
|
func (m *MockNotificationManager) HasRoute(orgID, routeID string) bool {
|
|
routeKey := getKey(orgID, routeID)
|
|
_, exists := m.routes[routeKey]
|
|
return exists
|
|
}
|