Skip to content

Commit

Permalink
Merge pull request #4 from automixer/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
automixer authored Apr 6, 2024
2 parents 4acca39 + b1be85d commit 2e23a1a
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: devel_image
name: build_branch

on:
workflow_dispatch:

jobs:
devel_image:
build_branch:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down Expand Up @@ -37,5 +37,5 @@ jobs:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: "${{ github.repository }}:devel"
tags: "${{ github.repository }}:${{ github.ref.name }}"
labels: ${{ steps.meta.outputs.labels }}
3 changes: 2 additions & 1 deletion config-keys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
global:
instance_name: my_instance # Instance name. Defaults to "default".
metric_prefix: gnmi # The prefix to prepend to Prometheus metrics, also called "metric namespace".
# It must satisfy this regex: "^[a-zA-Z0-9_]*$"
# It must satisfy the regex ^[a-zA-Z0-9_]*$
listen_address: 0.0.0.0 # Prometheus exporter listen address. Defaults to 0.0.0.0
listen_port: 9456 # Prometheus exporter listen port. Defaults to 9456
scrape_interval: 1m # The scrape interval configured on Prometheus server. No less than 10 seconds.
Expand Down Expand Up @@ -43,6 +43,7 @@ devices:
desc_sanitize: <string> # Regex pattern of device description fields allowed characters. Defaults to
# "[a-zA-Z0-9_:\\-/]". Any character in the description that doesn't match
# the pattern will be removed
options: <plugin options> # Plugin specific options. See plugin-options.yaml

# gNMI related keys:
force_encoding: proto # Force the gNMI client to use a specific encoding. Acceptable values are:
Expand Down
19 changes: 9 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ module github.com/automixer/gtexporter
go 1.22.0

