diff --git a/gnmi/config.go b/gnmi/config.go index 24c1578..eddb136 100644 --- a/gnmi/config.go +++ b/gnmi/config.go @@ -46,6 +46,9 @@ type Config struct { // Sep is the separator used in the metric name. Sep string `mapstructure:"sep"` + + // Origin is set as the origin of gNMI notifications. + Origin string `mapstructure:"origin"` } var _ component.Config = (*Config)(nil) diff --git a/gnmi/gnmi.go b/gnmi/gnmi.go index e1b2c33..1a8eac9 100644 --- a/gnmi/gnmi.go +++ b/gnmi/gnmi.go @@ -75,7 +75,11 @@ func NewGNMIExporter(logger *zap.Logger, cfg *Config) (*GNMI, error) { } func (g *GNMI) Start(_ context.Context, _ component.Host) error { - g.logger.Info("starting") + g.logger.Info("starting gNMI exporter", zap.String("addr", g.cfg.Addr), + zap.String("target", g.cfg.TargetName), zap.String("origin", g.cfg.Origin), + zap.String("sep", g.cfg.Sep), zap.Int("buffer_size", g.cfg.BufferSize), + zap.String("transport_security", g.cfg.TpSec), + ) reflection.Register(g.srv) gpb.RegisterGNMIServer(g.srv, g.telemSrv.GNMIServer) go g.srv.Serve(g.lis) @@ -303,6 +307,7 @@ func (g *GNMI) notificationsFromMetric(p pmetric.Metric) []*gpb.Notification { notis = append(notis, &gpb.Notification{ Timestamp: timestamps[i].AsTime().Unix(), Prefix: &gpb.Path{ + Origin: g.cfg.Origin, Target: g.cfg.TargetName, Elem: []*gpb.PathElem{ { @@ -313,7 +318,9 @@ func (g *GNMI) notificationsFromMetric(p pmetric.Metric) []*gpb.Notification { Update: []*gpb.Update{ { Path: &gpb.Path{ - Elem: g.toPathElems(p.Name()), + Target: g.cfg.TargetName, + Origin: g.cfg.Origin, + Elem: g.toPathElems(p.Name()), }, Val: val, }, @@ -324,7 +331,7 @@ func (g *GNMI) notificationsFromMetric(p pmetric.Metric) []*gpb.Notification { } // handleMetrics iterates over all received metrics and converts them into a -// gNMI update. This set of updates are then packed into a gNMI notfication +// gNMI update. This set of updates are then packed into a gNMI notification // and sent to the telemetry server. // Note: this currently supports only SUM metrics. func (g *GNMI) handleMetrics(_ gnmit.Queue, updateFn gnmit.UpdateFn, target string, cleanup func()) error { diff --git a/gnmi/gnmi_test.go b/gnmi/gnmi_test.go index 4ce6f0a..22c194f 100644 --- a/gnmi/gnmi_test.go +++ b/gnmi/gnmi_test.go @@ -30,6 +30,8 @@ func TestHandleMetrics(t *testing.T) { name string inCnt int InMetricType pmetric.MetricType + inTarget string + inOrigin string wantCnt int }{ { @@ -38,6 +40,28 @@ func TestHandleMetrics(t *testing.T) { InMetricType: pmetric.MetricTypeGauge, wantCnt: 20, }, + { + name: "gauge-10-with-target", + inCnt: 10, + InMetricType: pmetric.MetricTypeGauge, + inTarget: "moo-deng", + wantCnt: 20, + }, + { + name: "gauge-10-with-origin", + inCnt: 10, + InMetricType: pmetric.MetricTypeGauge, + inOrigin: "capybara", + wantCnt: 20, + }, + { + name: "gauge-10-with-target-and-origin", + inCnt: 10, + InMetricType: pmetric.MetricTypeGauge, + inTarget: "seals-on-ice-floe", + inOrigin: "orca-gang", + wantCnt: 20, + }, { name: "sum-10", inCnt: 10, @@ -67,8 +91,9 @@ func TestHandleMetrics(t *testing.T) { t.Run(tc.name, func(t *testing.T) { g := &GNMI{ cfg: &Config{ - TargetName: "target", + TargetName: tc.inTarget, Sep: "/", + Origin: tc.inOrigin, }, metricCh: make(chan *pmetric.Metrics, 10), } @@ -95,6 +120,14 @@ func TestHandleMetrics(t *testing.T) { if len(n.Update) != tc.wantCnt { t.Errorf("missing updates: want %d got %d", tc.wantCnt, len(n.Update)) } + for _, u := range n.Update { + if u.Path.Origin != tc.inOrigin { + t.Errorf("origin mismatch: want %s got %s", tc.inOrigin, u.Path.Origin) + } + if u.Path.Target != tc.inTarget { + t.Errorf("target mismatch: want %s got %s", tc.inTarget, u.Path.Target) + } + } }) } }