Skip to content

Commit

Permalink
Merge pull request #20 from openconfig/config-origin
Browse files Browse the repository at this point in the history
Added config for gNMI notification origin
  • Loading branch information
LarsxGitHub authored Jan 6, 2025
2 parents fce24eb + 1e18167 commit 5742d6d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 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)
15 changes: 11 additions & 4 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 @@ -292,6 +296,7 @@ func (g *GNMI) typedValuesAndTimesFromMetric(m pmetric.Metric) ([]*gpb.TypedValu
return tvals, tstamps
}

// notificationsFromMetric returns a list of gNMI notifications based on a metric.
func (g *GNMI) notificationsFromMetric(p pmetric.Metric) []*gpb.Notification {
var notis []*gpb.Notification
values, timestamps := g.typedValuesAndTimesFromMetric(p)
Expand All @@ -303,6 +308,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 +319,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,9 +332,8 @@ 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 {
go func() {
for ms := range g.metricCh {
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v26.1.5+incompatible // indirect
github.com/docker/docker v26.1.4+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1399,8 +1399,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v26.1.5+incompatible h1:NEAxTwEjxV6VbBMBoGG3zPqbiJosIApZjxlbrG9q3/g=
github.com/docker/docker v26.1.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v26.1.4+incompatible h1:vuTpXDuoga+Z38m1OZHzl7NKisKWaWlhjQk7IDPSLsU=
github.com/docker/docker v26.1.4+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
Expand Down

0 comments on commit 5742d6d

Please sign in to comment.