Files
o11y/pkg/query-service/app/opamp/configure_ingestionRules.go
zeekay b9f9cdf918 feat: bump o11y fork to latest SigNoz — full re-sync, builds green (§4c retained)
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).
2026-07-02 14:20:32 -07:00

146 lines
4.6 KiB
Go

package opamp
import (
"context"
"crypto/sha256"
"log/slog"
"github.com/knadh/koanf/parsers/yaml"
"github.com/open-telemetry/opamp-go/protobufs"
"go.opentelemetry.io/collector/confmap"
"github.com/hanzoai/o11y/pkg/errors"
model "github.com/hanzoai/o11y/pkg/query-service/app/opamp/model"
"github.com/hanzoai/o11y/pkg/query-service/app/opamp/otelconfig"
)
var (
CodeNoAgentsAvailable = errors.MustNewCode("no_agents_available")
CodeOpAmpServerDown = errors.MustNewCode("opamp_server_down")
CodeMultipleAgentsNotSupported = errors.MustNewCode("multiple_agents_not_supported")
)
// inserts or updates ingestion controller processors depending
// on the signal (metrics or traces)
func UpsertControlProcessors(ctx context.Context, signal string,
processors map[string]interface{}, callback model.OnChangeCallback,
) (string, error) {
// note: only processors enabled through tracesPipelinePlan will be added
// to pipeline. To enable or disable processors from pipeline, call
// AddToTracePipeline() or RemoveFromTracesPipeline() prior to calling
// this method
slog.Debug("initiating ingestion rules deployment config", "signal", signal, "processors", processors)
if signal != string(Metrics) && signal != string(Traces) {
slog.Error("received invalid signal in UpsertControlProcessors", "signal", signal)
return "", errors.NewInvalidInputf(errors.CodeInvalidInput, "signal not supported in ingestion rules: %s", signal)
}
if opAmpServer == nil {
return "", errors.NewInternalf(CodeOpAmpServerDown, "opamp server is down, unable to push config to agent at this moment")
}
agents := opAmpServer.agents.GetAllAgents()
if len(agents) == 0 {
return "", errors.NewInternalf(CodeNoAgentsAvailable, "no agents available at the moment")
}
if len(agents) > 1 && signal == string(Traces) {
slog.Debug("found multiple agents, this feature is not supported for traces pipeline (sampling rules)")
return "", errors.NewInvalidInputf(CodeMultipleAgentsNotSupported, "multiple agents not supported in sampling rules")
}
hash := ""
for _, agent := range agents {
agenthash, err := addIngestionControlToAgent(agent, signal, processors, false)
if err != nil {
slog.Error("failed to push ingestion rules config to agent", "agent_id", agent.AgentID, errors.Attr(err))
continue
}
if agenthash != "" {
// subscribe callback
model.ListenToConfigUpdate(agent.OrgID, agent.AgentID, agenthash, callback)
}
hash = agenthash
}
return hash, nil
}
// addIngestionControlToAgent adds ingestion contorl rules to agent config
func addIngestionControlToAgent(agent *model.Agent, signal string, processors map[string]interface{}, withLB bool) (string, error) {
confHash := ""
config := agent.Config
c, err := yaml.Parser().Unmarshal([]byte(config))
if err != nil {
return confHash, err
}
agentConf := confmap.NewFromStringMap(c)
// add ingestion control spec
err = makeIngestionControlSpec(agentConf, Signal(signal), processors)
if err != nil {
slog.Error("failed to prepare ingestion control processors for agent", "agent_id", agent.AgentID, errors.Attr(err))
return confHash, err
}
// ------ complete adding processor
configR, err := yaml.Parser().Marshal(agentConf.ToStringMap())
if err != nil {
return confHash, err
}
slog.Debug("sending new config", "config", string(configR))
hash := sha256.New()
_, err = hash.Write(configR)
if err != nil {
return confHash, err
}
confHash = string(hash.Sum(nil))
agent.Config = string(configR)
err = agent.Upsert()
if err != nil {
return confHash, err
}
agent.SendToAgent(&protobufs.ServerToAgent{
RemoteConfig: &protobufs.AgentRemoteConfig{
Config: &protobufs.AgentConfigMap{
ConfigMap: map[string]*protobufs.AgentConfigFile{
"collector.yaml": {
Body: configR,
ContentType: "application/x-yaml",
},
},
},
ConfigHash: []byte(confHash),
},
})
return string(confHash), nil
}
// prepare spec to introduce ingestion control in agent conf
func makeIngestionControlSpec(agentConf *confmap.Conf, signal Signal, processors map[string]interface{}) error {
configParser := otelconfig.NewConfigParser(agentConf)
configParser.UpdateProcessors(processors)
// edit pipeline if processor is missing
currentPipeline := configParser.PipelineProcessors(string(signal))
// merge tracesPipelinePlan with current pipeline
mergedPipeline, err := buildPipeline(signal, currentPipeline)
if err != nil {
slog.Error("failed to build pipeline", "signal", string(signal), errors.Attr(err))
return err
}
// add merged pipeline to the service
configParser.UpdateProcsInPipeline(string(signal), mergedPipeline)
return nil
}