|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/kelseyhightower/envconfig" |
| 13 | + _ "github.com/lib/pq" |
| 14 | +) |
| 15 | + |
| 16 | +// PGConfig contains configurations to connect to a PG database |
| 17 | +type PGConfig struct { |
| 18 | + Username string |
| 19 | + Password string |
| 20 | + Host string |
| 21 | + DBName string |
| 22 | + Query string |
| 23 | + FrequencyInMS int32 |
| 24 | +} |
| 25 | + |
| 26 | +// ConnStr returns a connection string to connect to postgres |
| 27 | +func (c *PGConfig) ConnStr() string { |
| 28 | + return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", c.Username, c.Password, c.Host, c.DBName) |
| 29 | +} |
| 30 | + |
| 31 | +// GetQuery returns the query to use to ping |
| 32 | +func (c *PGConfig) GetQuery() string { |
| 33 | + if c.Query == "" { |
| 34 | + return "select 1" |
| 35 | + } |
| 36 | + |
| 37 | + return c.Query |
| 38 | +} |
| 39 | + |
| 40 | +// GetFrequency returns the frequence in MS in which the query should be run |
| 41 | +func (c *PGConfig) GetFrequency() time.Duration { |
| 42 | + if c.FrequencyInMS == 0 { |
| 43 | + return time.Second |
| 44 | + } |
| 45 | + |
| 46 | + return time.Duration(c.FrequencyInMS) * time.Millisecond |
| 47 | +} |
| 48 | + |
| 49 | +func main() { |
| 50 | + var conf PGConfig |
| 51 | + err := envconfig.Process("PGPING", &conf) |
| 52 | + if err != nil { |
| 53 | + log.Fatal(err) |
| 54 | + } |
| 55 | + |
| 56 | + json.NewEncoder(os.Stdout).Encode(conf) |
| 57 | + |
| 58 | + db, err := sql.Open("postgres", conf.ConnStr()) |
| 59 | + if err != nil { |
| 60 | + log.Fatal(err) |
| 61 | + } |
| 62 | + defer db.Close() |
| 63 | + |
| 64 | + ticker := time.NewTicker(conf.GetFrequency()) |
| 65 | + encoder := json.NewEncoder(os.Stdout) |
| 66 | + for range ticker.C { |
| 67 | + encoder.Encode(executeQuery(db, conf.GetQuery())) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// SQLResult tracks sql response and time taken |
| 72 | +type SQLResult struct { |
| 73 | + Value string |
| 74 | + TimeTakenInMS float64 |
| 75 | + Failed bool |
| 76 | + FailureMessage string |
| 77 | +} |
| 78 | + |
| 79 | +func executeQuery(db *sql.DB, query string) SQLResult { |
| 80 | + res := SQLResult{} |
| 81 | + |
| 82 | + ctx, cancelFunc := context.WithCancel(context.Background()) |
| 83 | + tenSecondTimer := time.NewTimer(10 * time.Second) |
| 84 | + go func() { |
| 85 | + <-tenSecondTimer.C |
| 86 | + cancelFunc() |
| 87 | + }() |
| 88 | + |
| 89 | + start := time.Now() |
| 90 | + rows, err := db.QueryContext(ctx, query) |
| 91 | + res.TimeTakenInMS = time.Since(start).Seconds() * 1000 |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + res.Failed = true |
| 95 | + res.FailureMessage = err.Error() |
| 96 | + return res |
| 97 | + } |
| 98 | + defer rows.Close() |
| 99 | + |
| 100 | + for rows.Next() { |
| 101 | + var name string |
| 102 | + if err := rows.Scan(&name); err != nil { |
| 103 | + res.Failed = true |
| 104 | + res.FailureMessage = err.Error() |
| 105 | + return res |
| 106 | + } |
| 107 | + |
| 108 | + res.Value = name |
| 109 | + } |
| 110 | + |
| 111 | + return res |
| 112 | +} |
0 commit comments