scraper_test.go 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/mock" // client is an autogenerated mock type for the client type
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component/componenttest"
  13. "go.opentelemetry.io/collector/pdata/pmetric"
  14. "go.opentelemetry.io/collector/receiver/receivertest"
  15. "go.opentelemetry.io/collector/receiver/scrapererror"
  16. "go.uber.org/zap"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  19. )
  20. type MockClient struct {
  21. mock.Mock
  22. }
  23. // Close provides a mock function with given fields:
  24. func (_m *MockClient) Close() error {
  25. ret := _m.Called()
  26. var r0 error
  27. if rf, ok := ret.Get(0).(func() error); ok {
  28. r0 = rf()
  29. } else {
  30. r0 = ret.Error(0)
  31. }
  32. return r0
  33. }
  34. // Connect provides a mock function with given fields:
  35. func (_m *MockClient) Connect() error {
  36. ret := _m.Called()
  37. var r0 error
  38. if rf, ok := ret.Get(0).(func() error); ok {
  39. r0 = rf()
  40. } else {
  41. r0 = ret.Error(0)
  42. }
  43. return r0
  44. }
  45. // GetIndexedData provides a mock function with given fields: oids, scraperErrors
  46. func (_m *MockClient) GetIndexedData(oids []string, scraperErrors *scrapererror.ScrapeErrors) []SNMPData {
  47. ret := _m.Called(oids, scraperErrors)
  48. var r0 []SNMPData
  49. if rf, ok := ret.Get(0).(func([]string, *scrapererror.ScrapeErrors) []SNMPData); ok {
  50. r0 = rf(oids, scraperErrors)
  51. } else if ret.Get(0) != nil {
  52. r0 = ret.Get(0).([]SNMPData)
  53. }
  54. return r0
  55. }
  56. // GetScalarData provides a mock function with given fields: oids, scraperErrors
  57. func (_m *MockClient) GetScalarData(oids []string, scraperErrors *scrapererror.ScrapeErrors) []SNMPData {
  58. ret := _m.Called(oids, scraperErrors)
  59. var r0 []SNMPData
  60. if rf, ok := ret.Get(0).(func([]string, *scrapererror.ScrapeErrors) []SNMPData); ok {
  61. r0 = rf(oids, scraperErrors)
  62. } else if ret.Get(0) != nil {
  63. r0 = ret.Get(0).([]SNMPData)
  64. }
  65. return r0
  66. }
  67. func TestStart(t *testing.T) {
  68. testCases := []struct {
  69. desc string
  70. testFunc func(*testing.T)
  71. }{
  72. {
  73. desc: "Bad Config",
  74. testFunc: func(t *testing.T) {
  75. scraper := &snmpScraper{
  76. cfg: &Config{},
  77. settings: receivertest.NewNopCreateSettings(),
  78. }
  79. err := scraper.start(context.Background(), componenttest.NewNopHost())
  80. require.Error(t, err)
  81. },
  82. },
  83. {
  84. desc: "Valid Config",
  85. testFunc: func(t *testing.T) {
  86. scraper := &snmpScraper{
  87. cfg: createDefaultConfig().(*Config),
  88. settings: receivertest.NewNopCreateSettings(),
  89. }
  90. err := scraper.start(context.Background(), componenttest.NewNopHost())
  91. require.NoError(t, err)
  92. },
  93. },
  94. }
  95. for _, tc := range testCases {
  96. t.Run(tc.desc, tc.testFunc)
  97. }
  98. }
  99. func TestScrape(t *testing.T) {
  100. testCases := []struct {
  101. desc string
  102. testFunc func(*testing.T)
  103. }{
  104. {
  105. // Config is responsible for making sure this would never happen
  106. desc: "No Metric Configs returns no metrics with no error",
  107. testFunc: func(t *testing.T) {
  108. mockClient := new(MockClient)
  109. mockClient.On("Connect").Return(nil)
  110. mockClient.On("Close").Return(nil)
  111. scraper := &snmpScraper{
  112. cfg: &Config{},
  113. settings: receivertest.NewNopCreateSettings(),
  114. client: mockClient,
  115. logger: zap.NewNop(),
  116. }
  117. metrics, err := scraper.scrape(context.Background())
  118. require.NoError(t, err)
  119. require.Equal(t, metrics.MetricCount(), 0)
  120. },
  121. },
  122. {
  123. desc: "Scalar scrape errors and no indexed metric configs adds error",
  124. testFunc: func(t *testing.T) {
  125. mockClient := new(MockClient)
  126. clientErr := errors.New("problem getting scrape data")
  127. mockClient.On("Connect").Return(nil)
  128. mockClient.On("Close").Return(nil)
  129. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Run(
  130. func(args mock.Arguments) {
  131. scraperErrors := args.Get(1).(*scrapererror.ScrapeErrors)
  132. scraperErrors.AddPartial(1, clientErr)
  133. },
  134. ).Return([]SNMPData{})
  135. scraper := &snmpScraper{
  136. cfg: &Config{
  137. Metrics: map[string]*MetricConfig{
  138. "metric1": {
  139. ScalarOIDs: []ScalarOID{
  140. {
  141. OID: "1",
  142. },
  143. },
  144. },
  145. },
  146. },
  147. settings: receivertest.NewNopCreateSettings(),
  148. client: mockClient,
  149. logger: zap.NewNop(),
  150. }
  151. metrics, err := scraper.scrape(context.Background())
  152. require.EqualError(t, err, clientErr.Error())
  153. require.Equal(t, metrics.MetricCount(), 0)
  154. },
  155. },
  156. {
  157. desc: "Scalar scrape returns string data does not create metric",
  158. testFunc: func(t *testing.T) {
  159. mockClient := new(MockClient)
  160. oid := "1"
  161. clientSNMPData := SNMPData{
  162. oid: oid,
  163. value: "test",
  164. valueType: stringVal,
  165. }
  166. innerError := fmt.Errorf(errMsgBadValueType, oid)
  167. expectedScrapeErr := fmt.Errorf(errMsgScalarOIDProcessing, oid, innerError)
  168. mockClient.On("Connect").Return(nil)
  169. mockClient.On("Close").Return(nil)
  170. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{clientSNMPData})
  171. scraper := &snmpScraper{
  172. cfg: &Config{
  173. Metrics: map[string]*MetricConfig{
  174. "metric1": {
  175. ScalarOIDs: []ScalarOID{
  176. {
  177. OID: "1",
  178. },
  179. },
  180. },
  181. },
  182. },
  183. settings: receivertest.NewNopCreateSettings(),
  184. client: mockClient,
  185. logger: zap.NewNop(),
  186. }
  187. metrics, err := scraper.scrape(context.Background())
  188. require.EqualError(t, err, expectedScrapeErr.Error())
  189. require.Equal(t, metrics.MetricCount(), 0)
  190. },
  191. },
  192. {
  193. desc: "Simple scalar scrape creates int gauge metric (1)",
  194. testFunc: func(t *testing.T) {
  195. mockClient := new(MockClient)
  196. clientSNMPData := SNMPData{
  197. oid: ".1",
  198. value: int64(1),
  199. valueType: integerVal,
  200. }
  201. mockClient.On("Connect").Return(nil)
  202. mockClient.On("Close").Return(nil)
  203. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{clientSNMPData})
  204. scraper := &snmpScraper{
  205. cfg: &Config{
  206. Metrics: map[string]*MetricConfig{
  207. "metric1": {
  208. Description: "test description",
  209. Unit: "By",
  210. Gauge: &GaugeMetric{
  211. ValueType: "int",
  212. },
  213. ScalarOIDs: []ScalarOID{
  214. {
  215. OID: ".1",
  216. },
  217. },
  218. },
  219. },
  220. },
  221. settings: receivertest.NewNopCreateSettings(),
  222. client: mockClient,
  223. logger: zap.NewNop(),
  224. }
  225. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  226. goldenPath := filepath.Join("testdata", "expected_metrics", "1_scalar_metrics_golden.yaml")
  227. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  228. require.NoError(t, err)
  229. return expectedMetrics
  230. }
  231. expectedMetrics := expectedMetricGen(t)
  232. metrics, err := scraper.scrape(context.Background())
  233. require.NoError(t, err)
  234. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  235. require.NoError(t, err)
  236. },
  237. },
  238. {
  239. desc: "Simple scalar scrape with non '.' prefixed OID still creates metric (1)",
  240. testFunc: func(t *testing.T) {
  241. mockClient := new(MockClient)
  242. clientSNMPData := SNMPData{
  243. oid: ".1",
  244. value: int64(1),
  245. valueType: integerVal,
  246. }
  247. mockClient.On("Connect").Return(nil)
  248. mockClient.On("Close").Return(nil)
  249. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{clientSNMPData})
  250. scraper := &snmpScraper{
  251. cfg: &Config{
  252. Metrics: map[string]*MetricConfig{
  253. "metric1": {
  254. Description: "test description",
  255. Unit: "By",
  256. Gauge: &GaugeMetric{
  257. ValueType: "int",
  258. },
  259. ScalarOIDs: []ScalarOID{
  260. {
  261. OID: "1",
  262. },
  263. },
  264. },
  265. },
  266. },
  267. settings: receivertest.NewNopCreateSettings(),
  268. client: mockClient,
  269. logger: zap.NewNop(),
  270. }
  271. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  272. goldenPath := filepath.Join("testdata", "expected_metrics", "1_scalar_metrics_golden.yaml")
  273. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  274. require.NoError(t, err)
  275. return expectedMetrics
  276. }
  277. expectedMetrics := expectedMetricGen(t)
  278. metrics, err := scraper.scrape(context.Background())
  279. require.NoError(t, err)
  280. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  281. require.NoError(t, err)
  282. },
  283. },
  284. {
  285. desc: "Simple scalar scrape creates float cumulative monotonic sum metric (2)",
  286. testFunc: func(t *testing.T) {
  287. mockClient := new(MockClient)
  288. clientSNMPData := SNMPData{
  289. oid: ".1",
  290. value: float64(1.0),
  291. valueType: floatVal,
  292. }
  293. mockClient.On("Connect").Return(nil)
  294. mockClient.On("Close").Return(nil)
  295. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{clientSNMPData})
  296. scraper := &snmpScraper{
  297. cfg: &Config{
  298. Metrics: map[string]*MetricConfig{
  299. "metric1": {
  300. Description: "test description",
  301. Unit: "By",
  302. Sum: &SumMetric{
  303. Aggregation: "cumulative",
  304. Monotonic: true,
  305. ValueType: "double",
  306. },
  307. ScalarOIDs: []ScalarOID{
  308. {
  309. OID: ".1",
  310. },
  311. },
  312. },
  313. },
  314. },
  315. settings: receivertest.NewNopCreateSettings(),
  316. client: mockClient,
  317. logger: zap.NewNop(),
  318. }
  319. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  320. goldenPath := filepath.Join("testdata", "expected_metrics",
  321. "2_scalar_metrics_sum_cumulative_golden.yaml")
  322. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  323. require.NoError(t, err)
  324. return expectedMetrics
  325. }
  326. expectedMetrics := expectedMetricGen(t)
  327. metrics, err := scraper.scrape(context.Background())
  328. require.NoError(t, err)
  329. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  330. require.NoError(t, err)
  331. },
  332. },
  333. {
  334. desc: "Simple scalar scrape creates int delta non-monotonic sum metric (3)",
  335. testFunc: func(t *testing.T) {
  336. mockClient := new(MockClient)
  337. clientSNMPData := SNMPData{
  338. oid: ".1",
  339. value: int64(1),
  340. valueType: integerVal,
  341. }
  342. mockClient.On("Connect").Return(nil)
  343. mockClient.On("Close").Return(nil)
  344. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{clientSNMPData})
  345. scraper := &snmpScraper{
  346. cfg: &Config{
  347. Metrics: map[string]*MetricConfig{
  348. "metric1": {
  349. Description: "test description",
  350. Unit: "By",
  351. Sum: &SumMetric{
  352. Aggregation: "delta",
  353. Monotonic: false,
  354. ValueType: "int",
  355. },
  356. ScalarOIDs: []ScalarOID{
  357. {
  358. OID: ".1",
  359. },
  360. },
  361. },
  362. },
  363. },
  364. settings: receivertest.NewNopCreateSettings(),
  365. client: mockClient,
  366. logger: zap.NewNop(),
  367. }
  368. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  369. goldenPath := filepath.Join("testdata", "expected_metrics",
  370. "3_scalar_metrics_sum_delta_golden.yaml")
  371. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  372. require.NoError(t, err)
  373. return expectedMetrics
  374. }
  375. expectedMetrics := expectedMetricGen(t)
  376. metrics, err := scraper.scrape(context.Background())
  377. require.NoError(t, err)
  378. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  379. require.NoError(t, err)
  380. },
  381. },
  382. {
  383. desc: "Scalar scrape creates multiple metrics (4)",
  384. testFunc: func(t *testing.T) {
  385. mockClient := new(MockClient)
  386. snmpData1 := SNMPData{
  387. oid: ".1",
  388. value: int64(1),
  389. valueType: integerVal,
  390. }
  391. snmpData2 := SNMPData{
  392. oid: ".2",
  393. value: int64(2),
  394. valueType: integerVal,
  395. }
  396. mockClient.On("Connect").Return(nil)
  397. mockClient.On("Close").Return(nil)
  398. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  399. scraper := &snmpScraper{
  400. cfg: &Config{
  401. Metrics: map[string]*MetricConfig{
  402. "metric1": {
  403. Description: "test description",
  404. Unit: "By",
  405. Gauge: &GaugeMetric{
  406. ValueType: "int",
  407. },
  408. ScalarOIDs: []ScalarOID{
  409. {
  410. OID: ".1",
  411. },
  412. },
  413. },
  414. "metric2": {
  415. Description: "test description2",
  416. Unit: "{things}",
  417. Gauge: &GaugeMetric{
  418. ValueType: "int",
  419. },
  420. ScalarOIDs: []ScalarOID{
  421. {
  422. OID: ".2",
  423. },
  424. },
  425. },
  426. },
  427. },
  428. settings: receivertest.NewNopCreateSettings(),
  429. client: mockClient,
  430. logger: zap.NewNop(),
  431. }
  432. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  433. goldenPath := filepath.Join("testdata", "expected_metrics", "4_scalar_multiple_metrics_golden.yaml")
  434. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  435. require.NoError(t, err)
  436. return expectedMetrics
  437. }
  438. expectedMetrics := expectedMetricGen(t)
  439. metrics, err := scraper.scrape(context.Background())
  440. require.NoError(t, err)
  441. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  442. require.NoError(t, err)
  443. },
  444. },
  445. {
  446. desc: "Scalar scrape creates metric with attributes (5)",
  447. testFunc: func(t *testing.T) {
  448. mockClient := new(MockClient)
  449. clientSNMPData := SNMPData{
  450. oid: ".1",
  451. value: int64(1),
  452. valueType: integerVal,
  453. }
  454. mockClient.On("Connect").Return(nil)
  455. mockClient.On("Close").Return(nil)
  456. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{clientSNMPData})
  457. scraper := &snmpScraper{
  458. cfg: &Config{
  459. Attributes: map[string]*AttributeConfig{
  460. "attr1": {
  461. Enum: []string{"val1", "val2"},
  462. },
  463. "attr2": {
  464. Value: "attr2NewName",
  465. Enum: []string{"val1", "val2"},
  466. },
  467. },
  468. Metrics: map[string]*MetricConfig{
  469. "metric1": {
  470. Description: "test description",
  471. Unit: "By",
  472. Gauge: &GaugeMetric{
  473. ValueType: "int",
  474. },
  475. ScalarOIDs: []ScalarOID{
  476. {
  477. OID: ".1",
  478. Attributes: []Attribute{
  479. {
  480. Name: "attr1",
  481. Value: "val1",
  482. },
  483. {
  484. Name: "attr2",
  485. Value: "val2",
  486. },
  487. },
  488. },
  489. },
  490. },
  491. },
  492. },
  493. settings: receivertest.NewNopCreateSettings(),
  494. client: mockClient,
  495. logger: zap.NewNop(),
  496. }
  497. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  498. goldenPath := filepath.Join("testdata", "expected_metrics", "5_scalar_metric_w_attr_golden.yaml")
  499. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  500. require.NoError(t, err)
  501. return expectedMetrics
  502. }
  503. expectedMetrics := expectedMetricGen(t)
  504. metrics, err := scraper.scrape(context.Background())
  505. require.NoError(t, err)
  506. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  507. require.NoError(t, err)
  508. },
  509. },
  510. {
  511. desc: "Scalar scrape creates metric with multiple data points (6)",
  512. testFunc: func(t *testing.T) {
  513. mockClient := new(MockClient)
  514. snmpData1 := SNMPData{
  515. oid: ".1",
  516. value: int64(1),
  517. valueType: integerVal,
  518. }
  519. snmpData2 := SNMPData{
  520. oid: ".2",
  521. value: int64(2),
  522. valueType: integerVal,
  523. }
  524. mockClient.On("Connect").Return(nil)
  525. mockClient.On("Close").Return(nil)
  526. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  527. scraper := &snmpScraper{
  528. cfg: &Config{
  529. Attributes: map[string]*AttributeConfig{
  530. "attr1": {
  531. Enum: []string{"val1", "val2"},
  532. },
  533. },
  534. Metrics: map[string]*MetricConfig{
  535. "metric1": {
  536. Description: "test description",
  537. Unit: "By",
  538. Gauge: &GaugeMetric{
  539. ValueType: "int",
  540. },
  541. ScalarOIDs: []ScalarOID{
  542. {
  543. OID: ".1",
  544. Attributes: []Attribute{
  545. {
  546. Name: "attr1",
  547. Value: "val1",
  548. },
  549. },
  550. },
  551. {
  552. OID: ".2",
  553. Attributes: []Attribute{
  554. {
  555. Name: "attr1",
  556. Value: "val2",
  557. },
  558. },
  559. },
  560. },
  561. },
  562. },
  563. },
  564. settings: receivertest.NewNopCreateSettings(),
  565. client: mockClient,
  566. logger: zap.NewNop(),
  567. }
  568. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  569. goldenPath := filepath.Join("testdata", "expected_metrics",
  570. "6_scalar_metric_w_multi_datapoint_golden.yaml")
  571. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  572. require.NoError(t, err)
  573. return expectedMetrics
  574. }
  575. expectedMetrics := expectedMetricGen(t)
  576. metrics, err := scraper.scrape(context.Background())
  577. require.NoError(t, err)
  578. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  579. require.NoError(t, err)
  580. },
  581. },
  582. {
  583. desc: "Indexed scrape errors and no scalar metric configs adds error",
  584. testFunc: func(t *testing.T) {
  585. mockClient := new(MockClient)
  586. clientErr := errors.New("problem getting data")
  587. mockClient.On("Connect").Return(nil)
  588. mockClient.On("Close").Return(nil)
  589. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Run(
  590. func(args mock.Arguments) {
  591. scraperErrors := args.Get(1).(*scrapererror.ScrapeErrors)
  592. scraperErrors.AddPartial(1, clientErr)
  593. },
  594. ).Return([]SNMPData{})
  595. scraper := &snmpScraper{
  596. cfg: &Config{
  597. Metrics: map[string]*MetricConfig{
  598. "metric1": {
  599. ColumnOIDs: []ColumnOID{
  600. {
  601. OID: "1",
  602. },
  603. },
  604. },
  605. },
  606. },
  607. settings: receivertest.NewNopCreateSettings(),
  608. client: mockClient,
  609. logger: zap.NewNop(),
  610. }
  611. metrics, err := scraper.scrape(context.Background())
  612. require.EqualError(t, err, clientErr.Error())
  613. require.Equal(t, metrics.MetricCount(), 0)
  614. },
  615. },
  616. {
  617. desc: "Indexed metric scrape returns string data does not create metric",
  618. testFunc: func(t *testing.T) {
  619. mockClient := new(MockClient)
  620. columnOID := ".1"
  621. oid1 := ".1.1"
  622. oid2 := ".1.2"
  623. snmpData1 := SNMPData{
  624. columnOID: columnOID,
  625. oid: oid1,
  626. value: "test1",
  627. valueType: stringVal,
  628. }
  629. snmpData2 := SNMPData{
  630. columnOID: columnOID,
  631. oid: oid2,
  632. value: "test2",
  633. valueType: stringVal,
  634. }
  635. innerError1 := fmt.Errorf(errMsgBadValueType, oid1)
  636. expectedScrapeErr1 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid1, columnOID, innerError1)
  637. innerError2 := fmt.Errorf(errMsgBadValueType, oid2)
  638. expectedScrapeErr2 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid2, columnOID, innerError2)
  639. expectedScrapeErrMsg := expectedScrapeErr1.Error() + "; " + expectedScrapeErr2.Error()
  640. mockClient.On("Connect").Return(nil)
  641. mockClient.On("Close").Return(nil)
  642. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  643. scraper := &snmpScraper{
  644. cfg: &Config{
  645. Metrics: map[string]*MetricConfig{
  646. "metric1": {
  647. ColumnOIDs: []ColumnOID{
  648. {
  649. OID: ".1",
  650. },
  651. },
  652. },
  653. },
  654. },
  655. settings: receivertest.NewNopCreateSettings(),
  656. client: mockClient,
  657. logger: zap.NewNop(),
  658. }
  659. metrics, err := scraper.scrape(context.Background())
  660. require.EqualError(t, err, expectedScrapeErrMsg)
  661. require.Equal(t, metrics.MetricCount(), 0)
  662. },
  663. },
  664. {
  665. desc: "Simple indexed metric scrape creates int gauge metric on new no attribute resource (7)",
  666. testFunc: func(t *testing.T) {
  667. mockClient := new(MockClient)
  668. snmpData1 := SNMPData{
  669. columnOID: ".1",
  670. oid: ".1.1",
  671. value: int64(1),
  672. valueType: integerVal,
  673. }
  674. snmpData2 := SNMPData{
  675. columnOID: ".1",
  676. oid: ".1.2",
  677. value: int64(2),
  678. valueType: integerVal,
  679. }
  680. mockClient.On("Connect").Return(nil)
  681. mockClient.On("Close").Return(nil)
  682. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  683. scraper := &snmpScraper{
  684. cfg: &Config{
  685. Attributes: map[string]*AttributeConfig{
  686. "attr1": {
  687. IndexedValuePrefix: "attrPrefix",
  688. },
  689. },
  690. Metrics: map[string]*MetricConfig{
  691. "metric1": {
  692. Description: "test description",
  693. Unit: "By",
  694. Gauge: &GaugeMetric{
  695. ValueType: "int",
  696. },
  697. ColumnOIDs: []ColumnOID{
  698. {
  699. OID: ".1",
  700. Attributes: []Attribute{
  701. {
  702. Name: "attr1",
  703. },
  704. },
  705. },
  706. },
  707. },
  708. },
  709. },
  710. settings: receivertest.NewNopCreateSettings(),
  711. client: mockClient,
  712. logger: zap.NewNop(),
  713. }
  714. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  715. goldenPath := filepath.Join("testdata", "expected_metrics", "7_indexed_metrics_golden.yaml")
  716. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  717. require.NoError(t, err)
  718. return expectedMetrics
  719. }
  720. expectedMetrics := expectedMetricGen(t)
  721. metrics, err := scraper.scrape(context.Background())
  722. require.NoError(t, err)
  723. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  724. require.NoError(t, err)
  725. },
  726. },
  727. {
  728. desc: "Simple indexed metric scrape with non '.' prefixed OID still creates metric (7)",
  729. testFunc: func(t *testing.T) {
  730. mockClient := new(MockClient)
  731. snmpData1 := SNMPData{
  732. columnOID: ".1",
  733. oid: ".1.1",
  734. value: int64(1),
  735. valueType: integerVal,
  736. }
  737. snmpData2 := SNMPData{
  738. columnOID: ".1",
  739. oid: ".1.2",
  740. value: int64(2),
  741. valueType: integerVal,
  742. }
  743. mockClient.On("Connect").Return(nil)
  744. mockClient.On("Close").Return(nil)
  745. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  746. scraper := &snmpScraper{
  747. cfg: &Config{
  748. Attributes: map[string]*AttributeConfig{
  749. "attr1": {
  750. IndexedValuePrefix: "attrPrefix",
  751. },
  752. },
  753. Metrics: map[string]*MetricConfig{
  754. "metric1": {
  755. Description: "test description",
  756. Unit: "By",
  757. Gauge: &GaugeMetric{
  758. ValueType: "int",
  759. },
  760. ColumnOIDs: []ColumnOID{
  761. {
  762. OID: "1",
  763. Attributes: []Attribute{
  764. {
  765. Name: "attr1",
  766. },
  767. },
  768. },
  769. },
  770. },
  771. },
  772. },
  773. settings: receivertest.NewNopCreateSettings(),
  774. client: mockClient,
  775. logger: zap.NewNop(),
  776. }
  777. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  778. goldenPath := filepath.Join("testdata", "expected_metrics", "7_indexed_metrics_golden.yaml")
  779. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  780. require.NoError(t, err)
  781. return expectedMetrics
  782. }
  783. expectedMetrics := expectedMetricGen(t)
  784. metrics, err := scraper.scrape(context.Background())
  785. require.NoError(t, err)
  786. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  787. require.NoError(t, err)
  788. },
  789. },
  790. {
  791. desc: "Simple indexed metric scrape creates float cumulative monotonic sum metric (8)",
  792. testFunc: func(t *testing.T) {
  793. mockClient := new(MockClient)
  794. snmpData1 := SNMPData{
  795. columnOID: ".1",
  796. oid: ".1.1",
  797. value: float64(1.0),
  798. valueType: floatVal,
  799. }
  800. snmpData2 := SNMPData{
  801. columnOID: ".1",
  802. oid: ".1.2",
  803. value: float64(2.0),
  804. valueType: floatVal,
  805. }
  806. mockClient.On("Connect").Return(nil)
  807. mockClient.On("Close").Return(nil)
  808. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  809. scraper := &snmpScraper{
  810. cfg: &Config{
  811. Attributes: map[string]*AttributeConfig{
  812. "attr1": {
  813. IndexedValuePrefix: "attrPrefix",
  814. },
  815. },
  816. Metrics: map[string]*MetricConfig{
  817. "metric1": {
  818. Description: "test description",
  819. Unit: "By",
  820. Sum: &SumMetric{
  821. Aggregation: "cumulative",
  822. Monotonic: true,
  823. ValueType: "double",
  824. },
  825. ColumnOIDs: []ColumnOID{
  826. {
  827. OID: ".1",
  828. Attributes: []Attribute{
  829. {
  830. Name: "attr1",
  831. },
  832. },
  833. },
  834. },
  835. },
  836. },
  837. },
  838. settings: receivertest.NewNopCreateSettings(),
  839. client: mockClient,
  840. logger: zap.NewNop(),
  841. }
  842. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  843. goldenPath := filepath.Join("testdata", "expected_metrics",
  844. "8_indexed_metrics_sum_cumulative_golden.yaml")
  845. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  846. require.NoError(t, err)
  847. return expectedMetrics
  848. }
  849. expectedMetrics := expectedMetricGen(t)
  850. metrics, err := scraper.scrape(context.Background())
  851. require.NoError(t, err)
  852. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  853. require.NoError(t, err)
  854. },
  855. },
  856. {
  857. desc: "Simple indexed metric scrape creates int delta non-monotonic sum metric (9)",
  858. testFunc: func(t *testing.T) {
  859. mockClient := new(MockClient)
  860. snmpData1 := SNMPData{
  861. columnOID: ".1",
  862. oid: ".1.1",
  863. value: int64(1),
  864. valueType: integerVal,
  865. }
  866. snmpData2 := SNMPData{
  867. columnOID: ".1",
  868. oid: ".1.2",
  869. value: int64(2),
  870. valueType: integerVal,
  871. }
  872. mockClient.On("Connect").Return(nil)
  873. mockClient.On("Close").Return(nil)
  874. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  875. scraper := &snmpScraper{
  876. cfg: &Config{
  877. Attributes: map[string]*AttributeConfig{
  878. "attr1": {
  879. IndexedValuePrefix: "attrPrefix",
  880. },
  881. },
  882. Metrics: map[string]*MetricConfig{
  883. "metric1": {
  884. Description: "test description",
  885. Unit: "By",
  886. Sum: &SumMetric{
  887. Aggregation: "delta",
  888. Monotonic: false,
  889. ValueType: "int",
  890. },
  891. ColumnOIDs: []ColumnOID{
  892. {
  893. OID: ".1",
  894. Attributes: []Attribute{
  895. {
  896. Name: "attr1",
  897. },
  898. },
  899. },
  900. },
  901. },
  902. },
  903. },
  904. settings: receivertest.NewNopCreateSettings(),
  905. client: mockClient,
  906. logger: zap.NewNop(),
  907. }
  908. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  909. goldenPath := filepath.Join("testdata", "expected_metrics",
  910. "9_indexed_metrics_sum_delta_golden.yaml")
  911. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  912. require.NoError(t, err)
  913. return expectedMetrics
  914. }
  915. expectedMetrics := expectedMetricGen(t)
  916. metrics, err := scraper.scrape(context.Background())
  917. require.NoError(t, err)
  918. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  919. require.NoError(t, err)
  920. },
  921. },
  922. {
  923. desc: "Indexed metric scrape creates metric on existing resource if scalar metrics were scraped (10)",
  924. testFunc: func(t *testing.T) {
  925. mockClient := new(MockClient)
  926. snmpData0 := SNMPData{
  927. oid: ".0",
  928. value: int64(0),
  929. valueType: integerVal,
  930. }
  931. snmpData1 := SNMPData{
  932. columnOID: ".1",
  933. oid: ".1.1",
  934. value: int64(1),
  935. valueType: integerVal,
  936. }
  937. snmpData2 := SNMPData{
  938. columnOID: ".1",
  939. oid: ".1.2",
  940. value: int64(2),
  941. valueType: integerVal,
  942. }
  943. mockClient.On("Connect").Return(nil)
  944. mockClient.On("Close").Return(nil)
  945. mockClient.On("GetScalarData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0})
  946. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData1, snmpData2})
  947. scraper := &snmpScraper{
  948. cfg: &Config{
  949. Attributes: map[string]*AttributeConfig{
  950. "attr1": {
  951. IndexedValuePrefix: "attrPrefix",
  952. },
  953. },
  954. Metrics: map[string]*MetricConfig{
  955. "metric0": {
  956. Description: "test description0",
  957. Unit: "By",
  958. Gauge: &GaugeMetric{
  959. ValueType: "int",
  960. },
  961. ScalarOIDs: []ScalarOID{
  962. {
  963. OID: ".0",
  964. },
  965. },
  966. },
  967. "metric1": {
  968. Description: "test description1",
  969. Unit: "By",
  970. Gauge: &GaugeMetric{
  971. ValueType: "int",
  972. },
  973. ColumnOIDs: []ColumnOID{
  974. {
  975. OID: ".1",
  976. Attributes: []Attribute{
  977. {
  978. Name: "attr1",
  979. },
  980. },
  981. },
  982. },
  983. },
  984. },
  985. },
  986. settings: receivertest.NewNopCreateSettings(),
  987. client: mockClient,
  988. logger: zap.NewNop(),
  989. }
  990. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  991. goldenPath := filepath.Join("testdata", "expected_metrics",
  992. "10_indexed_scalar_metrics_same_resource_golden.yaml")
  993. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  994. require.NoError(t, err)
  995. return expectedMetrics
  996. }
  997. expectedMetrics := expectedMetricGen(t)
  998. metrics, err := scraper.scrape(context.Background())
  999. require.NoError(t, err)
  1000. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1001. require.NoError(t, err)
  1002. },
  1003. },
  1004. {
  1005. desc: "Indexed metric scrape creates multiple metrics on same no attribute resource (11)",
  1006. testFunc: func(t *testing.T) {
  1007. mockClient := new(MockClient)
  1008. snmpData0 := SNMPData{
  1009. columnOID: ".0",
  1010. oid: ".0.1",
  1011. value: int64(0),
  1012. valueType: integerVal,
  1013. }
  1014. snmpData1 := SNMPData{
  1015. columnOID: ".0",
  1016. oid: ".0.2",
  1017. value: int64(1),
  1018. valueType: integerVal,
  1019. }
  1020. snmpData2 := SNMPData{
  1021. columnOID: ".1",
  1022. oid: ".1.1",
  1023. value: int64(2),
  1024. valueType: integerVal,
  1025. }
  1026. snmpData3 := SNMPData{
  1027. columnOID: ".1",
  1028. oid: ".1.2",
  1029. value: int64(3),
  1030. valueType: integerVal,
  1031. }
  1032. mockClient.On("Connect").Return(nil)
  1033. mockClient.On("Close").Return(nil)
  1034. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0, snmpData1, snmpData2, snmpData3})
  1035. scraper := &snmpScraper{
  1036. cfg: &Config{
  1037. Attributes: map[string]*AttributeConfig{
  1038. "attr1": {
  1039. IndexedValuePrefix: "attrPrefix",
  1040. },
  1041. },
  1042. Metrics: map[string]*MetricConfig{
  1043. "metric0": {
  1044. Description: "test description0",
  1045. Unit: "By",
  1046. Gauge: &GaugeMetric{
  1047. ValueType: "int",
  1048. },
  1049. ColumnOIDs: []ColumnOID{
  1050. {
  1051. OID: ".0",
  1052. Attributes: []Attribute{
  1053. {
  1054. Name: "attr1",
  1055. },
  1056. },
  1057. },
  1058. },
  1059. },
  1060. "metric1": {
  1061. Description: "test description1",
  1062. Unit: "By",
  1063. Gauge: &GaugeMetric{
  1064. ValueType: "int",
  1065. },
  1066. ColumnOIDs: []ColumnOID{
  1067. {
  1068. OID: ".1",
  1069. Attributes: []Attribute{
  1070. {
  1071. Name: "attr1",
  1072. },
  1073. },
  1074. },
  1075. },
  1076. },
  1077. },
  1078. },
  1079. settings: receivertest.NewNopCreateSettings(),
  1080. client: mockClient,
  1081. logger: zap.NewNop(),
  1082. }
  1083. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1084. goldenPath := filepath.Join("testdata", "expected_metrics",
  1085. "11_indexed_multiple_metrics_same_resource_golden.yaml")
  1086. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1087. require.NoError(t, err)
  1088. return expectedMetrics
  1089. }
  1090. expectedMetrics := expectedMetricGen(t)
  1091. metrics, err := scraper.scrape(context.Background())
  1092. require.NoError(t, err)
  1093. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1094. require.NoError(t, err)
  1095. },
  1096. },
  1097. {
  1098. desc: "Indexed metric scrape creates with all attribute config creates metric with all attributes (12)",
  1099. testFunc: func(t *testing.T) {
  1100. mockClient := new(MockClient)
  1101. snmpData0 := SNMPData{
  1102. columnOID: ".0",
  1103. oid: ".0.1",
  1104. value: "thing1",
  1105. valueType: stringVal,
  1106. }
  1107. snmpData1 := SNMPData{
  1108. columnOID: ".0",
  1109. oid: ".0.2",
  1110. value: "thing2",
  1111. valueType: stringVal,
  1112. }
  1113. snmpData2 := SNMPData{
  1114. columnOID: ".1",
  1115. oid: ".1.1",
  1116. value: int64(1),
  1117. valueType: integerVal,
  1118. }
  1119. snmpData3 := SNMPData{
  1120. columnOID: ".1",
  1121. oid: ".1.2",
  1122. value: int64(2),
  1123. valueType: integerVal,
  1124. }
  1125. mockClient.On("Connect").Return(nil)
  1126. mockClient.On("Close").Return(nil)
  1127. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1128. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1129. scraper := &snmpScraper{
  1130. cfg: &Config{
  1131. Attributes: map[string]*AttributeConfig{
  1132. "attr1": {
  1133. IndexedValuePrefix: "attrPrefix",
  1134. },
  1135. "attr2": {
  1136. Value: "attr2NewName",
  1137. Enum: []string{"val1", "val2"},
  1138. },
  1139. "attr3": {
  1140. OID: ".0",
  1141. },
  1142. },
  1143. Metrics: map[string]*MetricConfig{
  1144. "metric1": {
  1145. Description: "test description",
  1146. Unit: "By",
  1147. Gauge: &GaugeMetric{
  1148. ValueType: "int",
  1149. },
  1150. ColumnOIDs: []ColumnOID{
  1151. {
  1152. OID: ".1",
  1153. Attributes: []Attribute{
  1154. {
  1155. Name: "attr1",
  1156. },
  1157. {
  1158. Name: "attr2",
  1159. Value: "val2",
  1160. },
  1161. {
  1162. Name: "attr3",
  1163. },
  1164. },
  1165. },
  1166. },
  1167. },
  1168. },
  1169. },
  1170. settings: receivertest.NewNopCreateSettings(),
  1171. client: mockClient,
  1172. logger: zap.NewNop(),
  1173. }
  1174. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1175. goldenPath := filepath.Join("testdata", "expected_metrics",
  1176. "12_indexed_metrics_w_all_attrs_golden.yaml")
  1177. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1178. require.NoError(t, err)
  1179. return expectedMetrics
  1180. }
  1181. expectedMetrics := expectedMetricGen(t)
  1182. metrics, err := scraper.scrape(context.Background())
  1183. require.NoError(t, err)
  1184. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1185. require.NoError(t, err)
  1186. },
  1187. },
  1188. {
  1189. desc: "Indexed attribute scrape error does not create metric datapoints",
  1190. testFunc: func(t *testing.T) {
  1191. mockClient := new(MockClient)
  1192. clientErr := errors.New("problem getting data")
  1193. columnOID := ".1"
  1194. oid1 := ".1.1"
  1195. oid2 := ".1.2"
  1196. snmpData0 := SNMPData{
  1197. columnOID: columnOID,
  1198. oid: oid1,
  1199. value: int64(1),
  1200. valueType: integerVal,
  1201. }
  1202. snmpData1 := SNMPData{
  1203. columnOID: columnOID,
  1204. oid: oid2,
  1205. value: int64(2),
  1206. valueType: integerVal,
  1207. }
  1208. mockClient.On("Connect").Return(nil)
  1209. mockClient.On("Close").Return(nil)
  1210. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Run(
  1211. func(args mock.Arguments) {
  1212. scraperErrors := args.Get(1).(*scrapererror.ScrapeErrors)
  1213. scraperErrors.AddPartial(1, clientErr)
  1214. },
  1215. ).Return([]SNMPData{}).Once()
  1216. metricName := "metric1"
  1217. innerInnerErr := errors.New(errMsgAttributeEmptyValue)
  1218. innerErr := fmt.Errorf(errMsgOIDAttributeEmptyValue, metricName, innerInnerErr)
  1219. expectedErr1 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid1, columnOID, innerErr)
  1220. expectedErr2 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid2, columnOID, innerErr)
  1221. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1222. scraper := &snmpScraper{
  1223. cfg: &Config{
  1224. Attributes: map[string]*AttributeConfig{
  1225. "attr1": {
  1226. OID: ".0",
  1227. },
  1228. },
  1229. Metrics: map[string]*MetricConfig{
  1230. "metric1": {
  1231. Description: "test description",
  1232. Unit: "By",
  1233. Gauge: &GaugeMetric{
  1234. ValueType: "int",
  1235. },
  1236. ColumnOIDs: []ColumnOID{
  1237. {
  1238. OID: ".1",
  1239. Attributes: []Attribute{
  1240. {
  1241. Name: "attr1",
  1242. },
  1243. },
  1244. },
  1245. },
  1246. },
  1247. },
  1248. },
  1249. settings: receivertest.NewNopCreateSettings(),
  1250. client: mockClient,
  1251. logger: zap.NewNop(),
  1252. }
  1253. metrics, err := scraper.scrape(context.Background())
  1254. require.EqualError(t, err, fmt.Sprintf("%s; %s; %s", clientErr, expectedErr1, expectedErr2))
  1255. require.Equal(t, metrics.MetricCount(), 0)
  1256. },
  1257. },
  1258. {
  1259. desc: "Indexed attribute scrape returning bad value type does not create metric data points",
  1260. testFunc: func(t *testing.T) {
  1261. mockClient := new(MockClient)
  1262. columnOID1 := ".0"
  1263. columnOID2 := ".1"
  1264. oid1 := ".0.1"
  1265. oid2 := ".0.2"
  1266. oid3 := ".1.1"
  1267. oid4 := ".1.2"
  1268. metricName := "metric1"
  1269. snmpData0 := SNMPData{
  1270. columnOID: columnOID1,
  1271. oid: oid1,
  1272. value: true,
  1273. valueType: notSupportedVal,
  1274. }
  1275. snmpData1 := SNMPData{
  1276. columnOID: columnOID1,
  1277. oid: oid2,
  1278. value: false,
  1279. valueType: notSupportedVal,
  1280. }
  1281. snmpData2 := SNMPData{
  1282. columnOID: columnOID2,
  1283. oid: oid3,
  1284. value: int64(1),
  1285. valueType: integerVal,
  1286. }
  1287. snmpData3 := SNMPData{
  1288. columnOID: columnOID2,
  1289. oid: oid4,
  1290. value: int64(2),
  1291. valueType: integerVal,
  1292. }
  1293. mockClient.On("Connect").Return(nil)
  1294. mockClient.On("Close").Return(nil)
  1295. innerErr1 := fmt.Errorf(errMsgIndexedAttributesBadValueType, oid1, columnOID1)
  1296. expectedErr1 := fmt.Errorf(errMsgIndexedAttributeOIDProcessing, oid1, columnOID1, innerErr1)
  1297. innerErr2 := fmt.Errorf(errMsgIndexedAttributesBadValueType, oid2, columnOID1)
  1298. expectedErr2 := fmt.Errorf(errMsgIndexedAttributeOIDProcessing, oid2, columnOID1, innerErr2)
  1299. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1300. innerInnerErr := errors.New(errMsgAttributeEmptyValue)
  1301. innerErr := fmt.Errorf(errMsgOIDAttributeEmptyValue, metricName, innerInnerErr)
  1302. expectedErr3 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid3, columnOID2, innerErr)
  1303. expectedErr4 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid4, columnOID2, innerErr)
  1304. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1305. scraper := &snmpScraper{
  1306. cfg: &Config{
  1307. Attributes: map[string]*AttributeConfig{
  1308. "attr1": {
  1309. OID: ".0",
  1310. },
  1311. },
  1312. Metrics: map[string]*MetricConfig{
  1313. "metric1": {
  1314. Description: "test description",
  1315. Unit: "By",
  1316. Gauge: &GaugeMetric{
  1317. ValueType: "int",
  1318. },
  1319. ColumnOIDs: []ColumnOID{
  1320. {
  1321. OID: ".1",
  1322. Attributes: []Attribute{
  1323. {
  1324. Name: "attr1",
  1325. },
  1326. },
  1327. },
  1328. },
  1329. },
  1330. },
  1331. },
  1332. settings: receivertest.NewNopCreateSettings(),
  1333. client: mockClient,
  1334. logger: zap.NewNop(),
  1335. }
  1336. metrics, err := scraper.scrape(context.Background())
  1337. require.EqualError(t, err, fmt.Sprintf("%s; %s; %s; %s", expectedErr1, expectedErr2, expectedErr3, expectedErr4))
  1338. require.Equal(t, metrics.MetricCount(), 0)
  1339. },
  1340. },
  1341. {
  1342. desc: "Indexed scrape for metric and attributes creates a metric with indexed attributes (13)",
  1343. testFunc: func(t *testing.T) {
  1344. mockClient := new(MockClient)
  1345. snmpData0 := SNMPData{
  1346. columnOID: ".0",
  1347. oid: ".0.1",
  1348. value: "thing1",
  1349. valueType: stringVal,
  1350. }
  1351. snmpData1 := SNMPData{
  1352. columnOID: ".0",
  1353. oid: ".0.2",
  1354. value: "thing2",
  1355. valueType: stringVal,
  1356. }
  1357. snmpData2 := SNMPData{
  1358. columnOID: ".1",
  1359. oid: ".1.1",
  1360. value: int64(1),
  1361. valueType: integerVal,
  1362. }
  1363. snmpData3 := SNMPData{
  1364. columnOID: ".1",
  1365. oid: ".1.2",
  1366. value: int64(2),
  1367. valueType: integerVal,
  1368. }
  1369. mockClient.On("Connect").Return(nil)
  1370. mockClient.On("Close").Return(nil)
  1371. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1372. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1373. scraper := &snmpScraper{
  1374. cfg: &Config{
  1375. Attributes: map[string]*AttributeConfig{
  1376. "attr1": {
  1377. OID: ".0",
  1378. },
  1379. },
  1380. Metrics: map[string]*MetricConfig{
  1381. "metric1": {
  1382. Description: "test description",
  1383. Unit: "By",
  1384. Gauge: &GaugeMetric{
  1385. ValueType: "int",
  1386. },
  1387. ColumnOIDs: []ColumnOID{
  1388. {
  1389. OID: ".1",
  1390. Attributes: []Attribute{
  1391. {
  1392. Name: "attr1",
  1393. },
  1394. },
  1395. },
  1396. },
  1397. },
  1398. },
  1399. },
  1400. settings: receivertest.NewNopCreateSettings(),
  1401. client: mockClient,
  1402. logger: zap.NewNop(),
  1403. }
  1404. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1405. goldenPath := filepath.Join("testdata", "expected_metrics",
  1406. "13_indexed_metrics_w_column_oid_attr_golden.yaml")
  1407. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1408. require.NoError(t, err)
  1409. return expectedMetrics
  1410. }
  1411. expectedMetrics := expectedMetricGen(t)
  1412. metrics, err := scraper.scrape(context.Background())
  1413. require.NoError(t, err)
  1414. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1415. require.NoError(t, err)
  1416. },
  1417. },
  1418. {
  1419. desc: "Indexed metric scrape with non '.' prefixed OID for attribute still creates metric (13)",
  1420. testFunc: func(t *testing.T) {
  1421. mockClient := new(MockClient)
  1422. snmpData0 := SNMPData{
  1423. columnOID: ".0",
  1424. oid: ".0.1",
  1425. value: "thing1",
  1426. valueType: stringVal,
  1427. }
  1428. snmpData1 := SNMPData{
  1429. columnOID: ".0",
  1430. oid: ".0.2",
  1431. value: "thing2",
  1432. valueType: stringVal,
  1433. }
  1434. snmpData2 := SNMPData{
  1435. columnOID: ".1",
  1436. oid: ".1.1",
  1437. value: int64(1),
  1438. valueType: integerVal,
  1439. }
  1440. snmpData3 := SNMPData{
  1441. columnOID: ".1",
  1442. oid: ".1.2",
  1443. value: int64(2),
  1444. valueType: integerVal,
  1445. }
  1446. mockClient.On("Connect").Return(nil)
  1447. mockClient.On("Close").Return(nil)
  1448. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1449. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1450. scraper := &snmpScraper{
  1451. cfg: &Config{
  1452. Attributes: map[string]*AttributeConfig{
  1453. "attr1": {
  1454. OID: "0",
  1455. },
  1456. },
  1457. Metrics: map[string]*MetricConfig{
  1458. "metric1": {
  1459. Description: "test description",
  1460. Unit: "By",
  1461. Gauge: &GaugeMetric{
  1462. ValueType: "int",
  1463. },
  1464. ColumnOIDs: []ColumnOID{
  1465. {
  1466. OID: "1",
  1467. Attributes: []Attribute{
  1468. {
  1469. Name: "attr1",
  1470. },
  1471. },
  1472. },
  1473. },
  1474. },
  1475. },
  1476. },
  1477. settings: receivertest.NewNopCreateSettings(),
  1478. client: mockClient,
  1479. logger: zap.NewNop(),
  1480. }
  1481. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1482. goldenPath := filepath.Join("testdata", "expected_metrics",
  1483. "13_indexed_metrics_w_column_oid_attr_golden.yaml")
  1484. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1485. require.NoError(t, err)
  1486. return expectedMetrics
  1487. }
  1488. expectedMetrics := expectedMetricGen(t)
  1489. metrics, err := scraper.scrape(context.Background())
  1490. require.NoError(t, err)
  1491. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1492. require.NoError(t, err)
  1493. },
  1494. },
  1495. {
  1496. desc: "SNMP float value for indexed attribute still creates metric (14)",
  1497. testFunc: func(t *testing.T) {
  1498. mockClient := new(MockClient)
  1499. snmpData0 := SNMPData{
  1500. columnOID: ".0",
  1501. oid: ".0.1",
  1502. value: 1.11111111,
  1503. valueType: floatVal,
  1504. }
  1505. snmpData1 := SNMPData{
  1506. columnOID: ".0",
  1507. oid: ".0.2",
  1508. value: 2.22222222,
  1509. valueType: floatVal,
  1510. }
  1511. snmpData2 := SNMPData{
  1512. columnOID: ".1",
  1513. oid: ".1.1",
  1514. value: int64(1),
  1515. valueType: integerVal,
  1516. }
  1517. snmpData3 := SNMPData{
  1518. columnOID: ".1",
  1519. oid: ".1.2",
  1520. value: int64(2),
  1521. valueType: integerVal,
  1522. }
  1523. mockClient.On("Connect").Return(nil)
  1524. mockClient.On("Close").Return(nil)
  1525. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1526. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1527. scraper := &snmpScraper{
  1528. cfg: &Config{
  1529. Attributes: map[string]*AttributeConfig{
  1530. "attr1": {
  1531. OID: ".0",
  1532. },
  1533. },
  1534. Metrics: map[string]*MetricConfig{
  1535. "metric1": {
  1536. Description: "test description",
  1537. Unit: "By",
  1538. Gauge: &GaugeMetric{
  1539. ValueType: "int",
  1540. },
  1541. ColumnOIDs: []ColumnOID{
  1542. {
  1543. OID: ".1",
  1544. Attributes: []Attribute{
  1545. {
  1546. Name: "attr1",
  1547. },
  1548. },
  1549. },
  1550. },
  1551. },
  1552. },
  1553. },
  1554. settings: receivertest.NewNopCreateSettings(),
  1555. client: mockClient,
  1556. logger: zap.NewNop(),
  1557. }
  1558. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1559. goldenPath := filepath.Join("testdata", "expected_metrics",
  1560. "14_indexed_column_oid_float_attr_golden.yaml")
  1561. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1562. require.NoError(t, err)
  1563. return expectedMetrics
  1564. }
  1565. expectedMetrics := expectedMetricGen(t)
  1566. metrics, err := scraper.scrape(context.Background())
  1567. require.NoError(t, err)
  1568. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1569. require.NoError(t, err)
  1570. },
  1571. },
  1572. {
  1573. desc: "SNMP integer value for indexed attribute still creates metric (15)",
  1574. testFunc: func(t *testing.T) {
  1575. mockClient := new(MockClient)
  1576. snmpData0 := SNMPData{
  1577. columnOID: ".0",
  1578. oid: ".0.1",
  1579. value: int64(1),
  1580. valueType: integerVal,
  1581. }
  1582. snmpData1 := SNMPData{
  1583. columnOID: ".0",
  1584. oid: ".0.2",
  1585. value: int64(2),
  1586. valueType: integerVal,
  1587. }
  1588. snmpData2 := SNMPData{
  1589. columnOID: ".1",
  1590. oid: ".1.1",
  1591. value: int64(1),
  1592. valueType: integerVal,
  1593. }
  1594. snmpData3 := SNMPData{
  1595. columnOID: ".1",
  1596. oid: ".1.2",
  1597. value: int64(2),
  1598. valueType: integerVal,
  1599. }
  1600. mockClient.On("Connect").Return(nil)
  1601. mockClient.On("Close").Return(nil)
  1602. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1603. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1604. scraper := &snmpScraper{
  1605. cfg: &Config{
  1606. Attributes: map[string]*AttributeConfig{
  1607. "attr1": {
  1608. OID: ".0",
  1609. },
  1610. },
  1611. Metrics: map[string]*MetricConfig{
  1612. "metric1": {
  1613. Description: "test description",
  1614. Unit: "By",
  1615. Gauge: &GaugeMetric{
  1616. ValueType: "int",
  1617. },
  1618. ColumnOIDs: []ColumnOID{
  1619. {
  1620. OID: ".1",
  1621. Attributes: []Attribute{
  1622. {
  1623. Name: "attr1",
  1624. },
  1625. },
  1626. },
  1627. },
  1628. },
  1629. },
  1630. },
  1631. settings: receivertest.NewNopCreateSettings(),
  1632. client: mockClient,
  1633. logger: zap.NewNop(),
  1634. }
  1635. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1636. goldenPath := filepath.Join("testdata", "expected_metrics",
  1637. "15_indexed_column_oid_int_attr_golden.yaml")
  1638. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1639. require.NoError(t, err)
  1640. return expectedMetrics
  1641. }
  1642. expectedMetrics := expectedMetricGen(t)
  1643. metrics, err := scraper.scrape(context.Background())
  1644. require.NoError(t, err)
  1645. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1646. require.NoError(t, err)
  1647. },
  1648. },
  1649. {
  1650. desc: "Resource attribute with prefix creates new resources with created metrics (16)",
  1651. testFunc: func(t *testing.T) {
  1652. mockClient := new(MockClient)
  1653. snmpData0 := SNMPData{
  1654. columnOID: ".1",
  1655. oid: ".1.1",
  1656. value: int64(1),
  1657. valueType: integerVal,
  1658. }
  1659. snmpData1 := SNMPData{
  1660. columnOID: ".1",
  1661. oid: ".1.2",
  1662. value: int64(2),
  1663. valueType: integerVal,
  1664. }
  1665. snmpData2 := SNMPData{
  1666. columnOID: ".2",
  1667. oid: ".2.1",
  1668. value: float64(1.0),
  1669. valueType: floatVal,
  1670. }
  1671. snmpData3 := SNMPData{
  1672. columnOID: ".2",
  1673. oid: ".2.2",
  1674. value: float64(2.0),
  1675. valueType: floatVal,
  1676. }
  1677. mockClient.On("Connect").Return(nil)
  1678. mockClient.On("Close").Return(nil)
  1679. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0, snmpData1, snmpData2, snmpData3})
  1680. scraper := &snmpScraper{
  1681. cfg: &Config{
  1682. ResourceAttributes: map[string]*ResourceAttributeConfig{
  1683. "rattr1": {
  1684. IndexedValuePrefix: "thing",
  1685. },
  1686. },
  1687. Metrics: map[string]*MetricConfig{
  1688. "metric1": {
  1689. Description: "test description",
  1690. Unit: "By",
  1691. Gauge: &GaugeMetric{
  1692. ValueType: "int",
  1693. },
  1694. ColumnOIDs: []ColumnOID{
  1695. {
  1696. OID: ".1",
  1697. ResourceAttributes: []string{"rattr1"},
  1698. },
  1699. },
  1700. },
  1701. "metric2": {
  1702. Description: "test description2",
  1703. Unit: "{units}",
  1704. Gauge: &GaugeMetric{
  1705. ValueType: "double",
  1706. },
  1707. ColumnOIDs: []ColumnOID{
  1708. {
  1709. OID: ".2",
  1710. ResourceAttributes: []string{"rattr1"},
  1711. },
  1712. },
  1713. },
  1714. },
  1715. },
  1716. settings: receivertest.NewNopCreateSettings(),
  1717. client: mockClient,
  1718. logger: zap.NewNop(),
  1719. }
  1720. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1721. goldenPath := filepath.Join("testdata", "expected_metrics",
  1722. "16_indexed_prefix_res_attr_golden.yaml")
  1723. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1724. require.NoError(t, err)
  1725. return expectedMetrics
  1726. }
  1727. expectedMetrics := expectedMetricGen(t)
  1728. metrics, err := scraper.scrape(context.Background())
  1729. require.NoError(t, err)
  1730. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1731. require.NoError(t, err)
  1732. },
  1733. },
  1734. {
  1735. desc: "Indexed resource attribute scrape error will not create metrics or resources",
  1736. testFunc: func(t *testing.T) {
  1737. mockClient := new(MockClient)
  1738. clientErr := errors.New("problem getting data")
  1739. columnOID := ".1"
  1740. oid1 := ".1.1"
  1741. oid2 := ".1.2"
  1742. metricName := "metric1"
  1743. snmpData0 := SNMPData{
  1744. columnOID: columnOID,
  1745. oid: oid1,
  1746. value: int64(1),
  1747. valueType: integerVal,
  1748. }
  1749. snmpData1 := SNMPData{
  1750. columnOID: columnOID,
  1751. oid: oid2,
  1752. value: int64(2),
  1753. valueType: integerVal,
  1754. }
  1755. mockClient.On("Connect").Return(nil)
  1756. mockClient.On("Close").Return(nil)
  1757. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Run(
  1758. func(args mock.Arguments) {
  1759. scraperErrors := args.Get(1).(*scrapererror.ScrapeErrors)
  1760. scraperErrors.AddPartial(1, clientErr)
  1761. },
  1762. ).Return([]SNMPData{}).Once()
  1763. innerInnerErr := errors.New(errMsgResourceAttributeEmptyValue)
  1764. innerErr := fmt.Errorf(errMsgOIDResourceAttributeEmptyValue, metricName, innerInnerErr)
  1765. expectedErr1 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid1, columnOID, innerErr)
  1766. expectedErr2 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid2, columnOID, innerErr)
  1767. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1768. scraper := &snmpScraper{
  1769. cfg: &Config{
  1770. ResourceAttributes: map[string]*ResourceAttributeConfig{
  1771. "rattr1": {
  1772. OID: ".0",
  1773. },
  1774. },
  1775. Metrics: map[string]*MetricConfig{
  1776. "metric1": {
  1777. Description: "test description",
  1778. Unit: "By",
  1779. Gauge: &GaugeMetric{
  1780. ValueType: "int",
  1781. },
  1782. ColumnOIDs: []ColumnOID{
  1783. {
  1784. OID: ".1",
  1785. ResourceAttributes: []string{"rattr1"},
  1786. },
  1787. },
  1788. },
  1789. },
  1790. },
  1791. settings: receivertest.NewNopCreateSettings(),
  1792. client: mockClient,
  1793. logger: zap.NewNop(),
  1794. }
  1795. metrics, err := scraper.scrape(context.Background())
  1796. require.EqualError(t, err, fmt.Sprintf("%s; %s; %s", clientErr, expectedErr1, expectedErr2))
  1797. require.Equal(t, metrics.MetricCount(), 0)
  1798. },
  1799. },
  1800. {
  1801. desc: "Indexed resource attribute scrape returning bad value type will not create metrics or resources",
  1802. testFunc: func(t *testing.T) {
  1803. mockClient := new(MockClient)
  1804. columnOID1 := ".0"
  1805. columnOID2 := ".1"
  1806. oid1 := ".0.1"
  1807. oid2 := ".0.2"
  1808. oid3 := ".1.1"
  1809. oid4 := ".1.2"
  1810. metricName := "metric1"
  1811. snmpData0 := SNMPData{
  1812. columnOID: ".0",
  1813. oid: ".0.1",
  1814. value: true,
  1815. valueType: notSupportedVal,
  1816. }
  1817. snmpData1 := SNMPData{
  1818. columnOID: ".0",
  1819. oid: ".0.2",
  1820. value: false,
  1821. valueType: notSupportedVal,
  1822. }
  1823. snmpData2 := SNMPData{
  1824. columnOID: ".1",
  1825. oid: ".1.1",
  1826. value: int64(1),
  1827. valueType: integerVal,
  1828. }
  1829. snmpData3 := SNMPData{
  1830. columnOID: ".1",
  1831. oid: ".1.2",
  1832. value: int64(2),
  1833. valueType: integerVal,
  1834. }
  1835. mockClient.On("Connect").Return(nil)
  1836. mockClient.On("Close").Return(nil)
  1837. innerErr1 := fmt.Errorf(errMsgIndexedAttributesBadValueType, oid1, columnOID1)
  1838. expectedErr1 := fmt.Errorf(errMsgIndexedAttributeOIDProcessing, oid1, columnOID1, innerErr1)
  1839. innerErr2 := fmt.Errorf(errMsgIndexedAttributesBadValueType, oid2, columnOID1)
  1840. expectedErr2 := fmt.Errorf(errMsgIndexedAttributeOIDProcessing, oid2, columnOID1, innerErr2)
  1841. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1842. innerInnerErr := errors.New(errMsgResourceAttributeEmptyValue)
  1843. innerErr := fmt.Errorf(errMsgOIDResourceAttributeEmptyValue, metricName, innerInnerErr)
  1844. expectedErr3 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid3, columnOID2, innerErr)
  1845. expectedErr4 := fmt.Errorf(errMsgIndexedMetricOIDProcessing, oid4, columnOID2, innerErr)
  1846. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  1847. scraper := &snmpScraper{
  1848. cfg: &Config{
  1849. ResourceAttributes: map[string]*ResourceAttributeConfig{
  1850. "rattr1": {
  1851. OID: ".0",
  1852. },
  1853. },
  1854. Metrics: map[string]*MetricConfig{
  1855. "metric1": {
  1856. Description: "test description",
  1857. Unit: "By",
  1858. Gauge: &GaugeMetric{
  1859. ValueType: "int",
  1860. },
  1861. ColumnOIDs: []ColumnOID{
  1862. {
  1863. OID: ".1",
  1864. ResourceAttributes: []string{"rattr1"},
  1865. },
  1866. },
  1867. },
  1868. },
  1869. },
  1870. settings: receivertest.NewNopCreateSettings(),
  1871. client: mockClient,
  1872. logger: zap.NewNop(),
  1873. }
  1874. metrics, err := scraper.scrape(context.Background())
  1875. require.EqualError(t, err, fmt.Sprintf("%s; %s; %s; %s", expectedErr1, expectedErr2, expectedErr3, expectedErr4))
  1876. require.Equal(t, metrics.MetricCount(), 0)
  1877. },
  1878. },
  1879. {
  1880. desc: "Resource attribute with column OID creates new resource with multiple metrics (17)",
  1881. testFunc: func(t *testing.T) {
  1882. mockClient := new(MockClient)
  1883. snmpData0 := SNMPData{
  1884. columnOID: ".0",
  1885. oid: ".0.1",
  1886. value: "thing1",
  1887. valueType: stringVal,
  1888. }
  1889. snmpData1 := SNMPData{
  1890. columnOID: ".0",
  1891. oid: ".0.2",
  1892. value: "thing2",
  1893. valueType: stringVal,
  1894. }
  1895. snmpData2 := SNMPData{
  1896. columnOID: ".1",
  1897. oid: ".1.1",
  1898. value: int64(1),
  1899. valueType: integerVal,
  1900. }
  1901. snmpData3 := SNMPData{
  1902. columnOID: ".1",
  1903. oid: ".1.2",
  1904. value: int64(2),
  1905. valueType: integerVal,
  1906. }
  1907. snmpData4 := SNMPData{
  1908. columnOID: ".2",
  1909. oid: ".2.1",
  1910. value: float64(1.0),
  1911. valueType: floatVal,
  1912. }
  1913. snmpData5 := SNMPData{
  1914. columnOID: ".2",
  1915. oid: ".2.2",
  1916. value: float64(2.0),
  1917. valueType: floatVal,
  1918. }
  1919. mockClient.On("Connect").Return(nil)
  1920. mockClient.On("Close").Return(nil)
  1921. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  1922. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData2, snmpData3, snmpData4, snmpData5}).Once()
  1923. scraper := &snmpScraper{
  1924. cfg: &Config{
  1925. ResourceAttributes: map[string]*ResourceAttributeConfig{
  1926. "rattr1": {
  1927. OID: ".0",
  1928. },
  1929. },
  1930. Metrics: map[string]*MetricConfig{
  1931. "metric1": {
  1932. Description: "test description",
  1933. Unit: "By",
  1934. Gauge: &GaugeMetric{
  1935. ValueType: "int",
  1936. },
  1937. ColumnOIDs: []ColumnOID{
  1938. {
  1939. OID: ".1",
  1940. ResourceAttributes: []string{"rattr1"},
  1941. },
  1942. },
  1943. },
  1944. "metric2": {
  1945. Description: "test description2",
  1946. Unit: "{units}",
  1947. Gauge: &GaugeMetric{
  1948. ValueType: "double",
  1949. },
  1950. ColumnOIDs: []ColumnOID{
  1951. {
  1952. OID: ".2",
  1953. ResourceAttributes: []string{"rattr1"},
  1954. },
  1955. },
  1956. },
  1957. },
  1958. },
  1959. settings: receivertest.NewNopCreateSettings(),
  1960. client: mockClient,
  1961. logger: zap.NewNop(),
  1962. }
  1963. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  1964. goldenPath := filepath.Join("testdata", "expected_metrics", "17_indexed_oid_res_attr_golden.yaml")
  1965. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  1966. require.NoError(t, err)
  1967. return expectedMetrics
  1968. }
  1969. expectedMetrics := expectedMetricGen(t)
  1970. metrics, err := scraper.scrape(context.Background())
  1971. require.NoError(t, err)
  1972. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  1973. require.NoError(t, err)
  1974. },
  1975. },
  1976. {
  1977. desc: "Resource attribute with column OID without . prefix still creates metrics (17)",
  1978. testFunc: func(t *testing.T) {
  1979. mockClient := new(MockClient)
  1980. snmpData0 := SNMPData{
  1981. columnOID: ".0",
  1982. oid: ".0.1",
  1983. value: "thing1",
  1984. valueType: stringVal,
  1985. }
  1986. snmpData1 := SNMPData{
  1987. columnOID: ".0",
  1988. oid: ".0.2",
  1989. value: "thing2",
  1990. valueType: stringVal,
  1991. }
  1992. snmpData2 := SNMPData{
  1993. columnOID: ".1",
  1994. oid: ".1.1",
  1995. value: int64(1),
  1996. valueType: integerVal,
  1997. }
  1998. snmpData3 := SNMPData{
  1999. columnOID: ".1",
  2000. oid: ".1.2",
  2001. value: int64(2),
  2002. valueType: integerVal,
  2003. }
  2004. snmpData4 := SNMPData{
  2005. columnOID: ".2",
  2006. oid: ".2.1",
  2007. value: float64(1.0),
  2008. valueType: floatVal,
  2009. }
  2010. snmpData5 := SNMPData{
  2011. columnOID: ".2",
  2012. oid: ".2.2",
  2013. value: float64(2.0),
  2014. valueType: floatVal,
  2015. }
  2016. mockClient.On("Connect").Return(nil)
  2017. mockClient.On("Close").Return(nil)
  2018. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  2019. mockClient.On("GetIndexedData", mock.Anything, mock.Anything).Return([]SNMPData{snmpData2, snmpData3, snmpData4, snmpData5}).Once()
  2020. scraper := &snmpScraper{
  2021. cfg: &Config{
  2022. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2023. "rattr1": {
  2024. OID: "0",
  2025. },
  2026. },
  2027. Metrics: map[string]*MetricConfig{
  2028. "metric1": {
  2029. Description: "test description",
  2030. Unit: "By",
  2031. Gauge: &GaugeMetric{
  2032. ValueType: "int",
  2033. },
  2034. ColumnOIDs: []ColumnOID{
  2035. {
  2036. OID: "1",
  2037. ResourceAttributes: []string{"rattr1"},
  2038. },
  2039. },
  2040. },
  2041. "metric2": {
  2042. Description: "test description2",
  2043. Unit: "{units}",
  2044. Gauge: &GaugeMetric{
  2045. ValueType: "double",
  2046. },
  2047. ColumnOIDs: []ColumnOID{
  2048. {
  2049. OID: "2",
  2050. ResourceAttributes: []string{"rattr1"},
  2051. },
  2052. },
  2053. },
  2054. },
  2055. },
  2056. settings: receivertest.NewNopCreateSettings(),
  2057. client: mockClient,
  2058. logger: zap.NewNop(),
  2059. }
  2060. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2061. goldenPath := filepath.Join("testdata", "expected_metrics", "17_indexed_oid_res_attr_golden.yaml")
  2062. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2063. require.NoError(t, err)
  2064. return expectedMetrics
  2065. }
  2066. expectedMetrics := expectedMetricGen(t)
  2067. metrics, err := scraper.scrape(context.Background())
  2068. require.NoError(t, err)
  2069. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2070. require.NoError(t, err)
  2071. },
  2072. },
  2073. {
  2074. desc: "Indexed config with both column OID and prefix resource attributes creates metric (18)",
  2075. testFunc: func(t *testing.T) {
  2076. mockClient := new(MockClient)
  2077. snmpData0 := SNMPData{
  2078. columnOID: ".0",
  2079. oid: ".0.1",
  2080. value: "thing1",
  2081. valueType: stringVal,
  2082. }
  2083. snmpData1 := SNMPData{
  2084. columnOID: ".0",
  2085. oid: ".0.2",
  2086. value: "thing2",
  2087. valueType: stringVal,
  2088. }
  2089. snmpData2 := SNMPData{
  2090. columnOID: ".1",
  2091. oid: ".1.1",
  2092. value: int64(1),
  2093. valueType: integerVal,
  2094. }
  2095. snmpData3 := SNMPData{
  2096. columnOID: ".1",
  2097. oid: ".1.2",
  2098. value: int64(2),
  2099. valueType: integerVal,
  2100. }
  2101. mockClient.On("Connect").Return(nil)
  2102. mockClient.On("Close").Return(nil)
  2103. mockClient.On("GetIndexedData", []string{".0"}, mock.Anything).Return([]SNMPData{snmpData0, snmpData1}).Once()
  2104. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{snmpData2, snmpData3}).Once()
  2105. scraper := &snmpScraper{
  2106. cfg: &Config{
  2107. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2108. "rattr1": {
  2109. OID: ".0",
  2110. },
  2111. "rattr2": {
  2112. IndexedValuePrefix: "object",
  2113. },
  2114. },
  2115. Metrics: map[string]*MetricConfig{
  2116. "metric1": {
  2117. Description: "test description",
  2118. Unit: "By",
  2119. Gauge: &GaugeMetric{
  2120. ValueType: "int",
  2121. },
  2122. ColumnOIDs: []ColumnOID{
  2123. {
  2124. OID: ".1",
  2125. ResourceAttributes: []string{"rattr1", "rattr2"},
  2126. },
  2127. },
  2128. },
  2129. },
  2130. },
  2131. settings: receivertest.NewNopCreateSettings(),
  2132. client: mockClient,
  2133. logger: zap.NewNop(),
  2134. }
  2135. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2136. goldenPath := filepath.Join("testdata", "expected_metrics",
  2137. "18_indexed_oid_and_prefix_res_attr_golden.yaml")
  2138. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2139. require.NoError(t, err)
  2140. return expectedMetrics
  2141. }
  2142. expectedMetrics := expectedMetricGen(t)
  2143. metrics, err := scraper.scrape(context.Background())
  2144. require.NoError(t, err)
  2145. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2146. require.NoError(t, err)
  2147. },
  2148. },
  2149. {
  2150. desc: "ScalarOID (string) resource attribute attached to ColumnOID metric alongside a ColumnOID (string) resource attribute creates metric (19)",
  2151. testFunc: func(t *testing.T) {
  2152. mockClient := new(MockClient)
  2153. scalarRA := SNMPData{
  2154. oid: ".0",
  2155. value: "scalar",
  2156. valueType: stringVal,
  2157. }
  2158. coidRA11 := SNMPData{
  2159. columnOID: ".1",
  2160. oid: ".1.1",
  2161. value: "string1",
  2162. valueType: stringVal,
  2163. }
  2164. coidRA12 := SNMPData{
  2165. columnOID: ".1",
  2166. oid: ".1.2",
  2167. value: "string2",
  2168. valueType: stringVal,
  2169. }
  2170. coid21 := SNMPData{
  2171. columnOID: ".2",
  2172. oid: ".2.1",
  2173. value: int64(3),
  2174. valueType: integerVal,
  2175. }
  2176. coid22 := SNMPData{
  2177. columnOID: ".2",
  2178. oid: ".2.2",
  2179. value: int64(4),
  2180. valueType: integerVal,
  2181. }
  2182. mockClient.On("Connect").Return(nil)
  2183. mockClient.On("Close").Return(nil)
  2184. mockClient.On("GetScalarData", []string{".0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2185. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{coidRA11, coidRA12}).Once()
  2186. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2187. scraper := &snmpScraper{
  2188. cfg: &Config{
  2189. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2190. "rattr1": {
  2191. ScalarOID: ".0",
  2192. },
  2193. "rattr2": {
  2194. OID: ".1",
  2195. },
  2196. },
  2197. Metrics: map[string]*MetricConfig{
  2198. "metric1": {
  2199. Description: "test description",
  2200. Unit: "By",
  2201. Gauge: &GaugeMetric{
  2202. ValueType: "int",
  2203. },
  2204. ColumnOIDs: []ColumnOID{
  2205. {
  2206. OID: ".2",
  2207. ResourceAttributes: []string{"rattr1", "rattr2"},
  2208. },
  2209. },
  2210. },
  2211. },
  2212. },
  2213. settings: receivertest.NewNopCreateSettings(),
  2214. client: mockClient,
  2215. logger: zap.NewNop(),
  2216. }
  2217. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2218. goldenPath := filepath.Join("testdata", "expected_metrics", "19_scalar_ra_string_and_coid_ra_string_on_coid_metric_golden.yaml")
  2219. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2220. require.NoError(t, err)
  2221. return expectedMetrics
  2222. }
  2223. expectedMetrics := expectedMetricGen(t)
  2224. metrics, err := scraper.scrape(context.Background())
  2225. require.NoError(t, err)
  2226. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2227. require.NoError(t, err)
  2228. },
  2229. },
  2230. {
  2231. desc: "ScalarOID (int) resource attribute attached to ColumnOID metric alongside a ColumnOID (int) resource attribute creates metric (20)",
  2232. testFunc: func(t *testing.T) {
  2233. mockClient := new(MockClient)
  2234. scalarRA := SNMPData{
  2235. oid: ".0",
  2236. value: int64(5),
  2237. valueType: integerVal,
  2238. }
  2239. coidRA11 := SNMPData{
  2240. columnOID: ".1",
  2241. oid: ".1.1",
  2242. value: int64(1),
  2243. valueType: integerVal,
  2244. }
  2245. coidRA12 := SNMPData{
  2246. columnOID: ".1",
  2247. oid: ".1.2",
  2248. value: int64(2),
  2249. valueType: integerVal,
  2250. }
  2251. coid21 := SNMPData{
  2252. columnOID: ".2",
  2253. oid: ".2.1",
  2254. value: int64(3),
  2255. valueType: integerVal,
  2256. }
  2257. coid22 := SNMPData{
  2258. columnOID: ".2",
  2259. oid: ".2.2",
  2260. value: int64(4),
  2261. valueType: integerVal,
  2262. }
  2263. mockClient.On("Connect").Return(nil)
  2264. mockClient.On("Close").Return(nil)
  2265. mockClient.On("GetScalarData", []string{".0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2266. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{coidRA11, coidRA12}).Once()
  2267. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2268. scraper := &snmpScraper{
  2269. cfg: &Config{
  2270. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2271. "rattr1": {
  2272. ScalarOID: ".0",
  2273. },
  2274. "rattr2": {
  2275. OID: ".1",
  2276. },
  2277. },
  2278. Metrics: map[string]*MetricConfig{
  2279. "metric1": {
  2280. Description: "test description",
  2281. Unit: "By",
  2282. Gauge: &GaugeMetric{
  2283. ValueType: "int",
  2284. },
  2285. ColumnOIDs: []ColumnOID{
  2286. {
  2287. OID: ".2",
  2288. ResourceAttributes: []string{"rattr1", "rattr2"},
  2289. },
  2290. },
  2291. },
  2292. },
  2293. },
  2294. settings: receivertest.NewNopCreateSettings(),
  2295. client: mockClient,
  2296. logger: zap.NewNop(),
  2297. }
  2298. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2299. goldenPath := filepath.Join("testdata", "expected_metrics", "20_scalar_ra_int_and_coid_ra_int_on_coid_metric_golden.yaml")
  2300. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2301. require.NoError(t, err)
  2302. return expectedMetrics
  2303. }
  2304. expectedMetrics := expectedMetricGen(t)
  2305. metrics, err := scraper.scrape(context.Background())
  2306. require.NoError(t, err)
  2307. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2308. require.NoError(t, err)
  2309. },
  2310. },
  2311. {
  2312. desc: "ScalarOID (float) resource attribute attached to ColumnOID metric alongside a ColumnOID (float) resource attribute creates metric (21)",
  2313. testFunc: func(t *testing.T) {
  2314. mockClient := new(MockClient)
  2315. scalarRA := SNMPData{
  2316. oid: ".0",
  2317. value: float64(5.0),
  2318. valueType: floatVal,
  2319. }
  2320. coidRA11 := SNMPData{
  2321. columnOID: ".1",
  2322. oid: ".1.1",
  2323. value: float64(1.0),
  2324. valueType: floatVal,
  2325. }
  2326. coidRA12 := SNMPData{
  2327. columnOID: ".1",
  2328. oid: ".1.2",
  2329. value: float64(2.0),
  2330. valueType: floatVal,
  2331. }
  2332. coid21 := SNMPData{
  2333. columnOID: ".2",
  2334. oid: ".2.1",
  2335. value: int64(3),
  2336. valueType: integerVal,
  2337. }
  2338. coid22 := SNMPData{
  2339. columnOID: ".2",
  2340. oid: ".2.2",
  2341. value: int64(4),
  2342. valueType: integerVal,
  2343. }
  2344. mockClient.On("Connect").Return(nil)
  2345. mockClient.On("Close").Return(nil)
  2346. mockClient.On("GetScalarData", []string{".0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2347. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{coidRA11, coidRA12}).Once()
  2348. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2349. scraper := &snmpScraper{
  2350. cfg: &Config{
  2351. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2352. "rattr1": {
  2353. ScalarOID: ".0",
  2354. },
  2355. "rattr2": {
  2356. OID: ".1",
  2357. },
  2358. },
  2359. Metrics: map[string]*MetricConfig{
  2360. "metric1": {
  2361. Description: "test description",
  2362. Unit: "By",
  2363. Gauge: &GaugeMetric{
  2364. ValueType: "int",
  2365. },
  2366. ColumnOIDs: []ColumnOID{
  2367. {
  2368. OID: ".2",
  2369. ResourceAttributes: []string{"rattr1", "rattr2"},
  2370. },
  2371. },
  2372. },
  2373. },
  2374. },
  2375. settings: receivertest.NewNopCreateSettings(),
  2376. client: mockClient,
  2377. logger: zap.NewNop(),
  2378. }
  2379. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2380. goldenPath := filepath.Join("testdata", "expected_metrics", "21_scalar_ra_float_and_coid_ra_float_on_coid_metric_golden.yaml")
  2381. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2382. require.NoError(t, err)
  2383. return expectedMetrics
  2384. }
  2385. expectedMetrics := expectedMetricGen(t)
  2386. metrics, err := scraper.scrape(context.Background())
  2387. require.NoError(t, err)
  2388. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2389. require.NoError(t, err)
  2390. },
  2391. },
  2392. {
  2393. desc: "ScalarOID (string) resource attribute attached to ColumnOID metric alongside an IndexedValuePrefix resource attribute creates metric (22)",
  2394. testFunc: func(t *testing.T) {
  2395. mockClient := new(MockClient)
  2396. scalarRA := SNMPData{
  2397. oid: ".0",
  2398. value: "scalar",
  2399. valueType: stringVal,
  2400. }
  2401. coid21 := SNMPData{
  2402. columnOID: ".2",
  2403. oid: ".2.1",
  2404. value: int64(3),
  2405. valueType: integerVal,
  2406. }
  2407. coid22 := SNMPData{
  2408. columnOID: ".2",
  2409. oid: ".2.2",
  2410. value: int64(4),
  2411. valueType: integerVal,
  2412. }
  2413. mockClient.On("Connect").Return(nil)
  2414. mockClient.On("Close").Return(nil)
  2415. mockClient.On("GetScalarData", []string{".0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2416. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2417. scraper := &snmpScraper{
  2418. cfg: &Config{
  2419. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2420. "rattr1": {
  2421. ScalarOID: ".0",
  2422. },
  2423. "rattr2": {
  2424. IndexedValuePrefix: "p",
  2425. },
  2426. },
  2427. Metrics: map[string]*MetricConfig{
  2428. "metric1": {
  2429. Description: "test description",
  2430. Unit: "By",
  2431. Gauge: &GaugeMetric{
  2432. ValueType: "int",
  2433. },
  2434. ColumnOIDs: []ColumnOID{
  2435. {
  2436. OID: ".2",
  2437. ResourceAttributes: []string{"rattr1", "rattr2"},
  2438. },
  2439. },
  2440. },
  2441. },
  2442. },
  2443. settings: receivertest.NewNopCreateSettings(),
  2444. client: mockClient,
  2445. logger: zap.NewNop(),
  2446. }
  2447. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2448. goldenPath := filepath.Join("testdata", "expected_metrics", "22_scalar_ra_string_and_prefix_ra_on_coid_metric_golden.yaml")
  2449. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2450. require.NoError(t, err)
  2451. return expectedMetrics
  2452. }
  2453. expectedMetrics := expectedMetricGen(t)
  2454. metrics, err := scraper.scrape(context.Background())
  2455. require.NoError(t, err)
  2456. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2457. require.NoError(t, err)
  2458. },
  2459. },
  2460. {
  2461. desc: "Multiple ScalarOID (string) resource attributes attached to ColumnOID metric alongside multiple Column OID (string) resource attributes creates metrics (23)",
  2462. testFunc: func(t *testing.T) {
  2463. mockClient := new(MockClient)
  2464. scalarRA1 := SNMPData{
  2465. oid: ".5.0",
  2466. value: "scalar",
  2467. valueType: stringVal,
  2468. }
  2469. scalarRA2 := SNMPData{
  2470. oid: ".6.0",
  2471. value: "also scalar",
  2472. valueType: stringVal,
  2473. }
  2474. coidRA11 := SNMPData{
  2475. columnOID: ".1",
  2476. oid: ".1.1",
  2477. value: "string1",
  2478. valueType: stringVal,
  2479. }
  2480. coidRA12 := SNMPData{
  2481. columnOID: ".1",
  2482. oid: ".1.2",
  2483. value: "string2",
  2484. valueType: stringVal,
  2485. }
  2486. coidRA21 := SNMPData{
  2487. columnOID: ".2",
  2488. oid: ".2.1",
  2489. value: "also a string1",
  2490. valueType: stringVal,
  2491. }
  2492. coidRA22 := SNMPData{
  2493. columnOID: ".2",
  2494. oid: ".2.2",
  2495. value: "also a string2",
  2496. valueType: stringVal,
  2497. }
  2498. coid31 := SNMPData{
  2499. columnOID: ".3",
  2500. oid: ".3.1",
  2501. value: int64(3),
  2502. valueType: integerVal,
  2503. }
  2504. coid32 := SNMPData{
  2505. columnOID: ".3",
  2506. oid: ".3.2",
  2507. value: int64(4),
  2508. valueType: integerVal,
  2509. }
  2510. mockClient.On("Connect").Return(nil)
  2511. mockClient.On("Close").Return(nil)
  2512. mockClient.On("GetScalarData", []string{".5.0", ".6.0"}, mock.Anything).Return([]SNMPData{scalarRA1, scalarRA2}).Once()
  2513. mockClient.On("GetIndexedData", []string{".1", ".2"}, mock.Anything).Return([]SNMPData{coidRA11, coidRA12, coidRA21, coidRA22}).Once()
  2514. mockClient.On("GetIndexedData", []string{".3"}, mock.Anything).Return([]SNMPData{coid31, coid32}).Once()
  2515. scraper := &snmpScraper{
  2516. cfg: &Config{
  2517. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2518. "rattr1": {
  2519. ScalarOID: ".5.0",
  2520. },
  2521. "rattr2": {
  2522. ScalarOID: ".6.0",
  2523. },
  2524. "rattr3": {
  2525. OID: ".1",
  2526. },
  2527. "rattr4": {
  2528. OID: ".2",
  2529. },
  2530. },
  2531. Metrics: map[string]*MetricConfig{
  2532. "metric1": {
  2533. Description: "test description",
  2534. Unit: "By",
  2535. Gauge: &GaugeMetric{
  2536. ValueType: "int",
  2537. },
  2538. ColumnOIDs: []ColumnOID{
  2539. {
  2540. OID: ".3",
  2541. ResourceAttributes: []string{"rattr1", "rattr2", "rattr3", "rattr4"},
  2542. },
  2543. },
  2544. },
  2545. },
  2546. },
  2547. settings: receivertest.NewNopCreateSettings(),
  2548. client: mockClient,
  2549. logger: zap.NewNop(),
  2550. }
  2551. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2552. goldenPath := filepath.Join("testdata", "expected_metrics", "23_multiple_scalar_oid_string_with_multiple_coid_ra_string_on_coid_metric_golden.yaml")
  2553. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2554. require.NoError(t, err)
  2555. return expectedMetrics
  2556. }
  2557. expectedMetrics := expectedMetricGen(t)
  2558. metrics, err := scraper.scrape(context.Background())
  2559. require.NoError(t, err)
  2560. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2561. require.NoError(t, err)
  2562. },
  2563. },
  2564. {
  2565. desc: "ScalarOID (string) resource attribute attached to ColumnOID metric alongside a ColumnOID (string) attribute creates metric (24)",
  2566. testFunc: func(t *testing.T) {
  2567. mockClient := new(MockClient)
  2568. scalarRA := SNMPData{
  2569. oid: ".0",
  2570. value: "scalar",
  2571. valueType: stringVal,
  2572. }
  2573. coidAttr11 := SNMPData{
  2574. columnOID: ".1",
  2575. oid: ".1.1",
  2576. value: "string1",
  2577. valueType: stringVal,
  2578. }
  2579. coidAttr12 := SNMPData{
  2580. columnOID: ".1",
  2581. oid: ".1.2",
  2582. value: "string2",
  2583. valueType: stringVal,
  2584. }
  2585. coid21 := SNMPData{
  2586. columnOID: ".2",
  2587. oid: ".2.1",
  2588. value: int64(3),
  2589. valueType: integerVal,
  2590. }
  2591. coid22 := SNMPData{
  2592. columnOID: ".2",
  2593. oid: ".2.2",
  2594. value: int64(4),
  2595. valueType: integerVal,
  2596. }
  2597. mockClient.On("Connect").Return(nil)
  2598. mockClient.On("Close").Return(nil)
  2599. mockClient.On("GetScalarData", []string{".0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2600. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{coidAttr11, coidAttr12}).Once()
  2601. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2602. scraper := &snmpScraper{
  2603. cfg: &Config{
  2604. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2605. "rattr1": {
  2606. ScalarOID: ".0",
  2607. },
  2608. },
  2609. Attributes: map[string]*AttributeConfig{
  2610. "attr1": {
  2611. OID: ".1",
  2612. },
  2613. },
  2614. Metrics: map[string]*MetricConfig{
  2615. "metric1": {
  2616. Description: "test description",
  2617. Unit: "By",
  2618. Gauge: &GaugeMetric{
  2619. ValueType: "int",
  2620. },
  2621. ColumnOIDs: []ColumnOID{
  2622. {
  2623. OID: ".2",
  2624. ResourceAttributes: []string{"rattr1"},
  2625. Attributes: []Attribute{
  2626. {
  2627. Name: "attr1",
  2628. },
  2629. },
  2630. },
  2631. },
  2632. },
  2633. },
  2634. },
  2635. settings: receivertest.NewNopCreateSettings(),
  2636. client: mockClient,
  2637. logger: zap.NewNop(),
  2638. }
  2639. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2640. goldenPath := filepath.Join("testdata", "expected_metrics", "24_scalar_ra_string_and_coid_attr_string_on_coid_metric_golden.yaml")
  2641. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2642. require.NoError(t, err)
  2643. return expectedMetrics
  2644. }
  2645. expectedMetrics := expectedMetricGen(t)
  2646. metrics, err := scraper.scrape(context.Background())
  2647. require.NoError(t, err)
  2648. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2649. require.NoError(t, err)
  2650. },
  2651. },
  2652. {
  2653. desc: "ScalarOID (string) resource attribute attached to ColumnOID metric alongside an Indexed Value Prefix attribute creates metric (25)",
  2654. testFunc: func(t *testing.T) {
  2655. mockClient := new(MockClient)
  2656. scalarRA := SNMPData{
  2657. oid: ".0",
  2658. value: "scalar",
  2659. valueType: stringVal,
  2660. }
  2661. coid21 := SNMPData{
  2662. columnOID: ".2",
  2663. oid: ".2.1",
  2664. value: int64(3),
  2665. valueType: integerVal,
  2666. }
  2667. coid22 := SNMPData{
  2668. columnOID: ".2",
  2669. oid: ".2.2",
  2670. value: int64(4),
  2671. valueType: integerVal,
  2672. }
  2673. mockClient.On("Connect").Return(nil)
  2674. mockClient.On("Close").Return(nil)
  2675. mockClient.On("GetScalarData", []string{".0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2676. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2677. scraper := &snmpScraper{
  2678. cfg: &Config{
  2679. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2680. "rattr1": {
  2681. ScalarOID: ".0",
  2682. },
  2683. },
  2684. Attributes: map[string]*AttributeConfig{
  2685. "attr1": {
  2686. IndexedValuePrefix: "prefix",
  2687. },
  2688. },
  2689. Metrics: map[string]*MetricConfig{
  2690. "metric1": {
  2691. Description: "test description",
  2692. Unit: "By",
  2693. Gauge: &GaugeMetric{
  2694. ValueType: "int",
  2695. },
  2696. ColumnOIDs: []ColumnOID{
  2697. {
  2698. OID: ".2",
  2699. ResourceAttributes: []string{"rattr1"},
  2700. Attributes: []Attribute{
  2701. {
  2702. Name: "attr1",
  2703. },
  2704. },
  2705. },
  2706. },
  2707. },
  2708. },
  2709. },
  2710. settings: receivertest.NewNopCreateSettings(),
  2711. client: mockClient,
  2712. logger: zap.NewNop(),
  2713. }
  2714. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2715. goldenPath := filepath.Join("testdata", "expected_metrics", "25_scalar_ra_string_and_ivp_attr_on_coid_metric_golden.yaml")
  2716. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2717. require.NoError(t, err)
  2718. return expectedMetrics
  2719. }
  2720. expectedMetrics := expectedMetricGen(t)
  2721. metrics, err := scraper.scrape(context.Background())
  2722. require.NoError(t, err)
  2723. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2724. require.NoError(t, err)
  2725. },
  2726. },
  2727. {
  2728. desc: "ScalarOID (string) resource attribute attached to ScalarOID metric creates metric (26)",
  2729. testFunc: func(t *testing.T) {
  2730. mockClient := new(MockClient)
  2731. scalarRA := SNMPData{
  2732. oid: ".5.0",
  2733. value: "scalar",
  2734. valueType: stringVal,
  2735. }
  2736. scalarOID := SNMPData{
  2737. oid: ".6.0",
  2738. value: int64(6),
  2739. valueType: integerVal,
  2740. }
  2741. mockClient.On("Connect").Return(nil)
  2742. mockClient.On("Close").Return(nil)
  2743. mockClient.On("GetScalarData", []string{".5.0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2744. mockClient.On("GetScalarData", []string{".6.0"}, mock.Anything).Return([]SNMPData{scalarOID}).Once()
  2745. scraper := &snmpScraper{
  2746. cfg: &Config{
  2747. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2748. "rattr1": {
  2749. ScalarOID: ".5.0",
  2750. },
  2751. },
  2752. Metrics: map[string]*MetricConfig{
  2753. "metric1": {
  2754. Description: "test description",
  2755. Unit: "By",
  2756. Gauge: &GaugeMetric{
  2757. ValueType: "int",
  2758. },
  2759. ScalarOIDs: []ScalarOID{
  2760. {
  2761. OID: ".6.0",
  2762. ResourceAttributes: []string{"rattr1"},
  2763. },
  2764. },
  2765. },
  2766. },
  2767. },
  2768. settings: receivertest.NewNopCreateSettings(),
  2769. client: mockClient,
  2770. logger: zap.NewNop(),
  2771. }
  2772. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2773. goldenPath := filepath.Join("testdata", "expected_metrics", "26_scalar_ra_string_on_scalar_metric_golden.yaml")
  2774. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2775. require.NoError(t, err)
  2776. return expectedMetrics
  2777. }
  2778. expectedMetrics := expectedMetricGen(t)
  2779. metrics, err := scraper.scrape(context.Background())
  2780. require.NoError(t, err)
  2781. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2782. require.NoError(t, err)
  2783. },
  2784. },
  2785. {
  2786. desc: "Multiple ScalarOID (string) resource attributes attached to ScalarOID metric creates single resource for metric (27)",
  2787. testFunc: func(t *testing.T) {
  2788. mockClient := new(MockClient)
  2789. scalarRA1 := SNMPData{
  2790. oid: ".5.0",
  2791. value: "scalar",
  2792. valueType: stringVal,
  2793. }
  2794. scalarRA2 := SNMPData{
  2795. oid: ".7.0",
  2796. value: "also scalar",
  2797. valueType: stringVal,
  2798. }
  2799. scalarOID := SNMPData{
  2800. oid: ".6.0",
  2801. value: int64(6),
  2802. valueType: integerVal,
  2803. }
  2804. mockClient.On("Connect").Return(nil)
  2805. mockClient.On("Close").Return(nil)
  2806. mockClient.On("GetScalarData", []string{".5.0", ".7.0"}, mock.Anything).Return([]SNMPData{scalarRA1, scalarRA2}).Once()
  2807. mockClient.On("GetScalarData", []string{".6.0"}, mock.Anything).Return([]SNMPData{scalarOID}).Once()
  2808. scraper := &snmpScraper{
  2809. cfg: &Config{
  2810. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2811. "rattr1": {
  2812. ScalarOID: ".5.0",
  2813. },
  2814. "rattr2": {
  2815. ScalarOID: ".7.0",
  2816. },
  2817. },
  2818. Metrics: map[string]*MetricConfig{
  2819. "metric1": {
  2820. Description: "test description",
  2821. Unit: "By",
  2822. Gauge: &GaugeMetric{
  2823. ValueType: "int",
  2824. },
  2825. ScalarOIDs: []ScalarOID{
  2826. {
  2827. OID: ".6.0",
  2828. ResourceAttributes: []string{"rattr1", "rattr2"},
  2829. },
  2830. },
  2831. },
  2832. },
  2833. },
  2834. settings: receivertest.NewNopCreateSettings(),
  2835. client: mockClient,
  2836. logger: zap.NewNop(),
  2837. }
  2838. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2839. goldenPath := filepath.Join("testdata", "expected_metrics", "27_multiple_scalar_ra_string_on_scalar_metric_golden.yaml")
  2840. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2841. require.NoError(t, err)
  2842. return expectedMetrics
  2843. }
  2844. expectedMetrics := expectedMetricGen(t)
  2845. metrics, err := scraper.scrape(context.Background())
  2846. require.NoError(t, err)
  2847. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2848. require.NoError(t, err)
  2849. },
  2850. },
  2851. {
  2852. desc: "Two scalar oid metrics with the same resource attribute get added to a single resource (28)",
  2853. testFunc: func(t *testing.T) {
  2854. mockClient := new(MockClient)
  2855. scalarRA := SNMPData{
  2856. oid: ".5.0",
  2857. value: "scalar",
  2858. valueType: stringVal,
  2859. }
  2860. scalarOID1 := SNMPData{
  2861. oid: ".6.0",
  2862. value: int64(6),
  2863. valueType: integerVal,
  2864. }
  2865. scalarOID2 := SNMPData{
  2866. oid: ".7.0",
  2867. value: int64(7),
  2868. valueType: integerVal,
  2869. }
  2870. mockClient.On("Connect").Return(nil)
  2871. mockClient.On("Close").Return(nil)
  2872. mockClient.On("GetScalarData", []string{".5.0"}, mock.Anything).Return([]SNMPData{scalarRA}).Once()
  2873. mockClient.On("GetScalarData", []string{".6.0", ".7.0"}, mock.Anything).Return([]SNMPData{scalarOID1, scalarOID2}).Once()
  2874. scraper := &snmpScraper{
  2875. cfg: &Config{
  2876. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2877. "rattr1": {
  2878. ScalarOID: ".5.0",
  2879. },
  2880. },
  2881. Metrics: map[string]*MetricConfig{
  2882. "metric1": {
  2883. Description: "test description",
  2884. Unit: "By",
  2885. Gauge: &GaugeMetric{
  2886. ValueType: "int",
  2887. },
  2888. ScalarOIDs: []ScalarOID{
  2889. {
  2890. OID: ".6.0",
  2891. ResourceAttributes: []string{"rattr1"},
  2892. },
  2893. },
  2894. },
  2895. "metric2": {
  2896. Description: "test description",
  2897. Unit: "By",
  2898. Gauge: &GaugeMetric{
  2899. ValueType: "int",
  2900. },
  2901. ScalarOIDs: []ScalarOID{
  2902. {
  2903. OID: ".7.0",
  2904. ResourceAttributes: []string{"rattr1"},
  2905. },
  2906. },
  2907. },
  2908. },
  2909. },
  2910. settings: receivertest.NewNopCreateSettings(),
  2911. client: mockClient,
  2912. logger: zap.NewNop(),
  2913. }
  2914. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  2915. goldenPath := filepath.Join("testdata", "expected_metrics", "28_two_scalar_oid_metrics_with_same_resource_attribute_golden.yaml")
  2916. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  2917. require.NoError(t, err)
  2918. return expectedMetrics
  2919. }
  2920. expectedMetrics := expectedMetricGen(t)
  2921. metrics, err := scraper.scrape(context.Background())
  2922. require.NoError(t, err)
  2923. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  2924. require.NoError(t, err)
  2925. },
  2926. },
  2927. {
  2928. desc: "Metric does not use all available scalar RAs and still behaves properly (near copy of (24)) (29)",
  2929. testFunc: func(t *testing.T) {
  2930. mockClient := new(MockClient)
  2931. scalarRA1 := SNMPData{
  2932. oid: ".5.0",
  2933. value: "scalar",
  2934. valueType: stringVal,
  2935. }
  2936. scalarRA2 := SNMPData{
  2937. oid: ".6.0",
  2938. value: "also scalar",
  2939. valueType: stringVal,
  2940. }
  2941. coidAttr11 := SNMPData{
  2942. columnOID: ".1",
  2943. oid: ".1.1",
  2944. value: "string1",
  2945. valueType: stringVal,
  2946. }
  2947. coidAttr12 := SNMPData{
  2948. columnOID: ".1",
  2949. oid: ".1.2",
  2950. value: "string2",
  2951. valueType: stringVal,
  2952. }
  2953. coid21 := SNMPData{
  2954. columnOID: ".2",
  2955. oid: ".2.1",
  2956. value: int64(3),
  2957. valueType: integerVal,
  2958. }
  2959. coid22 := SNMPData{
  2960. columnOID: ".2",
  2961. oid: ".2.2",
  2962. value: int64(4),
  2963. valueType: integerVal,
  2964. }
  2965. mockClient.On("Connect").Return(nil)
  2966. mockClient.On("Close").Return(nil)
  2967. mockClient.On("GetScalarData", []string{".5.0", ".6.0"}, mock.Anything).Return([]SNMPData{scalarRA1, scalarRA2}).Once()
  2968. mockClient.On("GetIndexedData", []string{".1"}, mock.Anything).Return([]SNMPData{coidAttr11, coidAttr12}).Once()
  2969. mockClient.On("GetIndexedData", []string{".2"}, mock.Anything).Return([]SNMPData{coid21, coid22}).Once()
  2970. scraper := &snmpScraper{
  2971. cfg: &Config{
  2972. ResourceAttributes: map[string]*ResourceAttributeConfig{
  2973. "rattr1": {
  2974. ScalarOID: ".5.0",
  2975. },
  2976. "rattr2": {
  2977. ScalarOID: ".6.0",
  2978. },
  2979. },
  2980. Attributes: map[string]*AttributeConfig{
  2981. "attr1": {
  2982. OID: ".1",
  2983. },
  2984. },
  2985. Metrics: map[string]*MetricConfig{
  2986. "metric1": {
  2987. Description: "test description",
  2988. Unit: "By",
  2989. Gauge: &GaugeMetric{
  2990. ValueType: "int",
  2991. },
  2992. ColumnOIDs: []ColumnOID{
  2993. {
  2994. OID: ".2",
  2995. ResourceAttributes: []string{"rattr1"},
  2996. Attributes: []Attribute{
  2997. {
  2998. Name: "attr1",
  2999. },
  3000. },
  3001. },
  3002. },
  3003. },
  3004. },
  3005. },
  3006. settings: receivertest.NewNopCreateSettings(),
  3007. client: mockClient,
  3008. logger: zap.NewNop(),
  3009. }
  3010. expectedMetricGen := func(t *testing.T) pmetric.Metrics {
  3011. goldenPath := filepath.Join("testdata", "expected_metrics", "29_metric_does_not_use_all_available_scalar_RAs_golden.yaml")
  3012. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  3013. require.NoError(t, err)
  3014. return expectedMetrics
  3015. }
  3016. expectedMetrics := expectedMetricGen(t)
  3017. metrics, err := scraper.scrape(context.Background())
  3018. require.NoError(t, err)
  3019. err = pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreTimestamp())
  3020. require.NoError(t, err)
  3021. },
  3022. },
  3023. }
  3024. for _, tc := range testCases {
  3025. t.Run(tc.desc, tc.testFunc)
  3026. }
  3027. }