dogapi 1.38.0 → 1.39.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.azure-pipelines/all.yml +7 -4
  3. data/.github/CODEOWNERS +10 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +36 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
  6. data/.github/PULL_REQUEST_TEMPLATE.md +77 -0
  7. data/.github/labeler.yml +5 -0
  8. data/.github/workflows/labeler.yml +11 -0
  9. data/.github/workflows/stale.yml +39 -0
  10. data/.rubocop_todo.yml +27 -32
  11. data/CHANGELOG.md +11 -0
  12. data/CONTRIBUTING.md +111 -0
  13. data/DEVELOPMENT.md +11 -0
  14. data/LICENSE +21 -20
  15. data/LICENSE-3rdparty.csv +6 -0
  16. data/README.rdoc +7 -0
  17. data/RELEASING.md +46 -0
  18. data/SUPPORT.md +9 -0
  19. data/examples/custom_metric.rb +1 -1
  20. data/lib/capistrano/README.md +4 -1
  21. data/lib/capistrano/datadog.rb +8 -4
  22. data/lib/capistrano/datadog/v2.rb +9 -1
  23. data/lib/capistrano/datadog/v3.rb +10 -1
  24. data/lib/dogapi.rb +4 -0
  25. data/lib/dogapi/common.rb +44 -9
  26. data/lib/dogapi/event.rb +4 -0
  27. data/lib/dogapi/facade.rb +56 -8
  28. data/lib/dogapi/metric.rb +4 -0
  29. data/lib/dogapi/v1.rb +5 -0
  30. data/lib/dogapi/v1/alert.rb +4 -0
  31. data/lib/dogapi/v1/aws_integration.rb +4 -0
  32. data/lib/dogapi/v1/aws_logs.rb +4 -0
  33. data/lib/dogapi/v1/azure_integration.rb +4 -0
  34. data/lib/dogapi/v1/comment.rb +4 -0
  35. data/lib/dogapi/v1/dash.rb +10 -4
  36. data/lib/dogapi/v1/dashboard.rb +4 -0
  37. data/lib/dogapi/v1/dashboard_list.rb +4 -0
  38. data/lib/dogapi/v1/embed.rb +4 -0
  39. data/lib/dogapi/v1/event.rb +4 -0
  40. data/lib/dogapi/v1/gcp_integration.rb +4 -0
  41. data/lib/dogapi/v1/hosts.rb +4 -0
  42. data/lib/dogapi/v1/integration.rb +4 -0
  43. data/lib/dogapi/v1/metadata.rb +4 -0
  44. data/lib/dogapi/v1/metric.rb +4 -0
  45. data/lib/dogapi/v1/monitor.rb +6 -2
  46. data/lib/dogapi/v1/screenboard.rb +4 -0
  47. data/lib/dogapi/v1/search.rb +4 -0
  48. data/lib/dogapi/v1/service_check.rb +4 -0
  49. data/lib/dogapi/v1/snapshot.rb +4 -0
  50. data/lib/dogapi/v1/synthetics.rb +80 -0
  51. data/lib/dogapi/v1/tag.rb +4 -0
  52. data/lib/dogapi/v1/usage.rb +4 -0
  53. data/lib/dogapi/v1/user.rb +4 -0
  54. data/lib/dogapi/v2.rb +4 -0
  55. data/lib/dogapi/v2/dashboard_list.rb +4 -0
  56. data/lib/dogapi/version.rb +5 -1
  57. data/spec/integration/alert_spec.rb +4 -0
  58. data/spec/integration/aws_integration_spec.rb +4 -0
  59. data/spec/integration/aws_logs_spec.rb +4 -0
  60. data/spec/integration/azure_integration_spec.rb +4 -0
  61. data/spec/integration/comment_spec.rb +5 -0
  62. data/spec/integration/common_spec.rb +4 -0
  63. data/spec/integration/dash_spec.rb +7 -1
  64. data/spec/integration/dashboard_list_spec.rb +4 -0
  65. data/spec/integration/dashboard_spec.rb +4 -0
  66. data/spec/integration/embed_spec.rb +4 -0
  67. data/spec/integration/event_spec.rb +5 -0
  68. data/spec/integration/gcp_integration_spec.rb +4 -0
  69. data/spec/integration/integration_spec.rb +4 -0
  70. data/spec/integration/metadata_spec.rb +4 -0
  71. data/spec/integration/metric_spec.rb +4 -0
  72. data/spec/integration/monitor_spec.rb +15 -0
  73. data/spec/integration/screenboard_spec.rb +4 -0
  74. data/spec/integration/search_spec.rb +4 -0
  75. data/spec/integration/service_check_spec.rb +4 -0
  76. data/spec/integration/snapshot_spec.rb +4 -0
  77. data/spec/integration/synthetics_spec.rb +131 -0
  78. data/spec/integration/tag_spec.rb +4 -0
  79. data/spec/integration/usage_spec.rb +4 -0
  80. data/spec/integration/user_spec.rb +4 -0
  81. data/spec/spec_helper.rb +4 -0
  82. data/spec/unit/capistrano_spec.rb +4 -0
  83. data/spec/unit/common_spec.rb +41 -0
  84. data/spec/unit/facade_spec.rb +4 -0
  85. metadata +18 -4
