Пример #1
0
func (p *Postgresql) accRow(meas_name string, row scanner, acc telegraf.Accumulator) error {
	var columnVars []interface{}
	var dbname bytes.Buffer

	// this is where we'll store the column name with its *interface{}
	columnMap := make(map[string]*interface{})

	for _, column := range p.OrderedColumns {
		columnMap[column] = new(interface{})
	}

	// populate the array of interface{} with the pointers in the right order
	for i := 0; i < len(columnMap); i++ {
		columnVars = append(columnVars, columnMap[p.OrderedColumns[i]])
	}

	// deconstruct array of variables and send to Scan
	err := row.Scan(columnVars...)

	if err != nil {
		return err
	}
	if columnMap["datname"] != nil {
		// extract the database name from the column map
		dbnameChars := (*columnMap["datname"]).([]uint8)
		for i := 0; i < len(dbnameChars); i++ {
			dbname.WriteString(string(dbnameChars[i]))
		}
	} else {
		dbname.WriteString("postgres")
	}

	var tagAddress string
	tagAddress, err = p.SanitizedAddress()
	if err != nil {
		return err
	}

	// Process the additional tags

	tags := map[string]string{}
	tags["server"] = tagAddress
	tags["db"] = dbname.String()
	var isATag int
	fields := make(map[string]interface{})
	for col, val := range columnMap {
		if acc.Debug() {
			log.Printf("postgresql_extensible: column: %s = %T: %s\n", col, *val, *val)
		}
		_, ignore := ignoredColumns[col]
		if !ignore && *val != nil {
			isATag = 0
			for tag := range p.AdditionalTags {
				if col == p.AdditionalTags[tag] {
					isATag = 1
					value_type_p := fmt.Sprintf(`%T`, *val)
					if value_type_p == "[]uint8" {
						tags[col] = fmt.Sprintf(`%s`, *val)
					} else if value_type_p == "int64" {
						tags[col] = fmt.Sprintf(`%v`, *val)
					}
				}
			}
			if isATag == 0 {
				fields[col] = *val
			}
		}
	}
	acc.AddFields(meas_name, fields, tags)
	return nil
}