Skip to content

Commit

Permalink
Added config for gNMI notification origin
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsxGitHub committed Oct 15, 2024
1 parent bd01d73 commit 061bd87
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
3 changes: 3 additions & 0 deletions gnmi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 10 additions & 3 deletions gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
{
Expand All @@ -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,
},
Expand All @@ -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 {
Expand Down
35 changes: 34 additions & 1 deletion gnmi/gnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestHandleMetrics(t *testing.T) {
name string
inCnt int
InMetricType pmetric.MetricType
inTarget string
inOrigin string
wantCnt int
}{
{
Expand All @@ -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,
Expand Down Expand Up @@ -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),
}
Expand All @@ -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)
}
}
})
}
}
Expand Down

0 comments on commit 061bd87

Please sign in to comment.