|
| 1 | +package commonutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "fmt" |
| 6 | + _ "github.com/lib/pq" |
| 7 | + "github.com/newrelic/infra-integrations-sdk/v3/data/metric" |
| 8 | + "github.com/newrelic/infra-integrations-sdk/v3/integration" |
| 9 | + "github.com/newrelic/infra-integrations-sdk/v3/log" |
| 10 | + "github.com/newrelic/nri-postgresql/src/args" |
| 11 | + "math/big" |
| 12 | + "reflect" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +const publishThreshold = 100 |
| 17 | +const randomIntRange = 1000000 |
| 18 | + |
| 19 | +func SetMetric(metricSet *metric.Set, name string, value interface{}, sourceType string) { |
| 20 | + switch sourceType { |
| 21 | + case `gauge`: |
| 22 | + err := metricSet.SetMetric(name, value, metric.GAUGE) |
| 23 | + if err != nil { |
| 24 | + return |
| 25 | + } |
| 26 | + case `attribute`: |
| 27 | + err := metricSet.SetMetric(name, value, metric.ATTRIBUTE) |
| 28 | + if err != nil { |
| 29 | + return |
| 30 | + } |
| 31 | + default: |
| 32 | + err := metricSet.SetMetric(name, value, metric.GAUGE) |
| 33 | + if err != nil { |
| 34 | + return |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func IngestMetric(metricList []interface{}, eventName string, pgIntegration *integration.Integration, args args.ArgumentList) { |
| 40 | + instanceEntity, err := createEntity(pgIntegration, args) |
| 41 | + if err != nil { |
| 42 | + log.Error("Error creating entity: %v", err) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + metricCount := 0 |
| 47 | + lenOfMetricList := len(metricList) |
| 48 | + |
| 49 | + for _, model := range metricList { |
| 50 | + if model == nil { |
| 51 | + continue |
| 52 | + } |
| 53 | + metricCount += 1 |
| 54 | + metricSet := instanceEntity.NewMetricSet(eventName) |
| 55 | + |
| 56 | + processModel(model, metricSet) |
| 57 | + |
| 58 | + if metricCount == publishThreshold || metricCount == lenOfMetricList { |
| 59 | + metricCount = 0 |
| 60 | + if err := publishMetrics(pgIntegration, &instanceEntity, args); err != nil { |
| 61 | + log.Error("Error publishing metrics: %v", err) |
| 62 | + return |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + if metricCount > 0 { |
| 67 | + if err := publishMetrics(pgIntegration, &instanceEntity, args); err != nil { |
| 68 | + log.Error("Error publishing metrics: %v", err) |
| 69 | + return |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func createEntity(pgIntegration *integration.Integration, args args.ArgumentList) (*integration.Entity, error) { |
| 75 | + return pgIntegration.Entity(fmt.Sprintf("%s:%s", args.Hostname, args.Port), "pg-instance") |
| 76 | +} |
| 77 | + |
| 78 | +func processModel(model interface{}, metricSet *metric.Set) { |
| 79 | + modelValue := reflect.ValueOf(model) |
| 80 | + if modelValue.Kind() == reflect.Ptr { |
| 81 | + modelValue = modelValue.Elem() |
| 82 | + } |
| 83 | + if !modelValue.IsValid() || modelValue.Kind() != reflect.Struct { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + modelType := reflect.TypeOf(model) |
| 88 | + |
| 89 | + for i := 0; i < modelValue.NumField(); i++ { |
| 90 | + field := modelValue.Field(i) |
| 91 | + fieldType := modelType.Field(i) |
| 92 | + metricName := fieldType.Tag.Get("metric_name") |
| 93 | + sourceType := fieldType.Tag.Get("source_type") |
| 94 | + ingestData := fieldType.Tag.Get("ingest_data") |
| 95 | + |
| 96 | + if ingestData == "false" { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + if field.Kind() == reflect.Ptr && !field.IsNil() { |
| 101 | + SetMetric(metricSet, metricName, field.Elem().Interface(), sourceType) |
| 102 | + } else if field.Kind() != reflect.Ptr { |
| 103 | + SetMetric(metricSet, metricName, field.Interface(), sourceType) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func publishMetrics(pgIntegration *integration.Integration, instanceEntity **integration.Entity, args args.ArgumentList) error { |
| 109 | + if err := pgIntegration.Publish(); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + var err error |
| 113 | + *instanceEntity, err = pgIntegration.Entity(fmt.Sprintf("%s:%s", args.Hostname, args.Port), "pg-instance") |
| 114 | + return err |
| 115 | +} |
| 116 | + |
| 117 | +func GenerateRandomIntegerString(queryID string) *string { |
| 118 | + randomInt, err := rand.Int(rand.Reader, big.NewInt(randomIntRange)) |
| 119 | + if err != nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + currentTime := time.Now().Format("20060102150405") |
| 123 | + result := fmt.Sprintf("%s-%d-%s", queryID, randomInt.Int64(), currentTime) |
| 124 | + return &result |
| 125 | +} |
0 commit comments