Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config for gNMI notification origin #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading