Don't overwrite forecast points (#5930)

This commit is contained in:
Daniel Nelson 2019-05-31 16:22:37 -07:00 committed by GitHub
parent a0213d9c4f
commit 0ca8ea1724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 315 additions and 313 deletions

View File

@ -10,6 +10,7 @@
- [bind](/plugins/inputs/bind/README.md) - Contributed by @dswarbrick & @danielllek - [bind](/plugins/inputs/bind/README.md) - Contributed by @dswarbrick & @danielllek
- [ecs](/plugins/inputs/ecs/README.md) - Contributed by @rbtr - [ecs](/plugins/inputs/ecs/README.md) - Contributed by @rbtr
- [github](/plugins/inputs/github/README.md) - Contributed by @influxdata - [github](/plugins/inputs/github/README.md) - Contributed by @influxdata
- [openweathermap](/plugins/inputs/openweathermap/README.md) - Contributed by @regel
- [powerdns_recursor](/plugins/inputs/powerdns_recursor/README.md) - Contributed by @dupondje - [powerdns_recursor](/plugins/inputs/powerdns_recursor/README.md) - Contributed by @dupondje
#### New Aggregators #### New Aggregators

View File

@ -236,6 +236,7 @@ For documentation on the latest development code see the [documentation index][d
* [nvidia_smi](./plugins/inputs/nvidia_smi) * [nvidia_smi](./plugins/inputs/nvidia_smi)
* [openldap](./plugins/inputs/openldap) * [openldap](./plugins/inputs/openldap)
* [opensmtpd](./plugins/inputs/opensmtpd) * [opensmtpd](./plugins/inputs/opensmtpd)
* [openweathermap](./plugins/inputs/openweathermap)
* [pf](./plugins/inputs/pf) * [pf](./plugins/inputs/pf)
* [pgbouncer](./plugins/inputs/pgbouncer) * [pgbouncer](./plugins/inputs/pgbouncer)
* [phpfpm](./plugins/inputs/phpfpm) * [phpfpm](./plugins/inputs/phpfpm)

View File

@ -1,73 +1,68 @@
# Telegraf Plugin: openweathermap # OpenWeatherMap Input Plugin
OpenWeatherMap provides the current weather and forecasts for more than 200,000 cities. To use this plugin you will need a token. For more information [click here](https://openweathermap.org/appid). Collect current weather and forecast data from OpenWeatherMap.
Find city identifiers in this [list](http://bulk.openweathermap.org/sample/city.list.json.gz). You can also use this [url](https://openweathermap.org/find) as an alternative to downloading a file. The ID is in the url of the city: `https://openweathermap.org/city/2643743` To use this plugin you will need an [api key][] (app_id).
### Configuration: City identifiers can be found in the [city list][]. Alternately you can
[search][] by name; the `city_id` can be found as the last digits of the URL:
https://openweathermap.org/city/2643743
### Configuration
```toml ```toml
[[inputs.openweathermap]] [[inputs.openweathermap]]
## Root url of API to pull stats ## OpenWeatherMap API key.
# base_url = "https://api.openweathermap.org/data/2.5/" app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## Your personal user token from openweathermap.org
# app_id = "xxxxxxxxxxxxxxxxxxxxxxx" ## City ID's to collect weather data from.
## List of city identifiers city_id = ["5391959"]
# city_id = ["2988507", "519188"]
## HTTP response timeout (default: 5s) ## APIs to fetch; can contain "weather" or "forecast".
fetch = ["weather", "forecast"]
## OpenWeatherMap base URL
# base_url = "https://api.openweathermap.org/"
## Timeout for HTTP response.
# response_timeout = "5s" # response_timeout = "5s"
## Query the current weather and future forecast
# fetch = ["weather", "forecast"] ## Preferred unit system for temperature and wind speed. Can be one of
## For temperature in Fahrenheit use units=imperial ## "metric", "imperial", or "standard".
## For temperature in Celsius use units=metric (default)
# units = "metric" # units = "metric"
## Query interval; OpenWeatherMap weather data is updated every 10
## minutes.
interval = "10m"
``` ```
### Metrics: ### Metrics
+ weather - weather
- fields:
- humidity (int, Humidity percentage)
- temperature (float, Unit: Celcius)
- pressure (float, Atmospheric pressure in hPa)
- rain (float, Rain volume for the last 3 hours, mm)
- wind_speed (float, Wind speed. Unit Default: meter/sec)
- wind_degrees (float, Wind direction, degrees)
- tags: - tags:
- city_id - city_id
- forecast - forecast
- fields:
- cloudiness (int, percent)
- humidity (int, percent)
- pressure (float, atmospheric pressure hPa)
- rain (float, rain volume for the last 3 hours in mm)
- sunrise (int, nanoseconds since unix epoch)
- sunset (int, nanoseconds since unix epoch)
- temperature (float, degrees)
- visibility (int, meters, not available on forecast data)
- wind_degrees (float, wind direction in degrees)
- wind_speed (float, wind speed in meters/sec or miles/sec)
### Example Output:
Using this configuration: ### Example Output
```toml
[[inputs.openweathermap]] ```
base_url = "https://api.openweathermap.org/data/2.5/" > weather,city=San\ Francisco,city_id=5391959,country=US,forecast=* cloudiness=40i,humidity=72i,pressure=1013,rain=0,sunrise=1559220629000000000i,sunset=1559273058000000000i,temperature=13.31,visibility=16093i,wind_degrees=280,wind_speed=4.6 1559268695000000000
app_id = "change_this_with_your_appid" > weather,city=San\ Francisco,city_id=5391959,country=US,forecast=3h cloudiness=0i,humidity=86i,pressure=1012.03,rain=0,temperature=10.69,wind_degrees=222.855,wind_speed=2.76 1559271600000000000
city_id = ["2988507", "519188"] > weather,city=San\ Francisco,city_id=5391959,country=US,forecast=6h cloudiness=11i,humidity=93i,pressure=1012.79,rain=0,temperature=9.34,wind_degrees=212.685,wind_speed=1.85 1559282400000000000
response_timeout = "5s"
fetch = ["weather", "forecast"]
units = "metric"
``` ```
When run with: [api key]: https://openweathermap.org/appid
``` [city list]: http://bulk.openweathermap.org/sample/city.list.json.gz
./telegraf -config telegraf.conf -input-filter openweathermap -test [search]: https://openweathermap.org/find
```
It produces data similar to:
```
> weather,city_id=4303602,forecast=* humidity=51i,pressure=1012,rain=0,temperature=16.410000000000025,wind_degrees=170,wind_speed=2.6 1556393944000000000
> weather,city_id=2988507,forecast=* humidity=87i,pressure=1020,rain=0,temperature=7.110000000000014,wind_degrees=260,wind_speed=5.1 1556393841000000000
> weather,city_id=2988507,forecast=3h humidity=69i,pressure=1020.38,rain=0,temperature=5.650000000000034,wind_degrees=268.456,wind_speed=5.83 1556398800000000000
> weather,city_id=2988507,forecast=* humidity=69i,pressure=1020.38,rain=0,temperature=5.650000000000034,wind_degrees=268.456,wind_speed=5.83 1556398800000000000
> weather,city_id=2988507,forecast=6h humidity=74i,pressure=1020.87,rain=0,temperature=5.810000000000002,wind_degrees=261.296,wind_speed=5.43 1556409600000000000
> weather,city_id=2988507,forecast=* humidity=74i,pressure=1020.87,rain=0,temperature=5.810000000000002,wind_degrees=261.296,wind_speed=5.43 1556409600000000000
> weather,city_id=4303602,forecast=9h humidity=66i,pressure=1010.63,rain=0,temperature=14.740000000000009,wind_degrees=196.264,wind_speed=4.3 1556398800000000000
> weather,city_id=4303602,forecast=* humidity=66i,pressure=1010.63,rain=0,temperature=14.740000000000009,wind_degrees=196.264,wind_speed=4.3 1556398800000000000
```

View File

@ -1,9 +1,10 @@
package openweathermap package openweathermap
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"mime"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -16,37 +17,50 @@ import (
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
const (
// https://openweathermap.org/current#severalid
// Call for several city IDs
// The limit of locations is 20.
owmRequestSeveralCityId int = 20
defaultBaseURL = "https://api.openweathermap.org/"
defaultResponseTimeout time.Duration = time.Second * 5
defaultUnits string = "metric"
)
type OpenWeatherMap struct { type OpenWeatherMap struct {
BaseUrl string AppId string `toml:"app_id"`
AppId string CityId []string `toml:"city_id"`
CityId []string Fetch []string `toml:"fetch"`
BaseUrl string `toml:"base_url"`
ResponseTimeout internal.Duration `toml:"response_timeout"`
Units string `toml:"units"`
client *http.Client client *http.Client
ResponseTimeout internal.Duration
Fetch []string
Units string
} }
// https://openweathermap.org/current#severalid
// Call for several city IDs
// The limit of locations is 20.
const owmRequestSeveralCityId int = 20
const defaultResponseTimeout time.Duration = time.Second * 5
const defaultUnits string = "metric"
var sampleConfig = ` var sampleConfig = `
## Root url of weather map REST API ## OpenWeatherMap API key.
base_url = "https://api.openweathermap.org/"
## Your personal user token from openweathermap.org
app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" app_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
city_id = ["2988507", "2988588"]
## HTTP response timeout (default: 5s) ## City ID's to collect weather data from.
response_timeout = "5s" city_id = ["5391959"]
## APIs to fetch; can contain "weather" or "forecast".
fetch = ["weather", "forecast"] fetch = ["weather", "forecast"]
units = "metric"
## Limit OpenWeatherMap query interval. See calls per minute info at: https://openweathermap.org/price ## OpenWeatherMap base URL
# base_url = "https://api.openweathermap.org/"
## Timeout for HTTP response.
# response_timeout = "5s"
## Preferred unit system for temperature and wind speed. Can be one of
## "metric", "imperial", or "standard".
# units = "metric"
## Query interval; OpenWeatherMap updates their weather data every 10
## minutes.
interval = "10m" interval = "10m"
` `
@ -69,7 +83,6 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error {
// Create an HTTP client that is re-used for each // Create an HTTP client that is re-used for each
// collection interval // collection interval
if n.client == nil { if n.client == nil {
client, err := n.createHttpClient() client, err := n.createHttpClient()
if err != nil { if err != nil {
@ -77,33 +90,43 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error {
} }
n.client = client n.client = client
} }
units := n.Units units := n.Units
if units == "" { switch n.Units {
case "imperial", "standard":
break
default:
units = defaultUnits units = defaultUnits
} }
for _, fetch := range n.Fetch { for _, fetch := range n.Fetch {
if fetch == "forecast" { if fetch == "forecast" {
var u *url.URL var u *url.URL
var addr *url.URL
for _, city := range n.CityId { for _, city := range n.CityId {
u, err = url.Parse(fmt.Sprintf("/data/2.5/forecast?id=%s&APPID=%s&units=%s", city, n.AppId, units)) u, err = url.Parse(fmt.Sprintf("/data/2.5/forecast?id=%s&APPID=%s&units=%s", city, n.AppId, units))
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("Unable to parse address '%s': %s", u, err)) acc.AddError(fmt.Errorf("unable to parse address '%s': %s", u, err))
continue continue
} }
addr = base.ResolveReference(u)
addr := base.ResolveReference(u).String()
wg.Add(1) wg.Add(1)
go func(addr *url.URL) { go func() {
defer wg.Done() defer wg.Done()
acc.AddError(n.gatherUrl(addr, acc, true)) status, err := n.gatherUrl(addr)
}(addr) if err != nil {
acc.AddError(err)
return
}
gatherForecast(acc, status)
}()
} }
} else if fetch == "weather" { } else if fetch == "weather" {
j := 0 j := 0
for j < len(n.CityId) { for j < len(n.CityId) {
var u *url.URL var u *url.URL
var addr *url.URL
strs = make([]string, 0) strs = make([]string, 0)
for i := 0; j < len(n.CityId) && i < owmRequestSeveralCityId; i++ { for i := 0; j < len(n.CityId) && i < owmRequestSeveralCityId; i++ {
strs = append(strs, n.CityId[j]) strs = append(strs, n.CityId[j])
@ -117,12 +140,18 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error {
continue continue
} }
addr = base.ResolveReference(u) addr := base.ResolveReference(u).String()
wg.Add(1) wg.Add(1)
go func(addr *url.URL) { go func() {
defer wg.Done() defer wg.Done()
acc.AddError(n.gatherUrl(addr, acc, false)) status, err := n.gatherUrl(addr)
}(addr) if err != nil {
acc.AddError(err)
return
}
gatherWeather(acc, status)
}()
} }
} }
@ -133,7 +162,6 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error {
} }
func (n *OpenWeatherMap) createHttpClient() (*http.Client, error) { func (n *OpenWeatherMap) createHttpClient() (*http.Client, error) {
if n.ResponseTimeout.Duration < time.Second { if n.ResponseTimeout.Duration < time.Second {
n.ResponseTimeout.Duration = defaultResponseTimeout n.ResponseTimeout.Duration = defaultResponseTimeout
} }
@ -146,65 +174,51 @@ func (n *OpenWeatherMap) createHttpClient() (*http.Client, error) {
return client, nil return client, nil
} }
func (n *OpenWeatherMap) gatherUrl(addr *url.URL, acc telegraf.Accumulator, forecast bool) error { func (n *OpenWeatherMap) gatherUrl(addr string) (*Status, error) {
resp, err := n.client.Get(addr.String()) resp, err := n.client.Get(addr)
if err != nil { if err != nil {
return fmt.Errorf("error making HTTP request to %s: %s", addr.String(), err) return nil, fmt.Errorf("error making HTTP request to %s: %s", addr, err)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status) return nil, fmt.Errorf("%s returned HTTP status %s", addr, resp.Status)
} }
contentType := strings.Split(resp.Header.Get("Content-Type"), ";")[0]
switch contentType { mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
case "application/json": if err != nil {
err = gatherWeatherUrl(bufio.NewReader(resp.Body), forecast, acc) return nil, err
return err
default:
return fmt.Errorf("%s returned unexpected content type %s", addr.String(), contentType)
} }
if mediaType != "application/json" {
return nil, fmt.Errorf("%s returned unexpected content type %s", addr, mediaType)
}
return gatherWeatherUrl(resp.Body)
} }
type WeatherEntry struct { type WeatherEntry struct {
Dt int64 `json:"dt"` Dt int64 `json:"dt"`
Dttxt string `json:"dt_txt"` // empty for weather/
Clouds struct { Clouds struct {
All int64 `json:"all"` All int64 `json:"all"`
} `json:"clouds"` } `json:"clouds"`
Main struct { Main struct {
GrndLevel float64 `json:"grnd_level"` // empty for weather/ Humidity int64 `json:"humidity"`
Humidity int64 `json:"humidity"` Pressure float64 `json:"pressure"`
SeaLevel float64 `json:"sea_level"` // empty for weather/ Temp float64 `json:"temp"`
Pressure float64 `json:"pressure"`
Temp float64 `json:"temp"`
TempMax float64 `json:"temp_max"`
TempMin float64 `json:"temp_min"`
} `json:"main"` } `json:"main"`
Rain struct { Rain struct {
Rain3 float64 `json:"3h"` Rain3 float64 `json:"3h"`
} `json:"rain"` } `json:"rain"`
Sys struct { Sys struct {
Pod string `json:"pod"` Country string `json:"country"`
Country string `json:"country"` Sunrise int64 `json:"sunrise"`
Message float64 `json:"message"` Sunset int64 `json:"sunset"`
Id int64 `json:"id"`
Type int64 `json:"type"`
Sunrise int64 `json:"sunrise"`
Sunset int64 `json:"sunset"`
} `json:"sys"` } `json:"sys"`
Wind struct { Wind struct {
Deg float64 `json:"deg"` Deg float64 `json:"deg"`
Speed float64 `json:"speed"` Speed float64 `json:"speed"`
} `json:"wind"` } `json:"wind"`
Weather []struct {
Description string `json:"description"`
Icon string `json:"icon"`
Id int64 `json:"id"`
Main string `json:"main"`
} `json:"weather"`
// Additional entries for weather/
Id int64 `json:"id"` Id int64 `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Coord struct { Coord struct {
@ -227,69 +241,66 @@ type Status struct {
List []WeatherEntry `json:"list"` List []WeatherEntry `json:"list"`
} }
func gatherWeatherUrl(r *bufio.Reader, forecast bool, acc telegraf.Accumulator) error { func gatherWeatherUrl(r io.Reader) (*Status, error) {
dec := json.NewDecoder(r) dec := json.NewDecoder(r)
status := &Status{} status := &Status{}
if err := dec.Decode(status); err != nil { if err := dec.Decode(status); err != nil {
return fmt.Errorf("Error while decoding JSON response: %s", err) return nil, fmt.Errorf("error while decoding JSON response: %s", err)
} }
status.Gather(forecast, acc) return status, nil
return nil
} }
func (s *Status) Gather(forecast bool, acc telegraf.Accumulator) { func gatherWeather(acc telegraf.Accumulator, status *Status) {
tags := map[string]string{ for _, e := range status.List {
"city_id": strconv.FormatInt(s.City.Id, 10),
"forecast": "*",
}
for i, e := range s.List {
tm := time.Unix(e.Dt, 0) tm := time.Unix(e.Dt, 0)
if e.Id > 0 {
tags["city_id"] = strconv.FormatInt(e.Id, 10)
}
if forecast {
tags["forecast"] = fmt.Sprintf("%dh", (i+1)*3)
}
acc.AddFields( acc.AddFields(
"weather", "weather",
map[string]interface{}{ map[string]interface{}{
"rain": e.Rain.Rain3, "cloudiness": e.Clouds.All,
"wind_degrees": e.Wind.Deg,
"wind_speed": e.Wind.Speed,
"humidity": e.Main.Humidity, "humidity": e.Main.Humidity,
"pressure": e.Main.Pressure, "pressure": e.Main.Pressure,
"rain": e.Rain.Rain3,
"sunrise": time.Unix(e.Sys.Sunrise, 0).UnixNano(),
"sunset": time.Unix(e.Sys.Sunset, 0).UnixNano(),
"temperature": e.Main.Temp, "temperature": e.Main.Temp,
"visibility": e.Visibility,
"wind_degrees": e.Wind.Deg,
"wind_speed": e.Wind.Speed,
},
map[string]string{
"city": e.Name,
"city_id": strconv.FormatInt(e.Id, 10),
"country": e.Sys.Country,
"forecast": "*",
},
tm)
}
}
func gatherForecast(acc telegraf.Accumulator, status *Status) {
tags := map[string]string{
"city_id": strconv.FormatInt(status.City.Id, 10),
"forecast": "*",
"city": status.City.Name,
"country": status.City.Country,
}
for i, e := range status.List {
tm := time.Unix(e.Dt, 0)
tags["forecast"] = fmt.Sprintf("%dh", (i+1)*3)
acc.AddFields(
"weather",
map[string]interface{}{
"cloudiness": e.Clouds.All,
"humidity": e.Main.Humidity,
"pressure": e.Main.Pressure,
"rain": e.Rain.Rain3,
"temperature": e.Main.Temp,
"wind_degrees": e.Wind.Deg,
"wind_speed": e.Wind.Speed,
}, },
tags, tags,
tm) tm)
} }
if forecast {
// intentional: overwrite future data points
// under the * tag
tags := map[string]string{
"city_id": strconv.FormatInt(s.City.Id, 10),
"forecast": "*",
}
for _, e := range s.List {
tm := time.Unix(e.Dt, 0)
if e.Id > 0 {
tags["city_id"] = strconv.FormatInt(e.Id, 10)
}
acc.AddFields(
"weather",
map[string]interface{}{
"rain": e.Rain.Rain3,
"wind_degrees": e.Wind.Deg,
"wind_speed": e.Wind.Speed,
"humidity": e.Main.Humidity,
"pressure": e.Main.Pressure,
"temperature": e.Main.Temp,
},
tags,
tm)
}
}
} }
func init() { func init() {
@ -300,6 +311,7 @@ func init() {
return &OpenWeatherMap{ return &OpenWeatherMap{
ResponseTimeout: tmout, ResponseTimeout: tmout,
Units: defaultUnits, Units: defaultUnits,
BaseUrl: defaultBaseURL,
} }
}) })
} }

View File

@ -7,6 +7,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -105,6 +106,9 @@ const groupWeatherResponse = `
{ {
"cnt": 1, "cnt": 1,
"list": [{ "list": [{
"clouds": {
"all": 0
},
"coord": { "coord": {
"lat": 48.85, "lat": 48.85,
"lon": 2.35 "lon": 2.35
@ -282,13 +286,20 @@ func TestForecastGeneratesMetrics(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
err_openweathermap := n.Gather(&acc) err := n.Gather(&acc)
require.NoError(t, err_openweathermap) require.NoError(t, err)
for _, forecast_tag := range []string{"*", "3h"} {
acc.AssertContainsTaggedFields( expected := []telegraf.Metric{
t, testutil.MustMetric(
"weather", "weather",
map[string]string{
"city_id": "2988507",
"forecast": "3h",
"city": "Paris",
"country": "FR",
},
map[string]interface{}{ map[string]interface{}{
"cloudiness": int64(88),
"humidity": int64(91), "humidity": int64(91),
"pressure": 1018.65, "pressure": 1018.65,
"temperature": 6.71, "temperature": 6.71,
@ -296,16 +307,18 @@ func TestForecastGeneratesMetrics(t *testing.T) {
"wind_degrees": 228.501, "wind_degrees": 228.501,
"wind_speed": 3.76, "wind_speed": 3.76,
}, },
time.Unix(1543622400, 0),
),
testutil.MustMetric(
"weather",
map[string]string{ map[string]string{
"city_id": "2988507", "city_id": "2988507",
"forecast": forecast_tag, "forecast": "6h",
}) "city": "Paris",
} "country": "FR",
for _, forecast_tag := range []string{"*", "6h"} { },
acc.AssertContainsTaggedFields(
t,
"weather",
map[string]interface{}{ map[string]interface{}{
"cloudiness": int64(92),
"humidity": int64(98), "humidity": int64(98),
"pressure": 1032.18, "pressure": 1032.18,
"temperature": 6.38, "temperature": 6.38,
@ -313,11 +326,13 @@ func TestForecastGeneratesMetrics(t *testing.T) {
"wind_degrees": 335.005, "wind_degrees": 335.005,
"wind_speed": 2.66, "wind_speed": 2.66,
}, },
map[string]string{ time.Unix(1544043600, 0),
"city_id": "2988507", ),
"forecast": forecast_tag,
})
} }
testutil.RequireMetricsEqual(t,
expected, acc.GetTelegrafMetrics(),
testutil.SortMetrics())
} }
func TestWeatherGeneratesMetrics(t *testing.T) { func TestWeatherGeneratesMetrics(t *testing.T) {
@ -346,25 +361,34 @@ func TestWeatherGeneratesMetrics(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
err_openweathermap := n.Gather(&acc) err := n.Gather(&acc)
require.NoError(t, err)
require.NoError(t, err_openweathermap) expected := []telegraf.Metric{
testutil.MustMetric(
acc.AssertContainsTaggedFields( "weather",
t, map[string]string{
"weather", "city_id": "2988507",
map[string]interface{}{ "forecast": "*",
"humidity": int64(87), "city": "Paris",
"pressure": 1007.0, "country": "FR",
"temperature": 9.25, },
"wind_degrees": 290.0, map[string]interface{}{
"wind_speed": 8.7, "cloudiness": int64(0),
"rain": 0.0, "humidity": int64(87),
}, "pressure": 1007.0,
map[string]string{ "temperature": 9.25,
"city_id": "2988507", "rain": 0.0,
"forecast": "*", "sunrise": int64(1544167818000000000),
}) "sunset": int64(1544198047000000000),
"wind_degrees": 290.0,
"wind_speed": 8.7,
"visibility": 10000,
},
time.Unix(1544194800, 0),
),
}
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics())
} }
func TestBatchWeatherGeneratesMetrics(t *testing.T) { func TestBatchWeatherGeneratesMetrics(t *testing.T) {
@ -393,90 +417,78 @@ func TestBatchWeatherGeneratesMetrics(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
err_openweathermap := n.Gather(&acc) err := n.Gather(&acc)
require.NoError(t, err)
require.NoError(t, err_openweathermap) expected := []telegraf.Metric{
testutil.MustMetric(
acc.AssertContainsTaggedFields( "weather",
t, map[string]string{
"weather", "city_id": "524901",
map[string]interface{}{ "forecast": "*",
"humidity": int64(46), "city": "Moscow",
"pressure": 1014.0, "country": "RU",
"temperature": 9.57, },
"wind_degrees": 60.0, map[string]interface{}{
"wind_speed": 5.0, "cloudiness": 40,
"rain": 0.0, "humidity": int64(46),
}, "pressure": 1014.0,
map[string]string{ "temperature": 9.57,
"city_id": "524901", "wind_degrees": 60.0,
"forecast": "*", "wind_speed": 5.0,
}) "rain": 0.0,
acc.AssertContainsTaggedFields( "sunrise": int64(1556416455000000000),
t, "sunset": int64(1556470779000000000),
"weather", "visibility": 10000,
map[string]interface{}{ },
"humidity": int64(63), time.Unix(1556444155, 0),
"pressure": 1009.0, ),
"temperature": 19.29, testutil.MustMetric(
"wind_degrees": 0.0, "weather",
"wind_speed": 1.0, map[string]string{
"rain": 0.0, "city_id": "703448",
}, "forecast": "*",
map[string]string{ "city": "Kiev",
"city_id": "703448", "country": "UA",
"forecast": "*", },
}) map[string]interface{}{
acc.AssertContainsTaggedFields( "cloudiness": 0,
t, "humidity": int64(63),
"weather", "pressure": 1009.0,
map[string]interface{}{ "temperature": 19.29,
"humidity": int64(66), "wind_degrees": 0.0,
"pressure": 1019.0, "wind_speed": 1.0,
"temperature": 10.62, "rain": 0.0,
"wind_degrees": 290.0, "sunrise": int64(1556419155000000000),
"wind_speed": 6.2, "sunset": int64(1556471486000000000),
"rain": 0.072, "visibility": 10000,
}, },
map[string]string{ time.Unix(1556444155, 0),
"city_id": "2643743", ),
"forecast": "*", testutil.MustMetric(
}) "weather",
} map[string]string{
"city_id": "2643743",
func TestResponseTimeout(t *testing.T) { "forecast": "*",
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { "city": "London",
var rsp string "country": "GB",
if r.URL.Path == "/data/2.5/group" { },
rsp = groupWeatherResponse map[string]interface{}{
w.Header()["Content-Type"] = []string{"application/json"} "cloudiness": 75,
} else if r.URL.Path == "/data/2.5/forecast" { "humidity": int64(66),
rsp = sampleStatusResponse "pressure": 1019.0,
w.Header()["Content-Type"] = []string{"application/json"} "temperature": 10.62,
} else { "wind_degrees": 290.0,
panic("Cannot handle request") "wind_speed": 6.2,
} "rain": 0.072,
"sunrise": int64(1556426319000000000),
time.Sleep(time.Second * 6) // Cause timeout "sunset": int64(1556479032000000000),
fmt.Fprintln(w, rsp) "visibility": 10000,
})) },
defer ts.Close() time.Unix(1556444155, 0),
),
n := &OpenWeatherMap{
BaseUrl: ts.URL,
AppId: "noappid",
CityId: []string{"2988507"},
Fetch: []string{"weather"},
Units: "metric",
} }
testutil.RequireMetricsEqual(t,
var acc testutil.Accumulator expected, acc.GetTelegrafMetrics(),
testutil.SortMetrics())
err_openweathermap := n.Gather(&acc)
require.NoError(t, err_openweathermap)
acc.AssertDoesNotContainMeasurement(
t,
"weather",
)
} }

View File

@ -320,26 +320,6 @@ func (a *Accumulator) WaitError(n int) {
a.Unlock() a.Unlock()
} }
func (a *Accumulator) assertContainsTaggedFields(
t *testing.T,
measurement string,
fields map[string]interface{},
tags map[string]string,
) {
for _, p := range a.Metrics {
if !reflect.DeepEqual(tags, p.Tags) {
continue
}
if p.Measurement == measurement {
assert.Equal(t, fields, p.Fields)
return
}
}
msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags)
assert.Fail(t, msg)
}
func (a *Accumulator) AssertContainsTaggedFields( func (a *Accumulator) AssertContainsTaggedFields(
t *testing.T, t *testing.T,
measurement string, measurement string,
@ -357,7 +337,8 @@ func (a *Accumulator) AssertContainsTaggedFields(
return return
} }
} }
a.assertContainsTaggedFields(t, measurement, fields, tags) msg := fmt.Sprintf("unknown measurement %s with tags %v", measurement, tags)
assert.Fail(t, msg)
} }
func (a *Accumulator) AssertDoesNotContainsTaggedFields( func (a *Accumulator) AssertDoesNotContainsTaggedFields(