Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

issue fixing #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

const (
name = "mysql"
version = 8
version = 9
pluginType = plugin.PublisherPluginType
usernameDefault = "root"
passwordDefault = "root"
Expand All @@ -51,7 +51,7 @@ const (

databaseDefault = "SNAP_TEST"
tableDefault = "info"
tableColumns = "(timestamp VARCHAR(200), source_column VARCHAR(200), key_column VARCHAR(200), value_column VARCHAR(200))"
tableColumns = "(id int(11) NOT NULL AUTO_INCREMENT,timestamp VARCHAR(200), source_column VARCHAR(200), key_column VARCHAR(200), value_column VARCHAR(200), save_time datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`))"
)

type mysqlPublisher struct {
Expand Down Expand Up @@ -90,14 +90,17 @@ func (s *mysqlPublisher) Publish(contentType string, content []byte, cfg map[str
value, err := interfaceToString(m.Data())
if err != nil {
logger.Printf("Error: Cannot convert incoming data to string, err=%v", err)
s.db.Close()
return err
}
_, err = s.dbInsertStmt.Exec(m.Timestamp(), m.Tags()[core.STD_TAG_PLUGIN_RUNNING_ON], key, value)
if err != nil {
logger.Printf("Error: Cannot publish incoming metric to mysql db, err=%v", err)
s.db.Close()
return err
}
}
s.db.Close()
return nil
}

Expand Down Expand Up @@ -184,7 +187,7 @@ func (s *mysqlPublisher) init(cfg map[string]ctypes.ConfigValue) error {
}

// Put the values into the database with the current time
s.dbInsertStmt, err = s.db.Prepare("INSERT INTO" + " " + cfg["tablename"].(ctypes.ConfigValueStr).Value + " VALUES( ?, ?, ?, ? )")
s.dbInsertStmt, err = s.db.Prepare("INSERT INTO" + " " + cfg["tablename"].(ctypes.ConfigValueStr).Value + " (timestamp, source_column, key_column, value_column) VALUES( ?, ?, ?, ? )")
if err != nil {
fmt.Printf("Error: cannot prepare insert db statement, err=%v", err)
return err
Expand Down Expand Up @@ -231,6 +234,9 @@ func interfaceToString(face interface{}) (string, error) {
ret += ", "
ret += strconv.Itoa(val[i])
}
case int64:
ret = strconv.FormatInt(val, 10)

case int:
ret = strconv.Itoa(val)
case []uint:
Expand Down Expand Up @@ -285,3 +291,4 @@ func interfaceToString(face interface{}) (string, error) {
}
return ret, err
}