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.
316 lines
8.1 KiB
Go
316 lines
8.1 KiB
Go
// Copyright (c) 2026 O11y, Inc.
|
|
// Copyright 2019 Prometheus Team
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package msteamsv2
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/o11y/pkg/alertmanager/alertmanagertemplate"
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"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 (
|
|
colorRed = "Attention"
|
|
colorGreen = "Good"
|
|
colorGrey = "Warning"
|
|
)
|
|
|
|
const (
|
|
Integration = "msteamsv2"
|
|
)
|
|
|
|
type Notifier struct {
|
|
conf *config.MSTeamsV2Config
|
|
titleLink string
|
|
tmpl *template.Template
|
|
logger *slog.Logger
|
|
client *http.Client
|
|
retrier *notify.Retrier
|
|
webhookURL *config.SecretURL
|
|
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
|
|
templater alertmanagertypes.Templater
|
|
}
|
|
|
|
// https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1#adaptivecarditemschema
|
|
type Content struct {
|
|
Schema string `json:"$schema"`
|
|
Type string `json:"type"`
|
|
Version string `json:"version"`
|
|
Body []Body `json:"body"`
|
|
Msteams Msteams `json:"msteams,omitzero"`
|
|
Actions []Action `json:"actions"`
|
|
}
|
|
|
|
type Body struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
Weight string `json:"weight,omitempty"`
|
|
Size string `json:"size,omitempty"`
|
|
Wrap bool `json:"wrap,omitempty"`
|
|
Style string `json:"style,omitempty"`
|
|
Color string `json:"color,omitempty"`
|
|
Facts []Fact `json:"facts,omitempty"`
|
|
}
|
|
|
|
type Action struct {
|
|
Type string `json:"type"`
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
type Fact struct {
|
|
Title string `json:"title"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
type Msteams struct {
|
|
Width string `json:"width"`
|
|
}
|
|
|
|
type Attachment struct {
|
|
ContentType string `json:"contentType"`
|
|
ContentURL *string `json:"contentUrl"` // Use a pointer to handle null values
|
|
Content Content `json:"content"`
|
|
}
|
|
|
|
type teamsMessage struct {
|
|
Type string `json:"type"`
|
|
Attachments []Attachment `json:"attachments"`
|
|
}
|
|
|
|
// New returns a new notifier that uses the Microsoft Teams Power Platform connector.
|
|
func New(c *config.MSTeamsV2Config, t *template.Template, titleLink string, 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
|
|
}
|
|
|
|
n := &Notifier{
|
|
conf: c,
|
|
titleLink: titleLink,
|
|
tmpl: t,
|
|
logger: l,
|
|
client: client,
|
|
retrier: ¬ify.Retrier{},
|
|
webhookURL: c.WebhookURL,
|
|
postJSONFunc: notify.PostJSON,
|
|
templater: templater,
|
|
}
|
|
|
|
return n, nil
|
|
}
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
key, err := notify.ExtractGroupKey(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
n.logger.DebugContext(ctx, "extracted group key", slog.String("key", string(key)))
|
|
|
|
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
tmpl := notify.TmplText(n.tmpl, data, &err)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
titleLink := tmpl(n.titleLink)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
var url string
|
|
if n.conf.WebhookURL != nil {
|
|
url = n.conf.WebhookURL.String()
|
|
} else {
|
|
content, err := os.ReadFile(n.conf.WebhookURLFile)
|
|
if err != nil {
|
|
return false, errors.WrapInternalf(err, errors.CodeInternal, "read webhook_url_file")
|
|
}
|
|
url = strings.TrimSpace(string(content))
|
|
}
|
|
|
|
bodyBlocks, err := n.prepareContent(ctx, as)
|
|
if err != nil {
|
|
n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
|
|
return false, err
|
|
}
|
|
|
|
// A message as referenced in https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1%2Cdotnet#request-body-schema
|
|
t := teamsMessage{
|
|
Type: "message",
|
|
Attachments: []Attachment{
|
|
{
|
|
ContentType: "application/vnd.microsoft.card.adaptive",
|
|
ContentURL: nil,
|
|
Content: Content{
|
|
Schema: "http://adaptivecards.io/schemas/adaptive-card.json",
|
|
Type: "AdaptiveCard",
|
|
Version: "1.2",
|
|
Body: bodyBlocks,
|
|
Actions: []Action{
|
|
{
|
|
Type: "Action.OpenUrl",
|
|
Title: "View Alert",
|
|
URL: titleLink,
|
|
},
|
|
},
|
|
Msteams: Msteams{
|
|
Width: "full",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
var payload bytes.Buffer
|
|
if err = json.NewEncoder(&payload).Encode(t); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
resp, err := n.postJSONFunc(ctx, n.client, url, &payload) //nolint:bodyclose
|
|
if err != nil {
|
|
return true, notify.RedactURL(err)
|
|
}
|
|
defer notify.Drain(resp) //drain is used to close the body of the response hence the nolint directive
|
|
|
|
// https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#rate-limiting-for-connectors
|
|
shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
|
|
if err != nil {
|
|
return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err)
|
|
}
|
|
return shouldRetry, err
|
|
}
|
|
|
|
// prepareContent builds the Adaptive Card body blocks for the notification.
|
|
// The first block is always the title; the remainder depends on whether the
|
|
// alerts carried a custom body template.
|
|
func (n *Notifier) prepareContent(ctx context.Context, alerts []*types.Alert) ([]Body, error) {
|
|
customTitle, customBody := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
|
|
result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
|
|
TitleTemplate: customTitle,
|
|
BodyTemplate: customBody,
|
|
DefaultTitleTemplate: n.conf.Title,
|
|
DefaultBodyTemplate: n.conf.Text,
|
|
}, alerts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
color := colorGrey
|
|
switch types.Alerts(alerts...).Status() {
|
|
case model.AlertFiring:
|
|
color = colorRed
|
|
case model.AlertResolved:
|
|
color = colorGreen
|
|
}
|
|
|
|
blocks := []Body{{
|
|
Type: "TextBlock",
|
|
Text: result.Title,
|
|
Weight: "Bolder",
|
|
Size: "Medium",
|
|
Wrap: true,
|
|
Style: "heading",
|
|
Color: color,
|
|
}}
|
|
|
|
if result.IsDefaultBody {
|
|
for _, alert := range alerts {
|
|
blocks = append(blocks, Body{
|
|
Type: "TextBlock",
|
|
Text: "Alerts",
|
|
Weight: "Bolder",
|
|
Size: "Medium",
|
|
Wrap: true,
|
|
Color: color,
|
|
})
|
|
blocks = append(blocks, n.createLabelsAndAnnotationsBody(alert)...)
|
|
}
|
|
return blocks, nil
|
|
}
|
|
|
|
// Custom body path: result.Body is positionally aligned with alerts;
|
|
// entries for alerts whose template rendered empty are kept as "" so we
|
|
// can skip them here without shifting the per-alert color index.
|
|
for i, body := range result.Body {
|
|
if body == "" || i >= len(alerts) {
|
|
continue
|
|
}
|
|
perAlertColor := colorRed
|
|
if alerts[i].Resolved() {
|
|
perAlertColor = colorGreen
|
|
}
|
|
blocks = append(blocks, Body{
|
|
Type: "TextBlock",
|
|
Text: body,
|
|
Wrap: true,
|
|
Color: perAlertColor,
|
|
})
|
|
}
|
|
return blocks, nil
|
|
}
|
|
|
|
func (*Notifier) createLabelsAndAnnotationsBody(alert *types.Alert) []Body {
|
|
bodies := []Body{}
|
|
bodies = append(bodies, Body{
|
|
Type: "TextBlock",
|
|
Text: "Labels",
|
|
Weight: "Bolder",
|
|
Size: "Medium",
|
|
})
|
|
|
|
facts := []Fact{}
|
|
for k, v := range alert.Labels {
|
|
if slices.Contains([]string{"alertname", "severity", "ruleId", "ruleSource"}, string(k)) {
|
|
continue
|
|
}
|
|
facts = append(facts, Fact{Title: string(k), Value: string(v)})
|
|
}
|
|
bodies = append(bodies, Body{
|
|
Type: "FactSet",
|
|
Facts: facts,
|
|
})
|
|
|
|
bodies = append(bodies, Body{
|
|
Type: "TextBlock",
|
|
Text: "Annotations",
|
|
Weight: "Bolder",
|
|
Size: "Medium",
|
|
})
|
|
|
|
annotationsFacts := []Fact{}
|
|
for k, v := range alert.Annotations {
|
|
if slices.Contains([]string{"summary", "related_logs", "related_traces"}, string(k)) ||
|
|
alertmanagertypes.IsPrivateAnnotation(string(k)) {
|
|
continue
|
|
}
|
|
annotationsFacts = append(annotationsFacts, Fact{Title: string(k), Value: string(v)})
|
|
}
|
|
|
|
bodies = append(bodies, Body{
|
|
Type: "FactSet",
|
|
Facts: annotationsFacts,
|
|
})
|
|
|
|
return bodies
|
|
}
|