mirror of
https://github.com/hanzoai/o11y.git
synced 2026-11-03 02:04:48 +00:00
* debrand: signoz/SigNoz/SIGNOZ -> o11y/O11y/O11Y across the tree Rename all SigNoz *branding* to O11y in file contents, file/dir names, package names, env vars, config keys, comments, docs, SDK code, and the ClickHouse schema identifiers the querier reads. Collector dependency: - Repoint github.com/hanzoai/signoz-otel-collector -> github.com/hanzoai/otel-collector and bump v0.144.6 -> v0.144.7 (go.mod + go.sum reconciled via go mod tidy). Internal package paths also debranded upstream: signozschemamigrator -> o11yschemamigrator, signozlogspipelineprocessor -> o11ylogspipelineprocessor. - Fix: v0.144.7 dropped otelconst.DistributedFieldKeysTable from the collector's public constants; pin FieldKeysTable = "distributed_field_keys" locally in pkg/telemetrymetadata/tables.go. Package/dir renames (package decl + all importers): pkg/apiserver/signozapiserver -> o11yapiserver, plus signozalertmanager, signozauthzapi, signozglobal, signozquerier, signozruler, and pkg/signoz -> pkg/o11y. Schema (read plane): signoz_traces/metrics/logs/metadata/analytics/meter and signoz_index_v3/index_v2/error_index_v2/spans (+ distributed_*) -> o11y_*. Data-preserving cutover migration added: deploy/clickhouse/migrations/0001_rename_signoz_to_o11y.sql (metadata-only RENAME DATABASE/TABLE, no drop/recreate). Preserved (attribution / external / wire contracts): - LICENSE + NOTICE verbatim (incl. "software from SigNoz", "Copyright ... SigNoz Inc."). - Upstream github.com/SigNoz/* repo URLs + @SigNoz/* CODEOWNERS teams. - Billing resource-attribute regex "signoz.workspace.*" (external wire contract). go build ./pkg/... ./cmd/... = 0; gofmt clean; renamed+schema pkg tests pass. * o11y: bump collector to v0.144.8 (o11y_* physical schema writer) + lockstep deploy plan Collector v0.144.8's schema-migrator + exporters now CREATE/WRITE the same o11y_* physical databases/tables the querier reads and the RENAME migration (0001_rename_signoz_to_o11y.sql) targets. Cross-checked byte-identical: writer DB set == migration target DB set (6); writer prefixed-table set == migration target table set (8); o11y querier reads and cloud reads are subsets of that set. Zero table-name mismatches between writer, readers, and migration. Collector go.mod unchanged between v0.144.7 and v0.144.8 (identical go.mod hash) — pure source rename, no module-graph change. Adds deploy/clickhouse/DEPLOY_signoz_to_o11y_cutover.md: the lockstep order (collector v0.144.8 -> RENAME migration -> o11y+cloud readers) with a fresh/scratch-ClickHouse pre-flight (create via migrator, emit trace+metric, query back) and rollback. Ships together or telemetry blackholes. go build ./pkg/... ./cmd/community = 0.
350 lines
10 KiB
Go
350 lines
10 KiB
Go
// Copyright (c) 2026 O11y, Inc.
|
|
// Copyright 2019 Prometheus Team
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package opsgenie
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"maps"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/o11y/pkg/alertmanager/alertmanagertemplate"
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/hanzoai/o11y/pkg/templating/markdownrenderer"
|
|
"github.com/hanzoai/o11y/pkg/types/alertmanagertypes"
|
|
commoncfg "github.com/prometheus/common/config"
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
"github.com/prometheus/alertmanager/notify"
|
|
"github.com/prometheus/alertmanager/template"
|
|
"github.com/prometheus/alertmanager/types"
|
|
)
|
|
|
|
const (
|
|
Integration = "opsgenie"
|
|
)
|
|
|
|
// https://docs.opsgenie.com/docs/alert-api - 130 characters meaning runes.
|
|
const maxMessageLenRunes = 130
|
|
|
|
// Notifier implements a Notifier for OpsGenie notifications.
|
|
type Notifier struct {
|
|
conf *config.OpsGenieConfig
|
|
tmpl *template.Template
|
|
logger *slog.Logger
|
|
client *http.Client
|
|
retrier *notify.Retrier
|
|
templater alertmanagertypes.Templater
|
|
}
|
|
|
|
// New returns a new OpsGenie notifier.
|
|
func New(c *config.OpsGenieConfig, t *template.Template, l *slog.Logger, templater alertmanagertypes.Templater, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
|
client, err := notify.NewClientWithTracing(*c.HTTPConfig, Integration, httpOpts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Notifier{
|
|
conf: c,
|
|
tmpl: t,
|
|
logger: l,
|
|
client: client,
|
|
retrier: ¬ify.Retrier{RetryCodes: []int{http.StatusTooManyRequests}},
|
|
templater: templater,
|
|
}, nil
|
|
}
|
|
|
|
type opsGenieCreateMessage struct {
|
|
Alias string `json:"alias"`
|
|
Message string `json:"message"`
|
|
Description string `json:"description,omitempty"`
|
|
Details map[string]string `json:"details"`
|
|
Source string `json:"source"`
|
|
Responders []opsGenieCreateMessageResponder `json:"responders,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
Note string `json:"note,omitempty"`
|
|
Priority string `json:"priority,omitempty"`
|
|
Entity string `json:"entity,omitempty"`
|
|
Actions []string `json:"actions,omitempty"`
|
|
}
|
|
|
|
type opsGenieCreateMessageResponder struct {
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Username string `json:"username,omitempty"`
|
|
Type string `json:"type"` // team, user, escalation, schedule etc.
|
|
}
|
|
|
|
type opsGenieCloseMessage struct {
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
type opsGenieUpdateMessageMessage struct {
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
type opsGenieUpdateDescriptionMessage struct {
|
|
Description string `json:"description,omitempty"`
|
|
}
|
|
|
|
// Notify implements the Notifier interface.
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
requests, retry, err := n.createRequests(ctx, as...)
|
|
if err != nil {
|
|
return retry, err
|
|
}
|
|
|
|
for _, req := range requests {
|
|
req.Header.Set("User-Agent", notify.UserAgentHeader)
|
|
resp, err := n.client.Do(req) //nolint:bodyclose
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
|
|
notify.Drain(resp)
|
|
if err != nil {
|
|
return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err)
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// Like Split but filter out empty strings.
|
|
func safeSplit(s, sep string) []string {
|
|
a := strings.Split(strings.TrimSpace(s), sep)
|
|
b := a[:0]
|
|
for _, x := range a {
|
|
if x != "" {
|
|
b = append(b, x)
|
|
}
|
|
}
|
|
return b
|
|
}
|
|
|
|
// prepareContent expands alert templates and returns the OpsGenie-ready title
|
|
// (truncated to the 130-rune limit) and HTML description. Custom bodies are
|
|
// rendered to HTML and stitched together with <hr> dividers; default bodies
|
|
// are joined with newlines (OpsGenie's legacy plain-text description).
|
|
func (n *Notifier) prepareContent(ctx context.Context, alerts []*types.Alert) (string, string, error) {
|
|
customTitle, customBody := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
|
|
result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
|
|
TitleTemplate: customTitle,
|
|
BodyTemplate: customBody,
|
|
DefaultTitleTemplate: n.conf.Message,
|
|
DefaultBodyTemplate: n.conf.Description,
|
|
}, alerts)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
var description string
|
|
if result.IsDefaultBody {
|
|
description = strings.Join(result.Body, "\n")
|
|
} else {
|
|
var b strings.Builder
|
|
first := true
|
|
for _, part := range result.Body {
|
|
if part == "" {
|
|
continue
|
|
}
|
|
rendered, renderErr := markdownrenderer.RenderHTML(part)
|
|
if renderErr != nil {
|
|
return "", "", renderErr
|
|
}
|
|
if !first {
|
|
b.WriteString("<hr>")
|
|
}
|
|
b.WriteString("<div>")
|
|
b.WriteString(rendered)
|
|
b.WriteString("</div>")
|
|
first = false
|
|
}
|
|
description = b.String()
|
|
}
|
|
|
|
title, truncated := notify.TruncateInRunes(result.Title, maxMessageLenRunes)
|
|
if truncated {
|
|
n.logger.WarnContext(ctx, "Truncated message", slog.Int("max_runes", maxMessageLenRunes))
|
|
}
|
|
|
|
return title, description, nil
|
|
}
|
|
|
|
// Create requests for a list of alerts.
|
|
func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*http.Request, bool, error) {
|
|
key, err := notify.ExtractGroupKey(ctx)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
logger := n.logger.With(slog.Any("group_key", key))
|
|
logger.DebugContext(ctx, "extracted group key")
|
|
|
|
data := notify.GetTemplateData(ctx, n.tmpl, as, logger)
|
|
|
|
tmpl := notify.TmplText(n.tmpl, data, &err)
|
|
|
|
details := make(map[string]string)
|
|
|
|
maps.Copy(details, data.CommonLabels)
|
|
|
|
for k, v := range n.conf.Details {
|
|
details[k] = tmpl(v)
|
|
}
|
|
|
|
requests := []*http.Request{}
|
|
|
|
var (
|
|
alias = key.Hash()
|
|
alerts = types.Alerts(as...)
|
|
)
|
|
switch alerts.Status() {
|
|
case model.AlertResolved:
|
|
resolvedEndpointURL := n.conf.APIURL.Copy()
|
|
resolvedEndpointURL.Path += fmt.Sprintf("v2/alerts/%s/close", alias)
|
|
q := resolvedEndpointURL.Query()
|
|
q.Set("identifierType", "alias")
|
|
resolvedEndpointURL.RawQuery = q.Encode()
|
|
msg := &opsGenieCloseMessage{Source: tmpl(n.conf.Source)}
|
|
var buf bytes.Buffer
|
|
if err := json.NewEncoder(&buf).Encode(msg); err != nil {
|
|
return nil, false, err
|
|
}
|
|
req, err := http.NewRequest("POST", resolvedEndpointURL.String(), &buf)
|
|
if err != nil {
|
|
return nil, true, err
|
|
}
|
|
requests = append(requests, req.WithContext(ctx))
|
|
default:
|
|
message, description, err := n.prepareContent(ctx, as)
|
|
if err != nil {
|
|
n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
|
|
return nil, false, err
|
|
}
|
|
|
|
createEndpointURL := n.conf.APIURL.Copy()
|
|
createEndpointURL.Path += "v2/alerts"
|
|
|
|
var responders []opsGenieCreateMessageResponder
|
|
for _, r := range n.conf.Responders {
|
|
responder := opsGenieCreateMessageResponder{
|
|
ID: tmpl(r.ID),
|
|
Name: tmpl(r.Name),
|
|
Username: tmpl(r.Username),
|
|
Type: tmpl(r.Type),
|
|
}
|
|
|
|
if responder == (opsGenieCreateMessageResponder{}) {
|
|
// Filter out empty responders. This is useful if you want to fill
|
|
// responders dynamically from alert's common labels.
|
|
continue
|
|
}
|
|
|
|
if responder.Type == "teams" {
|
|
teams := safeSplit(responder.Name, ",")
|
|
for _, team := range teams {
|
|
newResponder := opsGenieCreateMessageResponder{
|
|
Name: tmpl(team),
|
|
Type: tmpl("team"),
|
|
}
|
|
responders = append(responders, newResponder)
|
|
}
|
|
continue
|
|
}
|
|
|
|
responders = append(responders, responder)
|
|
}
|
|
|
|
msg := &opsGenieCreateMessage{
|
|
Alias: alias,
|
|
Message: message,
|
|
Description: description,
|
|
Details: details,
|
|
Source: tmpl(n.conf.Source),
|
|
Responders: responders,
|
|
Tags: safeSplit(tmpl(n.conf.Tags), ","),
|
|
Note: tmpl(n.conf.Note),
|
|
Priority: tmpl(n.conf.Priority),
|
|
Entity: tmpl(n.conf.Entity),
|
|
Actions: safeSplit(tmpl(n.conf.Actions), ","),
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := json.NewEncoder(&buf).Encode(msg); err != nil {
|
|
return nil, false, err
|
|
}
|
|
req, err := http.NewRequest("POST", createEndpointURL.String(), &buf)
|
|
if err != nil {
|
|
return nil, true, err
|
|
}
|
|
requests = append(requests, req.WithContext(ctx))
|
|
|
|
if n.conf.UpdateAlerts {
|
|
updateMessageEndpointURL := n.conf.APIURL.Copy()
|
|
updateMessageEndpointURL.Path += fmt.Sprintf("v2/alerts/%s/message", alias)
|
|
q := updateMessageEndpointURL.Query()
|
|
q.Set("identifierType", "alias")
|
|
updateMessageEndpointURL.RawQuery = q.Encode()
|
|
updateMsgMsg := &opsGenieUpdateMessageMessage{
|
|
Message: msg.Message,
|
|
}
|
|
var updateMessageBuf bytes.Buffer
|
|
if err := json.NewEncoder(&updateMessageBuf).Encode(updateMsgMsg); err != nil {
|
|
return nil, false, err
|
|
}
|
|
req, err := http.NewRequest("PUT", updateMessageEndpointURL.String(), &updateMessageBuf)
|
|
if err != nil {
|
|
return nil, true, err
|
|
}
|
|
requests = append(requests, req)
|
|
|
|
updateDescriptionEndpointURL := n.conf.APIURL.Copy()
|
|
updateDescriptionEndpointURL.Path += fmt.Sprintf("v2/alerts/%s/description", alias)
|
|
q = updateDescriptionEndpointURL.Query()
|
|
q.Set("identifierType", "alias")
|
|
updateDescriptionEndpointURL.RawQuery = q.Encode()
|
|
updateDescMsg := &opsGenieUpdateDescriptionMessage{
|
|
Description: msg.Description,
|
|
}
|
|
|
|
var updateDescriptionBuf bytes.Buffer
|
|
if err := json.NewEncoder(&updateDescriptionBuf).Encode(updateDescMsg); err != nil {
|
|
return nil, false, err
|
|
}
|
|
req, err = http.NewRequest("PUT", updateDescriptionEndpointURL.String(), &updateDescriptionBuf)
|
|
if err != nil {
|
|
return nil, true, err
|
|
}
|
|
requests = append(requests, req.WithContext(ctx))
|
|
}
|
|
}
|
|
|
|
var apiKey string
|
|
if n.conf.APIKey != "" {
|
|
apiKey = tmpl(string(n.conf.APIKey))
|
|
} else {
|
|
content, err := os.ReadFile(n.conf.APIKeyFile)
|
|
if err != nil {
|
|
return nil, false, errors.WrapInternalf(err, errors.CodeInternal, "read key_file error")
|
|
}
|
|
apiKey = tmpl(string(content))
|
|
apiKey = strings.TrimSpace(string(apiKey))
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, false, errors.WrapInternalf(err, errors.CodeInternal, "templating error")
|
|
}
|
|
|
|
for _, req := range requests {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", fmt.Sprintf("GenieKey %s", apiKey))
|
|
}
|
|
|
|
return requests, true, nil
|
|
}
|