MCPcopy
hub / github.com/netdata/netdata / extractContextMetadata

Function extractContextMetadata

src/go/plugin/ibm.d/framework/state.go:310–389  ·  view source on GitHub ↗

extractContextMetadata extracts metadata from a Context[T] pointer

(ctx any)

Source from the content-addressed store, hash-verified

308
309// extractContextMetadata extracts metadata from a Context[T] pointer
310func extractContextMetadata(ctx any) *ContextMetadata {
311 v := reflect.ValueOf(ctx)
312 if v.Kind() == reflect.Pointer {
313 v = v.Elem()
314 }
315
316 if v.Kind() != reflect.Struct {
317 return nil
318 }
319
320 // Create metadata object
321 meta := &ContextMetadata{}
322
323 // Extract fields
324 if name := v.FieldByName("Name"); name.IsValid() && name.Kind() == reflect.String {
325 meta.Name = name.String()
326 }
327 if family := v.FieldByName("Family"); family.IsValid() && family.Kind() == reflect.String {
328 meta.Family = family.String()
329 }
330 if title := v.FieldByName("Title"); title.IsValid() && title.Kind() == reflect.String {
331 meta.Title = title.String()
332 }
333 if units := v.FieldByName("Units"); units.IsValid() && units.Kind() == reflect.String {
334 meta.Units = units.String()
335 }
336 if typ := v.FieldByName("Type"); typ.IsValid() {
337 // Type is module.ChartType (string)
338 if typ.Kind() == reflect.String {
339 meta.Type = collectorapi.ChartType(typ.String())
340 }
341 }
342 if priority := v.FieldByName("Priority"); priority.IsValid() && priority.Kind() == reflect.Int {
343 meta.Priority = int(priority.Int())
344 }
345 if updateEvery := v.FieldByName("UpdateEvery"); updateEvery.IsValid() && updateEvery.Kind() == reflect.Int {
346 meta.UpdateEvery = int(updateEvery.Int())
347 }
348
349 // Extract LabelKeys slice
350 if labelKeys := v.FieldByName("LabelKeys"); labelKeys.IsValid() && labelKeys.Kind() == reflect.Slice {
351 meta.HasLabels = labelKeys.Len() > 0
352 for i := 0; i < labelKeys.Len(); i++ {
353 if key := labelKeys.Index(i); key.Kind() == reflect.String {
354 meta.LabelOrder = append(meta.LabelOrder, key.String())
355 }
356 }
357 }
358
359 // Extract dimensions slice
360 if dims := v.FieldByName("Dimensions"); dims.IsValid() && dims.Kind() == reflect.Slice {
361 for i := 0; i < dims.Len(); i++ {
362 dim := dims.Index(i)
363 if dim.Kind() == reflect.Struct {
364 d := Dimension{}
365 if name := dim.FieldByName("Name"); name.IsValid() && name.Kind() == reflect.String {
366 d.Name = name.String()
367 }

Callers 4

GetInstanceMethod · 0.85
getContextMetadataFunction · 0.85
convertMetricsMethod · 0.85

Calls 4

IsValidMethod · 0.80
KindMethod · 0.65
LenMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…