From 02408455975ecee192f31bafaf031fd6569bb5a4 Mon Sep 17 00:00:00 2001 From: David Reimschussel Date: Wed, 9 Oct 2019 14:35:19 -0600 Subject: [PATCH 1/6] Add lang parameter to OpenWeathermap input plugin New fields: condition_description, condition_icon New tags: condition_id, condition_main --- .../inputs/openweathermap/openweathermap.go | 129 +++++++++------ .../openweathermap/openweathermap_test.go | 154 +++++++++++------- 2 files changed, 172 insertions(+), 111 deletions(-) diff --git a/plugins/inputs/openweathermap/openweathermap.go b/plugins/inputs/openweathermap/openweathermap.go index c15ee3832edc5..5613b6c2cf1ce 100644 --- a/plugins/inputs/openweathermap/openweathermap.go +++ b/plugins/inputs/openweathermap/openweathermap.go @@ -23,20 +23,23 @@ const ( // The limit of locations is 20. owmRequestSeveralCityId int = 20 - defaultBaseURL = "https://api.openweathermap.org/" + defaultSiteURL = "https://api.openweathermap.org/" defaultResponseTimeout time.Duration = time.Second * 5 defaultUnits string = "metric" + defaultLang string = "en" ) type OpenWeatherMap struct { AppId string `toml:"app_id"` CityId []string `toml:"city_id"` + Lang string `toml:"lang"` Fetch []string `toml:"fetch"` - BaseUrl string `toml:"base_url"` + SiteURL string `toml:"base_url"` ResponseTimeout internal.Duration `toml:"response_timeout"` Units string `toml:"units"` - client *http.Client + client *http.Client + baseURL *url.URL } var sampleConfig = ` @@ -46,6 +49,9 @@ var sampleConfig = ` ## City ID's to collect weather data from. city_id = ["5391959"] + ## Language of the description. See https://openweathermap.org/current#multi for language values. + lang = "en" + ## APIs to fetch; can contain "weather" or "forecast". fetch = ["weather", "forecast"] @@ -73,12 +79,17 @@ func (n *OpenWeatherMap) Description() string { } func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error { - var wg sync.WaitGroup - var strs []string - - base, err := url.Parse(n.BaseUrl) - if err != nil { - return err + var ( + wg sync.WaitGroup + strs []string + err error + ) + + if n.baseURL == nil { + n.baseURL, err = url.Parse(n.SiteURL) + if err != nil { + return err + } } // Create an HTTP client that is re-used for each @@ -91,26 +102,16 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error { n.client = client } - units := n.Units switch n.Units { - case "imperial", "standard": - break + case "imperial", "standard", "metric": default: - units = defaultUnits + return fmt.Errorf("unknown units: %s", n.Units) } for _, fetch := range n.Fetch { if fetch == "forecast" { - var u *url.URL - 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)) - if err != nil { - acc.AddError(fmt.Errorf("unable to parse address '%s': %s", u, err)) - continue - } - - addr := base.ResolveReference(u).String() + addr := n.formatURL("/data/2.5/forecast", city) wg.Add(1) go func() { defer wg.Done() @@ -126,7 +127,6 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error { } else if fetch == "weather" { j := 0 for j < len(n.CityId) { - var u *url.URL strs = make([]string, 0) for i := 0; j < len(n.CityId) && i < owmRequestSeveralCityId; i++ { strs = append(strs, n.CityId[j]) @@ -134,13 +134,7 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error { } cities := strings.Join(strs, ",") - u, err = url.Parse(fmt.Sprintf("/data/2.5/group?id=%s&APPID=%s&units=%s", cities, n.AppId, units)) - if err != nil { - acc.AddError(fmt.Errorf("Unable to parse address '%s': %s", u, err)) - continue - } - - addr := base.ResolveReference(u).String() + addr := n.formatURL("/data/2.5/group", cities) wg.Add(1) go func() { defer wg.Done() @@ -226,6 +220,12 @@ type WeatherEntry struct { Lon float64 `json:"lon"` } `json:"coord"` Visibility int64 `json:"visibility"` + Weather []struct { + ID int64 `json:"id"` + Main string `json:"main"` + Description string `json:"description"` + Icon string `json:"icon"` + } `json:"weather"` } type Status struct { @@ -253,27 +253,34 @@ func gatherWeatherUrl(r io.Reader) (*Status, error) { func gatherWeather(acc telegraf.Accumulator, status *Status) { for _, e := range status.List { tm := time.Unix(e.Dt, 0) - acc.AddFields( - "weather", - map[string]interface{}{ - "cloudiness": e.Clouds.All, - "humidity": e.Main.Humidity, - "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, - "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) + + fields := map[string]interface{}{ + "cloudiness": e.Clouds.All, + "humidity": e.Main.Humidity, + "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, + "visibility": e.Visibility, + "wind_degrees": e.Wind.Deg, + "wind_speed": e.Wind.Speed, + } + tags := map[string]string{ + "city": e.Name, + "city_id": strconv.FormatInt(e.Id, 10), + "country": e.Sys.Country, + "forecast": "*", + } + + if len(e.Weather) > 0 { + fields["condition_description"] = e.Weather[0].Description + fields["condition_icon"] = e.Weather[0].Icon + tags["condition_id"] = strconv.FormatInt(e.Weather[0].ID, 10) + tags["condition_main"] = e.Weather[0].Main + } + + acc.AddFields("weather", fields, tags, tm) } } @@ -311,7 +318,25 @@ func init() { return &OpenWeatherMap{ ResponseTimeout: tmout, Units: defaultUnits, - BaseUrl: defaultBaseURL, + SiteURL: defaultSiteURL, } }) } + +func (n *OpenWeatherMap) formatURL(path string, city string) string { + + //u, err := url.Parse(fmt.Sprintf("/data/2.5/forecast?id=%s&APPID=%s&units=%s&lang=%s", city, n.AppId, units, n.Lang)) + v := url.Values{ + "id": []string{city}, + "APPID": []string{n.AppId}, + "units": []string{n.Units}, + "lang": []string{n.Lang}, + } + + relative := &url.URL{ + Path: path, + RawQuery: v.Encode(), + } + + return n.baseURL.ResolveReference(relative).String() +} diff --git a/plugins/inputs/openweathermap/openweathermap_test.go b/plugins/inputs/openweathermap/openweathermap_test.go index d59766dd6b7de..37bfc3451b002 100644 --- a/plugins/inputs/openweathermap/openweathermap_test.go +++ b/plugins/inputs/openweathermap/openweathermap_test.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "net/url" "testing" "time" @@ -277,7 +278,7 @@ func TestForecastGeneratesMetrics(t *testing.T) { defer ts.Close() n := &OpenWeatherMap{ - BaseUrl: ts.URL, + SiteURL: ts.URL, AppId: "noappid", CityId: []string{"2988507"}, Fetch: []string{"weather", "forecast"}, @@ -352,7 +353,7 @@ func TestWeatherGeneratesMetrics(t *testing.T) { defer ts.Close() n := &OpenWeatherMap{ - BaseUrl: ts.URL, + SiteURL: ts.URL, AppId: "noappid", CityId: []string{"2988507"}, Fetch: []string{"weather"}, @@ -368,22 +369,26 @@ func TestWeatherGeneratesMetrics(t *testing.T) { testutil.MustMetric( "weather", map[string]string{ - "city_id": "2988507", - "forecast": "*", - "city": "Paris", - "country": "FR", + "city_id": "2988507", + "forecast": "*", + "city": "Paris", + "country": "FR", + "condition_id": "300", + "condition_main": "Drizzle", }, map[string]interface{}{ - "cloudiness": int64(0), - "humidity": int64(87), - "pressure": 1007.0, - "temperature": 9.25, - "rain": 0.0, - "sunrise": int64(1544167818000000000), - "sunset": int64(1544198047000000000), - "wind_degrees": 290.0, - "wind_speed": 8.7, - "visibility": 10000, + "cloudiness": int64(0), + "humidity": int64(87), + "pressure": 1007.0, + "temperature": 9.25, + "rain": 0.0, + "sunrise": int64(1544167818000000000), + "sunset": int64(1544198047000000000), + "wind_degrees": 290.0, + "wind_speed": 8.7, + "visibility": 10000, + "condition_description": "light intensity drizzle", + "condition_icon": "09d", }, time.Unix(1544194800, 0), ), @@ -408,7 +413,7 @@ func TestBatchWeatherGeneratesMetrics(t *testing.T) { defer ts.Close() n := &OpenWeatherMap{ - BaseUrl: ts.URL, + SiteURL: ts.URL, AppId: "noappid", CityId: []string{"524901", "703448", "2643743"}, Fetch: []string{"weather"}, @@ -424,66 +429,78 @@ func TestBatchWeatherGeneratesMetrics(t *testing.T) { testutil.MustMetric( "weather", map[string]string{ - "city_id": "524901", - "forecast": "*", - "city": "Moscow", - "country": "RU", + "city_id": "524901", + "forecast": "*", + "city": "Moscow", + "country": "RU", + "condition_id": "802", + "condition_main": "Clouds", }, map[string]interface{}{ - "cloudiness": 40, - "humidity": int64(46), - "pressure": 1014.0, - "temperature": 9.57, - "wind_degrees": 60.0, - "wind_speed": 5.0, - "rain": 0.0, - "sunrise": int64(1556416455000000000), - "sunset": int64(1556470779000000000), - "visibility": 10000, + "cloudiness": 40, + "humidity": int64(46), + "pressure": 1014.0, + "temperature": 9.57, + "wind_degrees": 60.0, + "wind_speed": 5.0, + "rain": 0.0, + "sunrise": int64(1556416455000000000), + "sunset": int64(1556470779000000000), + "visibility": 10000, + "condition_description": "scattered clouds", + "condition_icon": "03d", }, time.Unix(1556444155, 0), ), testutil.MustMetric( "weather", map[string]string{ - "city_id": "703448", - "forecast": "*", - "city": "Kiev", - "country": "UA", + "city_id": "703448", + "forecast": "*", + "city": "Kiev", + "country": "UA", + "condition_id": "520", + "condition_main": "Rain", }, map[string]interface{}{ - "cloudiness": 0, - "humidity": int64(63), - "pressure": 1009.0, - "temperature": 19.29, - "wind_degrees": 0.0, - "wind_speed": 1.0, - "rain": 0.0, - "sunrise": int64(1556419155000000000), - "sunset": int64(1556471486000000000), - "visibility": 10000, + "cloudiness": 0, + "humidity": int64(63), + "pressure": 1009.0, + "temperature": 19.29, + "wind_degrees": 0.0, + "wind_speed": 1.0, + "rain": 0.0, + "sunrise": int64(1556419155000000000), + "sunset": int64(1556471486000000000), + "visibility": 10000, + "condition_description": "light intensity shower rain", + "condition_icon": "09d", }, time.Unix(1556444155, 0), ), testutil.MustMetric( "weather", map[string]string{ - "city_id": "2643743", - "forecast": "*", - "city": "London", - "country": "GB", + "city_id": "2643743", + "forecast": "*", + "city": "London", + "country": "GB", + "condition_id": "803", + "condition_main": "Clouds", }, map[string]interface{}{ - "cloudiness": 75, - "humidity": int64(66), - "pressure": 1019.0, - "temperature": 10.62, - "wind_degrees": 290.0, - "wind_speed": 6.2, - "rain": 0.072, - "sunrise": int64(1556426319000000000), - "sunset": int64(1556479032000000000), - "visibility": 10000, + "cloudiness": 75, + "humidity": int64(66), + "pressure": 1019.0, + "temperature": 10.62, + "wind_degrees": 290.0, + "wind_speed": 6.2, + "rain": 0.072, + "sunrise": int64(1556426319000000000), + "sunset": int64(1556479032000000000), + "visibility": 10000, + "condition_description": "broken clouds", + "condition_icon": "04d", }, time.Unix(1556444155, 0), ), @@ -492,3 +509,22 @@ func TestBatchWeatherGeneratesMetrics(t *testing.T) { expected, acc.GetTelegrafMetrics(), testutil.SortMetrics()) } + +func TestFormatURL(t *testing.T) { + b, _ := url.Parse("http://foo.com") + n := &OpenWeatherMap{ + AppId: "appid", + Units: "units", + Lang: "lang", + baseURL: b, + } + + t.Run("path 1", func(t *testing.T) { + s := n.formatURL("/data/2.5/forecast", "12345") + if want := "http://foo.com/data/2.5/forecast?APPID=appid&id=12345&lang=lang&units=units"; s != want { + t.Errorf("FormatOwnUrl: got %s, want %s", s, want) + } + }) + + //t.Errorf("bad things happened") +} From 89814242ff07171eb9cd149658a8e039f5cb8876 Mon Sep 17 00:00:00 2001 From: David Reimschussel Date: Wed, 9 Oct 2019 17:09:56 -0600 Subject: [PATCH 2/6] Add new conditions tags and fields for forecast API --- .../inputs/openweathermap/openweathermap.go | 45 ++++++++-------- .../openweathermap/openweathermap_test.go | 52 +++++++++++-------- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/plugins/inputs/openweathermap/openweathermap.go b/plugins/inputs/openweathermap/openweathermap.go index 5613b6c2cf1ce..e57a16d57fb3c 100644 --- a/plugins/inputs/openweathermap/openweathermap.go +++ b/plugins/inputs/openweathermap/openweathermap.go @@ -49,7 +49,7 @@ var sampleConfig = ` ## City ID's to collect weather data from. city_id = ["5391959"] - ## Language of the description. See https://openweathermap.org/current#multi for language values. + ## Language of the description field. lang = "en" ## APIs to fetch; can contain "weather" or "forecast". @@ -285,28 +285,31 @@ func gatherWeather(acc telegraf.Accumulator, status *Status) { } 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) + fields := 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 := map[string]string{ + "city_id": strconv.FormatInt(status.City.Id, 10), + "forecast": "*", + "city": status.City.Name, + "country": status.City.Country, + } + if len(e.Weather) > 0 { + fields["condition_description"] = e.Weather[0].Description + fields["condition_icon"] = e.Weather[0].Icon + tags["condition_id"] = strconv.FormatInt(e.Weather[0].ID, 10) + tags["condition_main"] = e.Weather[0].Main + } 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, - tm) + acc.AddFields("weather", fields, tags, tm) } } @@ -324,8 +327,6 @@ func init() { } func (n *OpenWeatherMap) formatURL(path string, city string) string { - - //u, err := url.Parse(fmt.Sprintf("/data/2.5/forecast?id=%s&APPID=%s&units=%s&lang=%s", city, n.AppId, units, n.Lang)) v := url.Values{ "id": []string{city}, "APPID": []string{n.AppId}, diff --git a/plugins/inputs/openweathermap/openweathermap_test.go b/plugins/inputs/openweathermap/openweathermap_test.go index 37bfc3451b002..15112029898a4 100644 --- a/plugins/inputs/openweathermap/openweathermap_test.go +++ b/plugins/inputs/openweathermap/openweathermap_test.go @@ -294,38 +294,46 @@ func TestForecastGeneratesMetrics(t *testing.T) { testutil.MustMetric( "weather", map[string]string{ - "city_id": "2988507", - "forecast": "3h", - "city": "Paris", - "country": "FR", + "city_id": "2988507", + "forecast": "3h", + "city": "Paris", + "country": "FR", + "condition_id": "500", + "condition_main": "Rain", }, map[string]interface{}{ - "cloudiness": int64(88), - "humidity": int64(91), - "pressure": 1018.65, - "temperature": 6.71, - "rain": 0.035, - "wind_degrees": 228.501, - "wind_speed": 3.76, + "cloudiness": int64(88), + "humidity": int64(91), + "pressure": 1018.65, + "temperature": 6.71, + "rain": 0.035, + "wind_degrees": 228.501, + "wind_speed": 3.76, + "condition_description": "light rain", + "condition_icon": "10n", }, time.Unix(1543622400, 0), ), testutil.MustMetric( "weather", map[string]string{ - "city_id": "2988507", - "forecast": "6h", - "city": "Paris", - "country": "FR", + "city_id": "2988507", + "forecast": "6h", + "city": "Paris", + "country": "FR", + "condition_id": "500", + "condition_main": "Rain", }, map[string]interface{}{ - "cloudiness": int64(92), - "humidity": int64(98), - "pressure": 1032.18, - "temperature": 6.38, - "rain": 0.049999999999997, - "wind_degrees": 335.005, - "wind_speed": 2.66, + "cloudiness": int64(92), + "humidity": int64(98), + "pressure": 1032.18, + "temperature": 6.38, + "rain": 0.049999999999997, + "wind_degrees": 335.005, + "wind_speed": 2.66, + "condition_description": "light rain", + "condition_icon": "10n", }, time.Unix(1544043600, 0), ), From 39836f6c5a3ce96dc282cf32e0ad1494a2d2a8dc Mon Sep 17 00:00:00 2001 From: David Reimschussel Date: Wed, 9 Oct 2019 17:10:23 -0600 Subject: [PATCH 3/6] Update README.md for new conditions fields and tags --- plugins/inputs/openweathermap/README.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/openweathermap/README.md b/plugins/inputs/openweathermap/README.md index d796990495a38..fac68b51f6ce2 100644 --- a/plugins/inputs/openweathermap/README.md +++ b/plugins/inputs/openweathermap/README.md @@ -4,9 +4,11 @@ Collect current weather and forecast data from OpenWeatherMap. To use this plugin you will need an [api key][] (app_id). -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 +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. Language +identifiers can be found in the [lang list][]. Documentation for +condition ID, icon, and main is at [weather conditions][]. ### Configuration @@ -18,6 +20,9 @@ https://openweathermap.org/city/2643743 ## City ID's to collect weather data from. city_id = ["5391959"] + ## Language of the description field. + lang = "en" + ## APIs to fetch; can contain "weather" or "forecast". fetch = ["weather", "forecast"] @@ -42,6 +47,8 @@ https://openweathermap.org/city/2643743 - tags: - city_id - forecast + - condition_id + - condition_main - fields: - cloudiness (int, percent) - humidity (int, percent) @@ -53,16 +60,20 @@ https://openweathermap.org/city/2643743 - 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) + - condition_description (string, localized long description) + - condition_icon ### Example Output ``` -> 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 -> 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 -> 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 +> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=*,host=debian cloudiness=1i,condition_description="clear sky",condition_icon="01d",humidity=35i,pressure=1012,rain=0,sunrise=1570630329000000000i,sunset=1570671689000000000i,temperature=21.52,visibility=16093i,wind_degrees=280,wind_speed=5.7 1570659256000000000 +> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=3h,host=debian cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=41i,pressure=1010,rain=0,temperature=22.34,wind_degrees=249.393,wind_speed=2.085 1570665600000000000 +> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=6h,host=debian cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=50i,pressure=1012,rain=0,temperature=17.09,wind_degrees=310.754,wind_speed=3.009 1570676400000000000 ``` [api key]: https://openweathermap.org/appid [city list]: http://bulk.openweathermap.org/sample/city.list.json.gz [search]: https://openweathermap.org/find +[lang list]: https://openweathermap.org/current#multi +[weather conditions]: https://openweathermap.org/weather-conditions From b510d4d74b2e13c5f96ac24491ed329c30ccb1e9 Mon Sep 17 00:00:00 2001 From: David Reimschussel Date: Thu, 10 Oct 2019 17:10:51 -0600 Subject: [PATCH 4/6] Made changes requested in glinton's review --- plugins/inputs/openweathermap/README.md | 6 ++-- .../inputs/openweathermap/openweathermap.go | 29 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/plugins/inputs/openweathermap/README.md b/plugins/inputs/openweathermap/README.md index fac68b51f6ce2..4933b2235d7ad 100644 --- a/plugins/inputs/openweathermap/README.md +++ b/plugins/inputs/openweathermap/README.md @@ -67,9 +67,9 @@ condition ID, icon, and main is at [weather conditions][]. ### Example Output ``` -> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=*,host=debian cloudiness=1i,condition_description="clear sky",condition_icon="01d",humidity=35i,pressure=1012,rain=0,sunrise=1570630329000000000i,sunset=1570671689000000000i,temperature=21.52,visibility=16093i,wind_degrees=280,wind_speed=5.7 1570659256000000000 -> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=3h,host=debian cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=41i,pressure=1010,rain=0,temperature=22.34,wind_degrees=249.393,wind_speed=2.085 1570665600000000000 -> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=6h,host=debian cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=50i,pressure=1012,rain=0,temperature=17.09,wind_degrees=310.754,wind_speed=3.009 1570676400000000000 +> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=* cloudiness=1i,condition_description="clear sky",condition_icon="01d",humidity=35i,pressure=1012,rain=0,sunrise=1570630329000000000i,sunset=1570671689000000000i,temperature=21.52,visibility=16093i,wind_degrees=280,wind_speed=5.7 1570659256000000000 +> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=3h cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=41i,pressure=1010,rain=0,temperature=22.34,wind_degrees=249.393,wind_speed=2.085 1570665600000000000 +> weather,city=San\ Francisco,city_id=5391959,condition_id=800,condition_main=Clear,country=US,forecast=6h cloudiness=0i,condition_description="clear sky",condition_icon="01n",humidity=50i,pressure=1012,rain=0,temperature=17.09,wind_degrees=310.754,wind_speed=3.009 1570676400000000000 ``` [api key]: https://openweathermap.org/appid diff --git a/plugins/inputs/openweathermap/openweathermap.go b/plugins/inputs/openweathermap/openweathermap.go index e57a16d57fb3c..d918376c97298 100644 --- a/plugins/inputs/openweathermap/openweathermap.go +++ b/plugins/inputs/openweathermap/openweathermap.go @@ -25,8 +25,6 @@ const ( defaultSiteURL = "https://api.openweathermap.org/" defaultResponseTimeout time.Duration = time.Second * 5 - defaultUnits string = "metric" - defaultLang string = "en" ) type OpenWeatherMap struct { @@ -50,7 +48,7 @@ var sampleConfig = ` city_id = ["5391959"] ## Language of the description field. - lang = "en" + # lang = "en" ## APIs to fetch; can contain "weather" or "forecast". fetch = ["weather", "forecast"] @@ -103,7 +101,7 @@ func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error { } switch n.Units { - case "imperial", "standard", "metric": + case "imperial", "standard", "metric", "": default: return fmt.Errorf("unknown units: %s", n.Units) } @@ -285,6 +283,12 @@ func gatherWeather(acc telegraf.Accumulator, status *Status) { } 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) fields := map[string]interface{}{ @@ -296,12 +300,6 @@ func gatherForecast(acc telegraf.Accumulator, status *Status) { "wind_degrees": e.Wind.Deg, "wind_speed": e.Wind.Speed, } - tags := map[string]string{ - "city_id": strconv.FormatInt(status.City.Id, 10), - "forecast": "*", - "city": status.City.Name, - "country": status.City.Country, - } if len(e.Weather) > 0 { fields["condition_description"] = e.Weather[0].Description fields["condition_icon"] = e.Weather[0].Icon @@ -320,7 +318,6 @@ func init() { } return &OpenWeatherMap{ ResponseTimeout: tmout, - Units: defaultUnits, SiteURL: defaultSiteURL, } }) @@ -330,8 +327,14 @@ func (n *OpenWeatherMap) formatURL(path string, city string) string { v := url.Values{ "id": []string{city}, "APPID": []string{n.AppId}, - "units": []string{n.Units}, - "lang": []string{n.Lang}, + } + + if n.Units != "" { + v["units"] = []string{n.Units} + } + + if n.Lang != "" { + v["lang"] = []string{n.Lang} } relative := &url.URL{ From 3ccb586826d6d34f0fd5774cf6aec8597cf9315f Mon Sep 17 00:00:00 2001 From: David Reimschussel Date: Tue, 15 Oct 2019 15:15:32 -0600 Subject: [PATCH 5/6] changed requested in reviews --- .../inputs/openweathermap/openweathermap.go | 66 +++++++++---------- .../openweathermap/openweathermap_test.go | 30 +++++---- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/plugins/inputs/openweathermap/openweathermap.go b/plugins/inputs/openweathermap/openweathermap.go index d918376c97298..331d8caaa1b19 100644 --- a/plugins/inputs/openweathermap/openweathermap.go +++ b/plugins/inputs/openweathermap/openweathermap.go @@ -23,8 +23,9 @@ const ( // The limit of locations is 20. owmRequestSeveralCityId int = 20 - defaultSiteURL = "https://api.openweathermap.org/" + defaultBaseUrl = "https://api.openweathermap.org/" defaultResponseTimeout time.Duration = time.Second * 5 + defaultUnits string = "metric" ) type OpenWeatherMap struct { @@ -32,12 +33,12 @@ type OpenWeatherMap struct { CityId []string `toml:"city_id"` Lang string `toml:"lang"` Fetch []string `toml:"fetch"` - SiteURL string `toml:"base_url"` + BaseUrl string `toml:"base_url"` ResponseTimeout internal.Duration `toml:"response_timeout"` Units string `toml:"units"` client *http.Client - baseURL *url.URL + baseUrl *url.URL } var sampleConfig = ` @@ -77,34 +78,8 @@ func (n *OpenWeatherMap) Description() string { } func (n *OpenWeatherMap) Gather(acc telegraf.Accumulator) error { - var ( - wg sync.WaitGroup - strs []string - err error - ) - - if n.baseURL == nil { - n.baseURL, err = url.Parse(n.SiteURL) - if err != nil { - return err - } - } - - // Create an HTTP client that is re-used for each - // collection interval - if n.client == nil { - client, err := n.createHttpClient() - if err != nil { - return err - } - n.client = client - } - - switch n.Units { - case "imperial", "standard", "metric", "": - default: - return fmt.Errorf("unknown units: %s", n.Units) - } + var wg sync.WaitGroup + var strs []string for _, fetch := range n.Fetch { if fetch == "forecast" { @@ -318,11 +293,36 @@ func init() { } return &OpenWeatherMap{ ResponseTimeout: tmout, - SiteURL: defaultSiteURL, + BaseUrl: defaultBaseUrl, } }) } +func (n *OpenWeatherMap) Init() error { + var err error + n.baseUrl, err = url.Parse(n.BaseUrl) + if err != nil { + return err + } + + // Create an HTTP client that is re-used for each + // collection interval + n.client, err = n.createHttpClient() + if err != nil { + return err + } + + switch n.Units { + case "imperial", "standard", "metric": + case "": + n.Units = defaultUnits + default: + return fmt.Errorf("unknown units: %s", n.Units) + } + + return nil +} + func (n *OpenWeatherMap) formatURL(path string, city string) string { v := url.Values{ "id": []string{city}, @@ -342,5 +342,5 @@ func (n *OpenWeatherMap) formatURL(path string, city string) string { RawQuery: v.Encode(), } - return n.baseURL.ResolveReference(relative).String() + return n.baseUrl.ResolveReference(relative).String() } diff --git a/plugins/inputs/openweathermap/openweathermap_test.go b/plugins/inputs/openweathermap/openweathermap_test.go index 15112029898a4..c1efcfd5e2374 100644 --- a/plugins/inputs/openweathermap/openweathermap_test.go +++ b/plugins/inputs/openweathermap/openweathermap_test.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "net/http/httptest" - "net/url" "testing" "time" @@ -278,12 +277,13 @@ func TestForecastGeneratesMetrics(t *testing.T) { defer ts.Close() n := &OpenWeatherMap{ - SiteURL: ts.URL, + BaseUrl: ts.URL, AppId: "noappid", CityId: []string{"2988507"}, Fetch: []string{"weather", "forecast"}, Units: "metric", } + n.Init() var acc testutil.Accumulator @@ -361,12 +361,13 @@ func TestWeatherGeneratesMetrics(t *testing.T) { defer ts.Close() n := &OpenWeatherMap{ - SiteURL: ts.URL, + BaseUrl: ts.URL, AppId: "noappid", CityId: []string{"2988507"}, Fetch: []string{"weather"}, Units: "metric", } + n.Init() var acc testutil.Accumulator @@ -421,12 +422,13 @@ func TestBatchWeatherGeneratesMetrics(t *testing.T) { defer ts.Close() n := &OpenWeatherMap{ - SiteURL: ts.URL, + BaseUrl: ts.URL, AppId: "noappid", CityId: []string{"524901", "703448", "2643743"}, Fetch: []string{"weather"}, Units: "metric", } + n.Init() var acc testutil.Accumulator @@ -519,20 +521,22 @@ func TestBatchWeatherGeneratesMetrics(t *testing.T) { } func TestFormatURL(t *testing.T) { - b, _ := url.Parse("http://foo.com") n := &OpenWeatherMap{ AppId: "appid", Units: "units", Lang: "lang", - baseURL: b, + BaseUrl: "http://foo.com", } + n.Init() - t.Run("path 1", func(t *testing.T) { - s := n.formatURL("/data/2.5/forecast", "12345") - if want := "http://foo.com/data/2.5/forecast?APPID=appid&id=12345&lang=lang&units=units"; s != want { - t.Errorf("FormatOwnUrl: got %s, want %s", s, want) - } - }) + require.Equal(t, + "http://foo.com/data/2.5/forecast?APPID=appid&id=12345&lang=lang&units=units", + n.formatURL("/data/2.5/forecast", "12345")) +} + +func TestDefaultUnits(t *testing.T) { + n := &OpenWeatherMap{} + n.Init() - //t.Errorf("bad things happened") + require.Equal(t, "metric", n.Units) } From d6d05b55f70fb96f34ddaa9579e76bafa7e5bb87 Mon Sep 17 00:00:00 2001 From: David Reimschussel Date: Thu, 17 Oct 2019 13:02:50 -0600 Subject: [PATCH 6/6] Make changes requested in review --- plugins/inputs/openweathermap/README.md | 7 +++-- .../inputs/openweathermap/openweathermap.go | 27 ++++++++++++------- .../openweathermap/openweathermap_test.go | 7 +++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/plugins/inputs/openweathermap/README.md b/plugins/inputs/openweathermap/README.md index 4933b2235d7ad..b2a9d1c0af23d 100644 --- a/plugins/inputs/openweathermap/README.md +++ b/plugins/inputs/openweathermap/README.md @@ -20,8 +20,11 @@ condition ID, icon, and main is at [weather conditions][]. ## City ID's to collect weather data from. city_id = ["5391959"] - ## Language of the description field. - lang = "en" + ## Language of the description field. Can be one of "ar", "bg", + ## "ca", "cz", "de", "el", "en", "fa", "fi", "fr", "gl", "hr", "hu", + ## "it", "ja", "kr", "la", "lt", "mk", "nl", "pl", "pt", "ro", "ru", + ## "se", "sk", "sl", "es", "tr", "ua", "vi", "zh_cn", "zh_tw" + # lang = "en" ## APIs to fetch; can contain "weather" or "forecast". fetch = ["weather", "forecast"] diff --git a/plugins/inputs/openweathermap/openweathermap.go b/plugins/inputs/openweathermap/openweathermap.go index 331d8caaa1b19..079973ddd2832 100644 --- a/plugins/inputs/openweathermap/openweathermap.go +++ b/plugins/inputs/openweathermap/openweathermap.go @@ -26,6 +26,7 @@ const ( defaultBaseUrl = "https://api.openweathermap.org/" defaultResponseTimeout time.Duration = time.Second * 5 defaultUnits string = "metric" + defaultLang string = "en" ) type OpenWeatherMap struct { @@ -48,7 +49,10 @@ var sampleConfig = ` ## City ID's to collect weather data from. city_id = ["5391959"] - ## Language of the description field. + ## Language of the description field. Can be one of "ar", "bg", + ## "ca", "cz", "de", "el", "en", "fa", "fi", "fr", "gl", "hr", "hu", + ## "it", "ja", "kr", "la", "lt", "mk", "nl", "pl", "pt", "ro", "ru", + ## "se", "sk", "sl", "es", "tr", "ua", "vi", "zh_cn", "zh_tw" # lang = "en" ## APIs to fetch; can contain "weather" or "forecast". @@ -320,6 +324,17 @@ func (n *OpenWeatherMap) Init() error { return fmt.Errorf("unknown units: %s", n.Units) } + switch n.Lang { + case "ar", "bg", "ca", "cz", "de", "el", "en", "fa", "fi", "fr", "gl", + "hr", "hu", "it", "ja", "kr", "la", "lt", "mk", "nl", "pl", + "pt", "ro", "ru", "se", "sk", "sl", "es", "tr", "ua", "vi", + "zh_cn", "zh_tw": + case "": + n.Lang = defaultLang + default: + return fmt.Errorf("unknown language: %s", n.Lang) + } + return nil } @@ -327,14 +342,8 @@ func (n *OpenWeatherMap) formatURL(path string, city string) string { v := url.Values{ "id": []string{city}, "APPID": []string{n.AppId}, - } - - if n.Units != "" { - v["units"] = []string{n.Units} - } - - if n.Lang != "" { - v["lang"] = []string{n.Lang} + "lang": []string{n.Lang}, + "units": []string{n.Units}, } relative := &url.URL{ diff --git a/plugins/inputs/openweathermap/openweathermap_test.go b/plugins/inputs/openweathermap/openweathermap_test.go index c1efcfd5e2374..20a00e5db062d 100644 --- a/plugins/inputs/openweathermap/openweathermap_test.go +++ b/plugins/inputs/openweathermap/openweathermap_test.go @@ -540,3 +540,10 @@ func TestDefaultUnits(t *testing.T) { require.Equal(t, "metric", n.Units) } + +func TestDefaultLang(t *testing.T) { + n := &OpenWeatherMap{} + n.Init() + + require.Equal(t, "en", n.Lang) +}