require (
github.com/golang/glog v1.2.0
github.com/golang/glog v1.2.1
github.com/openconfig/gnmi v0.11.0
github.com/openconfig/ygot v0.29.18
github.com/prometheus/client_golang v1.19.0
google.golang.org/grpc v1.62.1
google.golang.org/grpc v1.63.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/openconfig/goyang v1.4.5 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.51.1 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
38 changes: 18 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68=
github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/golang/glog v1.2.1 h1:OptwRhECazUx5ix5TTWC3EZhsZEHWcYWY4FQHTIubm4=
github.com/golang/glog v1.2.1/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand All @@ -41,8 +41,6 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down Expand Up @@ -77,12 +75,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos=
github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8=
github.com/prometheus/common v0.51.1 h1:eIjN50Bwglz6a/c3hAgSMcofL3nD+nFQkV6Dd4DsQCw=
github.com/prometheus/common v0.51.1/go.mod h1:lrWtQx+iDfn2mbH5GUzlH9TSHyfZpHkSiG1W7y3sF2Q=
github.com/prometheus/procfs v0.13.0 h1:GqzLlQyfsPbaEHaQkO7tbDlriv/4o5Hudv6OXHGKX7o=
github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43ZKY6tow0Y1g=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/protocolbuffers/txtpbfmt v0.0.0-20220608084003-fc78c767cd6a/go.mod h1:KjY0wibdYKc4DYkerHSbguaf3JeIPGhNJBp2BNiFH78=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand All @@ -96,8 +94,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand All @@ -114,8 +112,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -132,8 +130,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down Expand Up @@ -161,8 +159,8 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20210811021853-ddbe55d93216/go.mod h1:cFeNkxwySK631ADgubI+/XFU/xp8FD5KIVV4rj8UC5w=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa h1:RBgMaUMP+6soRkik4VoN8ojR2nex2TqZwjSSogic+eo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
Expand All @@ -171,8 +169,8 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.39.1/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/grpc v1.63.0 h1:WjKe+dnvABXyPJMD7KDNLxtoGk5tgk+YFWN6cBWjZE8=
google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down
14 changes: 13 additions & 1 deletion pkg/core/confmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
type yamlDevConfig struct {
Keys map[string]string `yaml:"devices,inline"`
Plugins []string `yaml:"plugins"`
Options map[string]string `yaml:"options"`
}

type yamlConfig struct {
Expand All @@ -46,17 +47,23 @@ func (c *Core) parseAppConfig(yCfg *yamlConfig) error {
return errors.New("no devices configured")
}
for i, devCfg := range yCfg.Devices {
// Keys
for k, v := range yCfg.Templates.Keys {
if devCfg.Keys[k] == "" {
yCfg.Devices[i].Keys[k] = v
}
}
// Plugin list
if devCfg.Plugins == nil {
if yCfg.Templates.Plugins == nil {
return errors.New("no plugins configured")
}
yCfg.Devices[i].Plugins = append(yCfg.Devices[i].Plugins, yCfg.Templates.Plugins...)
}
// Plugin options
if devCfg.Options == nil {
yCfg.Devices[i].Options = yCfg.Templates.Options
}
}

// Check devices cfg
Expand Down Expand Up @@ -113,7 +120,7 @@ func (c *Core) validateGlobalConfig(yCfg *yamlConfig) error {
return nil
}

// validateDeviceConfig validates a device section in the configuration file
// validateDeviceConfig checks if mandatory keys are present
func (c *Core) validateDeviceConfig(yCfg *yamlDevConfig) error {
if _, ok := yCfg.Keys["name"]; !ok {
return fmt.Errorf("device section must contain a device name")
Expand Down Expand Up @@ -197,6 +204,7 @@ func (c *Core) buildPluginCfg(yCfg *yamlConfig, index int) {
PlugName: plugName,
CustomLabel: src.Keys["custom_label"],
DescSanitize: src.Keys["desc_sanitize"],
Options: make(map[string]string),
}
// Default string values
if newPlug.DescSanitize == "" {
Expand All @@ -214,5 +222,9 @@ func (c *Core) buildPluginCfg(yCfg *yamlConfig, index int) {
scrapeInterval, _ := time.ParseDuration(yCfg.Global["scrape_interval"])
newPlug.ScrapeInterval = scrapeInterval
c.plugCfg[src.Keys["name"]] = append(c.plugCfg[src.Keys["name"]], newPlug)
// Plugin options
for k, v := range src.Options {
newPlug.Options[k] = v
}
}
}
48 changes: 24 additions & 24 deletions pkg/gnmiclient/gnmiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"math"
"net"
"os"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -57,11 +58,12 @@ type Config struct {
// GnmiClient The gNMI client object
type GnmiClient struct {
clientMon
config Config
shutdown func()
encoding gnmi.Encoding
plugins map[string]plugin // Map key: plugin name
xPaths map[string]plugin // Map key: subscribed xPath
config Config
shutdown func()
encoding gnmi.Encoding
plugins map[string]plugin // Map key: plugin name
xPaths map[string]plugin // Map key: subscribed xPath (schema path)
xPathList []string // Full paths list to be subscribed. Include YANG keys filter
}

// New Creates a new GnmiClient instance
Expand All @@ -87,31 +89,30 @@ func (c *GnmiClient) RegisterPlugin(name string, plug plugin) error {
c.plugins = make(map[string]plugin)
}
if _, ok := c.plugins[name]; ok {
return fmt.Errorf("plugin %s already registered", name)
return fmt.Errorf("plugin %s is already registered", name)
}
if plug == nil {
return fmt.Errorf("plugin parameter cannot be nil")
return fmt.Errorf("plugin cannot be nil")
}

// Before registering, check for duplicated xpath
if c.xPaths == nil {
c.xPaths = make(map[string]plugin)
}
plugPaths := plug.GetPathsToSubscribe()
re := regexp.MustCompile(`\[.*?]`)
for _, reqPath := range plugPaths {
for path := range c.xPaths {
if strings.HasPrefix(reqPath, path) {
return fmt.Errorf("path %s is already registered", reqPath)
}
}
c.xPathList = append(c.xPathList, reqPath)
// Remove keys from YANG path
reqPath = re.ReplaceAllString(reqPath, "")
c.xPaths[reqPath] = plug
}

c.plugins[name] = plug
return nil
}

// Start starts the goRoutine that take care of GNMI communication with the device
// Start starts the goRoutine that take care of GNMI channel
// It is non-blocking
func (c *GnmiClient) Start() error {
gCtx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -220,7 +221,7 @@ func (c *GnmiClient) checkCapabilities(ctx context.Context, stub gnmi.GNMIClient

// Pick an encoding
if c.config.ForceEncoding != "" {
// Encoding is enforced by config
// Config enforces encoding
c.config.ForceEncoding = strings.ToUpper(c.config.ForceEncoding)
switch c.config.ForceEncoding {
case "JSON":
Expand Down Expand Up @@ -269,10 +270,9 @@ func (c *GnmiClient) subscribe(ctx context.Context, stub gnmi.GNMIClient) (gnmi.

// Prepare Subscription struct slice
subscriptions := make([]*gnmi.Subscription, 0)
for _, plug := range c.plugins {
xPaths := plug.GetPathsToSubscribe()
for i := 0; i < len(c.plugins); i++ {
// One subscription for each plugin's path
for _, path := range xPaths {
for _, path := range c.xPathList {
p, err := ygot.StringToPath(path, ygot.StructuredPath, ygot.StringSlicePath)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -372,7 +372,7 @@ func (c *GnmiClient) routeSr(sr *gnmi.SubscribeResponse) {
}
c.plugins[nf.Prefix.Target].Notification(nf)
} else {
// Device does not support gnmi targeting or the subscription does not include a prefix target
// Device does not support gnmi targeting, or the subscription does not include a prefix target
pfx, _ := ygot.PathToSchemaPath(nf.Prefix)
if len(pfx) < 2 {
// Empty prefix
Expand Down Expand Up @@ -473,13 +473,8 @@ func (c *GnmiClient) run(ctx context.Context) {
}

// Dial
timeout := c.config.ScrapeInterval * timeoutMultiplier
if timeout > time.Minute*5 {
timeout = time.Minute * 5
}
gCtx, gCtxCancelFunc = context.WithTimeout(ctx, timeout)
log.Infof("Dialing %s...", c.config.DevName)
conn, err = grpc.DialContext(gCtx, targetDev, dialOpts...)
conn, err = grpc.NewClient(targetDev, dialOpts...)
if err != nil {
log.Info(err)
c.incDialErrors()
Expand All @@ -488,6 +483,11 @@ func (c *GnmiClient) run(ctx context.Context) {
stub = gnmi.NewGNMIClient(conn)

// Check capabilities
timeout := c.config.ScrapeInterval * timeoutMultiplier
if timeout > time.Minute*5 {
timeout = time.Minute * 5
}
gCtx, gCtxCancelFunc = context.WithTimeout(ctx, timeout)
log.Infof("Checking %s capabilities...", c.config.DevName)
if err = c.checkCapabilities(gCtx, stub); err != nil {
log.Info(err)
Expand Down
Loading

0 comments on commit 2e23a1a

Please sign in to comment.