-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from metonymic-smokey/develop
Use real time temperature data
- Loading branch information
Showing
6 changed files
with
132 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
# prom-exporter | ||
Repo for learning how to implement Prometheus exporter | ||
|
||
### Instructions to run: | ||
* Have Prometheus running on port 9090 and make sure port 2112 is not in use. | ||
* Async exporter: `go run async.go temp.go`. | ||
* Sync exporter: `go run sync.go temp.go`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
type Temperature struct { | ||
Temp float64 `json:"temperature"` | ||
} | ||
|
||
type Final struct { | ||
StartTime string `json:"startTime"` | ||
Values Temperature `json:"values"` | ||
} | ||
|
||
type Interval struct { | ||
Timestep string `json:"timestep"` | ||
StartTime string `json:"startTime"` | ||
EndTime string `json:"endTime"` | ||
TempVal []Final `json:"intervals"` | ||
} | ||
|
||
type Timelines struct { | ||
Timestep []Interval `json:"timelines"` | ||
} | ||
|
||
type Response struct { | ||
Data Timelines `json:"data"` | ||
} | ||
|
||
func getTempData() (Response, error) { | ||
url := fmt.Sprintf("https://api.tomorrow.io/v4/timelines?location=%f,%f&fields=temperature×teps=%s&units=%s", 73.98529171943665, 40.75872069597532, "1h", "metric") | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return Response{}, errors.New("error in GET request") | ||
} | ||
req.Header.Add("apikey", "APIKEY") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return Response{}, err | ||
} | ||
defer res.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return Response{}, errors.New("error reading response") | ||
} | ||
|
||
var dat Response | ||
if err := json.Unmarshal(body, &dat); err != nil { | ||
return Response{}, errors.New("error unmarshalling JSON") | ||
} | ||
|
||
return dat, nil | ||
} |