@@ -0,0 +1,111 @@
1
+ # How to contribute
2
+
3
+ First of all, thanks for contributing!
4
+
5
+ This document provides some basic guidelines for contributing to this repository. To propose improvements, feel free to submit a PR.
6
+
7
+ ## Reporting a Bug - Requesting a feature - Github Issues
8
+
9
+ * **Ensure the bug was not already reported** by searching on GitHub under [Issues][1].
10
+ * If you're unable to find an open issue addressing the problem, [open a new one][2].
11
+ - **Fill out the issue template completely**. Label the issue properly.
12
+ - Add `severity/` label.
13
+ - Add `documentation` label if this issue is related to documentation changes.
14
+ * If you have a feature request, it is encouraged to [contact support][3] so the request can be prioritized and properly tracked.
15
+ * **Do not open an issue if you have a question**, instead [contact support][3].
16
+
17
+ ## Suggesting an enhancements - Pull Requests
18
+
19
+ Have you fixed an issue? Many thanks!
20
+
21
+ Read the [development guide][/DEVELOPMENT.md] for more information on how to get started.
22
+
23
+ In order to ease/speed up our review, here are some items you can check/improve when submitting your PR:
24
+ * **Ensure an [Issue has been created](#reporting)**.
25
+ * Avoid changing too many things at once.
26
+ - Make sure that your Pull Requests only fixes one Issue at the time.
27
+ * **Write tests** for the code you wrote.
28
+ * Make sure that **all tests pass locally**.
29
+ * Summarize your PR with a **meaningful title** and **fill out the pull request description template completely!**
30
+ * Add the most suitable changelog label choosing one of the following:
31
+ * `changelog/Added` for new features.
32
+ * `changelog/Changed` for changes in existing functionality.
33
+ * `changelog/Deprecated` for soon-to-be removed features.
34
+ * `changelog/Removed` for now removed features.
35
+ * `changelog/Fixed` for any bug fixes.
36
+ * `changelog/Security` in case of vulnerabilities.
37
+ * `changelog/no-changelog` in case this PR should not appear in the changelog at all.
38
+
39
+ See [here][4] for more details about changelogs.
40
+
41
+ Your pull request must pass all CI tests before. If you're seeing
42
+ an error and don't think it's your fault, it may not be! [Join us on Slack][5] or send us an email, and together we'll
43
+ get it sorted out.
44
+
45
+ ### Keep it small, focused
46
+
47
+ Avoid changing too many things at once. For instance if you're fixing two different
48
+ issues at once, it makes reviewing harder and the _time-to-release_ longer.
49
+
50
+ ### Pull Request title
51
+
52
+ Unless the PR is marked with the proper exclusion label, the title will be used
53
+ to automatically fill the changelog entries. For this reason the title must be
54
+ concise but explanatory.
55
+
56
+ ### Commit Messages
57
+
58
+ Please don't be this person: `git commit -m "Fixed stuff"`. Take a moment to
59
+ write meaningful commit messages.
60
+
61
+ The commit message should describe the reason for the change and give extra details
62
+ that will allow someone later on to understand in 5 seconds the thing you've been
63
+ working on for a day.
64
+
65
+ ### Releasing
66
+
67
+ The release procedure is managed by Datadog, instructions can be found in the [RELEASING](/RELEASING.md) document.
68
+
69
+ ## Asking a questions
70
+
71
+ Need help? Contact [Datadog support][3]
72
+
73
+ ## Additional Notes
74
+
75
+ ### Issue and Pull Request Labels
76
+
77
+ This section lists the labels we use to help us track and manage issues and pull requests.
78
+
79
+ | Label name | Usage | Description
80
+ |-------------------------------|--------------------------|------------------------------------------------------------
81
+ | `backward-incompatible` | Issues and Pull Requests | Warn for backward incompatible changes.
82
+ | `changelog/Added` | Pull Request Only | Added features results into a minor version bump.
83
+ | `changelog/Changed` | Pull Request Only | Changed features results into a major version bump.
84
+ | `changelog/Deprecated` | Pull Request Only | Deprecated features results into a major version bump.
85
+ | `changelog/Fixed` | Pull Request Only | Fixed features results into a bug fix version bump.
86
+ | `changelog/no-changelog` | Pull Request Only | Changes don't appear in changelog.
87
+ | `changelog/Removed` | Pull Request Only | Deprecated features results into a major version bump.
88
+ | `changelog/Security` | Pull Request Only | Fixed features results into a bug fix version bump.
89
+ | `community/help-wanted` | Issue Only | Community help wanted.
90
+ | `community` | Issues and Pull Requests | Community driven changes.
91
+ | `dev/testing` | Issues and Pull Requests | Tests related changes.
92
+ | `dev/tooling` | Issues and Pull Requests | Tooling related changes.
93
+ | `do-not-merge/HOLD` | Pull Request Only | Do not merge this PR.
94
+ | `do-not-merge/WIP` | Pull Request Only | Do not merge this PR.
95
+ | `documentation` | Issues and Pull Requests | Documentation related changes.
96
+ | `duplicate` | Issue Only | Duplicate issue.
97
+ | `invalid` | Issue Only | Invalid issue.
98
+ | `kind/bug` | Issue Only | Bug related issue.
99
+ | `kind/feature-request` | Issue Only | Feature request related issue.
100
+ | `severity/critical` | Issue Only | Critical severity issue.
101
+ | `severity/major` | Issue Only | Major severity issue.
102
+ | `severity/minor` | Issue Only | Minor severity issue.
103
+ | `severity/normal` | Issue Only | Normal severity issue.
104
+ | `stale` | Issues and Pull Requests | Stale - Bot reminder.
105
+ | `stale/exempt` | Issues and Pull Requests | Exempt from being marked as stale.
106
+
107
+ [1]: https://github.com/DataDog/dogapi-rb/issues
108
+ [2]: https://github.com/DataDog/dogapi-rb/issues/new
109
+ [3]: https://docs.datadoghq.com/help
110
+ [4]: https://keepachangelog.com/en/1.0.0
111
+ [5]: https://datadoghq.slack.com
@@ -0,0 +1,11 @@
1
+ # Development
2
+
3
+ The API implementation is located in the [`lib/dogapi`](lib/dogapi) folder.
4
+
5
+ ## Testing
6
+
7
+ Test files are located in the [`spec`](spec) folder.
8
+
9
+ ## Coding conventions
10
+
11
+ This project uses rubocop for linting.
data/LICENSE CHANGED
@@ -1,25 +1,26 @@
1
- Copyright (c) 2011, Datadog <info@datadoghq.com>
2
- All rights reserved.
1
+ Copyright (c) 2011-Present, Datadog <opensource@datadoghq.com>
3
2
 
4
3
  Redistribution and use in source and binary forms, with or without
5
4
  modification, are permitted provided that the following conditions are met:
6
- * Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
8
- * Redistributions in binary form must reproduce the above copyright
9
- notice, this list of conditions and the following disclaimer in the
10
- documentation and/or other materials provided with the distribution.
11
- * Neither the name of the Datadog nor the
12
- names of its contributors may be used to endorse or promote products
13
- derived from this software without specific prior written permission.
14
5
 
15
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
- DISCLAIMED. IN NO EVENT SHALL DATADOG BE LIABLE FOR ANY
19
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6
+ * Redistributions of source code must retain the above copyright notice, this
7
+ list of conditions and the following disclaimer.
25
8
 
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ * Neither the name of the copyright holder nor the names of its
14
+ contributors may be used to endorse or promote products derived from
15
+ this software without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,6 @@
1
+ Component,Origin,License,Copyright
2
+ gemfile, rubocop, MIT, Copyright (c) 2012-19 Bozhidar Batsov
3
+ gemfile, rake, MIT, Copyright (c) Jim Weirich
4
+ gemfile, rspec, MIT, Copyright © 2009 Chad Humphries David Chelimsky Copyright © 2006 David Chelimsky The RSpec Development Team Copyright © 2005 Steven Baker
5
+ gemfile, simplecov, MIT, Copyright (c) 2010-2017 Christoph Olszowka
6
+ gemfile, webmock, MIT, Copyright (c) 2009-2010 Bartosz Blimke
@@ -36,6 +36,13 @@ If you get a LoadError, missing mkmf, you need to install the development packag
36
36
 
37
37
  = Usage
38
38
 
39
+ == Supported Versions
40
+
41
+ This project currently works with Ruby versions 1.9.3+
42
+
43
+ *Note* Newer features and new endpoint support may no longer support EOL Ruby versions but
44
+ the client should still intialize and allow metric/event submission.
45
+
39
46
  == How to find your API and application keys
40
47
 
41
48
  Go to your setup page[https://app.datadoghq.com/account/settings].
@@ -0,0 +1,46 @@
1
+ # Releasing
2
+ This document summarizes the process of doing a new release of this project.
3
+ Release can only be performed by Datadog maintainers of this repository.
4
+
5
+ ## Schedule
6
+ This project does not have a strict release schedule. However, we would make a release at least every 2 months.
7
+ - No release will be done if no changes got merged to the `master` branch during the above mentioned window.
8
+ - Releases may be done more frequently than the above mentioned window.
9
+
10
+ ## Prerelease checklist
11
+ * Check and upgrade dependencies where it applies and makes sense.
12
+ - Create a distinct pull request and test your changes since it may introduce regressions.
13
+ - While using the latest versions of dependencies is advised, it may not always be possible due to potential compatibility issues.
14
+ - Upgraded dependencies should be thoroughly considered and tested to ensure they are safe!
15
+ * Make sure tests are passing.
16
+ - Locally and in the continuous integration system.
17
+ * Manually test changes included in the new release.
18
+ * Make sure documentation is up-to-date.
19
+
20
+ ## Release Process
21
+ ### Prerequisite
22
+ Install [bundler](https://bundler.io/) and setup your RubyGems credentials:
23
+ 1. Register an account on https://rubygems.org/
24
+ 1. *Datadog Admins only* - Be assigned to Datadog's RubyGems gems by an owner.
25
+ 1. Set a `~/.gem/credentials` file as the following:
26
+ ```
27
+ ---
28
+ :rubygems_api_key: $RUBYGEMS_APIKEY
29
+ ```
30
+ 1. Install [datadog_checks_dev](https://datadog-checks-base.readthedocs.io/en/latest/datadog_checks_dev.cli.html#installation) using Python 3.
31
+
32
+ ### Update Changelog
33
+ #### Commands
34
+ - See changes ready for release by running `ddev release show changes .` at the root of this project. Add any missing labels to PRs if needed.
35
+ - Run `ddev release changelog . <NEW_VERSION>` to update the `CHANGELOG.md` file at the root of this repository
36
+ - Commit the changes to the repository in a release branch. Do not merge yet.
37
+
38
+ ### Release
39
+ 1. Update the gem version number in `lib/dogapi/version.rb`, push it to your changelog PR.
40
+ 1. Merge the PR to master.
41
+ 1. Create the release in the [Github releases page](https://github.com/DataDog/dogapi-rb/releases).
42
+ 1. Checkout the tag created at the previous step.
43
+ 1. Build the gem: `bundle exec gem build dogapi.gemspec`.
44
+ 1. Push the gem: `bundle exec gem push dogapi-x.x.x.gem`.
45
+ 1. Check that the [Ruby Gem is published](https://rubygems.org/gems/dogapi).
46
+ 1. Bump the version again in `lib/dogapi/version.rb` to a dev version (e.g. `1.39.0` -> `1.40.0.dev`), open a PR and merge it to master.
@@ -0,0 +1,9 @@
1
+ # Support
2
+
3
+ The issue queue we have here on GitHub is primarily intended for tracking features,
4
+ bugs and work items associated with this Datadog open source project.
5
+
6
+ For any other support request, please reach out through one of the following:
7
+
8
+ * Contact our [support](https://docs.datadoghq.com/help/)
9
+ * Join us [on Slack](http://datadoghq.slack.com)
@@ -31,5 +31,5 @@ dog.emit_points('test.api.test_metric', points)
31
31
  # Emit differents metrics in a single request to be more efficient
32
32
  dog.batch_metrics do
33
33
  dog.emit_point('test.api.test_metric',10)
34
- dog.emit_point('test.api.this_other_metric', 1, :type => 'counter')
34
+ dog.emit_point('test.api.this_other_metric', 1, :type => 'count')
35
35
  end
@@ -18,7 +18,10 @@ To set up your Capfile:
18
18
  # for instance, only push the production event
19
19
  # set :datadog_event_filter, proc { |event, hosts| event.msg_title.include?('ran production') ? [event, hosts] : nil }
20
20
 
21
+ # (optional) use the `Etc.getlogin` method (default) or the `Etc.getpwuid` method to get the user name
22
+ # default: use `Etc.getlogin`
23
+ # set :use_getlogin, true
24
+
21
25
  You can find your Datadog API key [here](https://app.datadoghq.com/account/settings#api). If you don't have a Datadog account, you can sign up for one [here](http://www.datadoghq.com/).
22
26
 
23
27
  `capistrano/datadog` will capture each Capistrano task that that Capfile runs, including the roles that the task applies to and any logging output that it emits and submits them as events to Datadog at the end of the execution of all the tasks. If sending to Datadog fails for any reason, your scripts will still succeed.
24
-
@@ -1,3 +1,7 @@
1
+ # Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
2
+ # This product includes software developed at Datadog (https://www.datadoghq.com/).
3
+ # Copyright 2011-Present Datadog, Inc.
4
+
1
5
  require 'etc'
2
6
  require 'digest/md5'
3
7
  require 'timeout'
@@ -24,11 +28,11 @@ module Capistrano
24
28
  @cap_version
25
29
  end
26
30
 
27
- def self.submit(api_key)
31
+ def self.submit(api_key, use_getlogin=true)
28
32
  begin
29
33
  if api_key
30
34
  dog = Dogapi::Client.new(api_key)
31
- reporter.report.each do |event, hosts|
35
+ reporter.report(use_getlogin).each do |event, hosts|
32
36
  if hosts.size > 0
33
37
  hosts.each do |host|
34
38
  dog.emit_event event, host: host
@@ -89,9 +93,9 @@ module Capistrano
89
93
  end
90
94
  end
91
95
 
92
- def report()
96
+ def report(use_getlogin=true)
93
97
  hostname = Dogapi.find_localhost
94
- user = Etc.getlogin
98
+ user = use_getlogin ? Etc.getlogin : Etc.getpwuid.name
95
99
 
96
100
  # Lazy randomness
97
101
  aggregation_key = Digest::MD5.hexdigest "#{Time.new}|#{rand}"
@@ -1,3 +1,7 @@
1
+ # Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
2
+ # This product includes software developed at Datadog (https://www.datadoghq.com/).
3
+ # Copyright 2011-Present Datadog, Inc.
4
+
1
5
  require 'benchmark'
2
6
  require 'delegate'
3
7
 
@@ -58,7 +62,11 @@ module Capistrano
58
62
  namespace :datadog do
59
63
  desc 'Submit the tasks that have run to Datadog as events'
60
64
  task :submit do |ns|
61
- Capistrano::Datadog.submit variables[:datadog_api_key]
65
+ if variables[:use_getlogin].nil?
66
+ Capistrano::Datadog.submit variables[:datadog_api_key]
67
+ else
68
+ Capistrano::Datadog.submit variables[:datadog_api_key], variables[:use_getlogin]
69
+ end
62
70
  end
63
71
  end
64
72
  end
@@ -1,3 +1,7 @@
1
+ # Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
2
+ # This product includes software developed at Datadog (https://www.datadoghq.com/).
3
+ # Copyright 2011-Present Datadog, Inc.
4
+
1
5
  require 'benchmark'
2
6
  require 'delegate'
3
7
  require 'sshkit/formatters/pretty'
@@ -76,5 +80,10 @@ end
76
80
 
77
81
  at_exit do
78
82
  api_key = Capistrano::Configuration.env.fetch :datadog_api_key
79
- Capistrano::Datadog.submit api_key
83
+ use_getlogin = Capistrano::Configuration.env.fetch :use_getlogin
84
+ if use_getlogin.nil?
85
+ Capistrano::Datadog.submit api_key
86
+ else
87
+ Capistrano::Datadog.submit api_key, use_getlogin
88
+ end
80
89
  end
@@ -1,3 +1,7 @@
1
+ # Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
2
+ # This product includes software developed at Datadog (https://www.datadoghq.com/).
3
+ # Copyright 2011-Present Datadog, Inc.
4
+
1
5
  require 'dogapi/common'
2
6
  require 'dogapi/facade'
3
7
  require 'dogapi/event'
@@ -1,3 +1,7 @@
1
+ # Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
2
+ # This product includes software developed at Datadog (https://www.datadoghq.com/).
3
+ # Copyright 2011-Present Datadog, Inc.
4
+
1
5
  require 'cgi'
2
6
  require 'net/https'
3
7
  require 'socket'
@@ -7,6 +11,7 @@ require 'English'
7
11
  require 'rubygems'
8
12
  require 'multi_json'
9
13
  require 'set'
14
+ require 'open3'
10
15
 
11
16
  module Dogapi
12
17
 
@@ -83,13 +88,12 @@ module Dogapi
83
88
  def connect
84
89
  connection = Net::HTTP
85
90
 
86
- # After ruby 2.0 Net::HTTP looks for the env variable but not ruby 1.9
87
- if RUBY_VERSION < '2.0.0'
88
- proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
89
- if proxy
90
- proxy_uri = URI.parse(proxy)
91
- connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
92
- end
91
+ # Expose using a proxy without setting the HTTPS_PROXY or HTTP_PROXY variables
92
+ proxy = Dogapi.find_proxy()
93
+
94
+ if proxy
95
+ proxy_uri = URI.parse(proxy)
96
+ connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
93
97
  end
94
98
 
95
99
  uri = URI.parse(@api_host)
@@ -121,6 +125,9 @@ module Dogapi
121
125
  params = prepare_params(extra_params, url, with_app_key)
122
126
  req = prepare_request(method, url, params, body, send_json, with_app_key)
123
127
  resp = conn.request(req)
128
+ if resp.code.to_i / 100 == 3
129
+ resp = handle_redirect(conn, req, resp)
130
+ end
124
131
  return handle_response(resp)
125
132
  rescue Exception => e
126
133
  suppress_error_if_silent e
@@ -173,6 +180,15 @@ module Dogapi
173
180
  raise 'Invalid JSON Response: ' + resp.body
174
181
  end
175
182
  end
183
+
184
+ def handle_redirect(conn, req, resp, retries=10)
185
+ req.uri = URI.parse(resp.header['location'])
186
+ new_response = conn.request(req)
187
+ if retries > 1 && new_response.code / 100 == 3
188
+ new_response = handle_redirect(conn, req, new_response, retries - 1)
189
+ end
190
+ new_response
191
+ end
176
192
  end
177
193
 
178
194
  def Dogapi.find_datadog_host
@@ -184,9 +200,28 @@ module Dogapi
184
200
  @@hostname = nil
185
201
 
186
202
  def Dogapi.find_localhost
187
- @@hostname ||= %x[hostname -f].strip
203
+ unless @@hostname
204
+ out, status = Open3.capture2('hostname', '-f', err: File::NULL)
205
+ @@hostname = out.strip
206
+ # Get status to check if the call was successful
207
+ raise SystemCallError, 'Could not get hostname with `hostname -f`' unless status.exitstatus.zero?
208
+ end
188
209
  rescue SystemCallError
189
- raise $ERROR_INFO unless $ERROR_INFO.class.name == 'Errno::ENOENT'
190
210
  @@hostname = Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
191
211
  end
212
+
213
+ def Dogapi.find_proxy
214
+ ENV['DD_PROXY_HTTPS'] || ENV['dd_proxy_https'] ||
215
+ ENV['DD_PROXY_HTTP'] || ENV['dd_proxy_http'] ||
216
+ ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
217
+ end
218
+
219
+ def Dogapi.validate_tags(tags)
220
+ unless tags.is_a? Array
221
+ raise ArgumentError, "The tags parameter needs to be an array of string. Current value: #{tags}"
222
+ end
223
+ tags.each do |tag|
224
+ raise ArgumentError, "Each tag needs to be a string. Current value: #{tag}" unless tag.is_a? String
225
+ end
226
+ end
192
227
  end