stackdriver_simple 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README.md +53 -0
  4. data/lib/stackdriver_simple.rb +41 -0
  5. metadata +76 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e16354a6269722025c5124a621cfe909f8a8d1f424302bf01dcaf1c0f6d4615
4
+ data.tar.gz: 633eef3b4d3dd7c99300f26efde767af34e2cc9192da431731bfa1e1478f8583
5
+ SHA512:
6
+ metadata.gz: 79a2a2e7739a3d2d14b01057e83ee3125d04d5c550aef4cd3bd59f90eff9e73db8596e8406e566fea048d524950fde3863f31756539a1ce6c7432f7b9057d767
7
+ data.tar.gz: 320bdeef1596a9c9981526bceae32127d014c0a2ddafdec456ec50c0b1643738a6270cf808dc5c59c03d7674f2edfbcf452111bd21fca59d9047fb4e4b0890ec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Peter Jones
2
+ Copyright (c) 2009 James Healy
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # stackdriver_simple
2
+
3
+ It's surprisingly hard to submit metrics directly to stackdriver using ruby.
4
+
5
+ The [official docs](https://cloud.google.com/monitoring/custom-metrics/open-census)
6
+ recommend using [opencensus](https://opencensus.io/), but the stackdriver
7
+ backend for the ruby opencensus implementation is incomplete.
8
+
9
+ I eventually worked out the right voodoo, but it's not the kind of code you want
10
+ to re-implement in multiple projects.
11
+
12
+ ## Installation
13
+
14
+ Manually:
15
+
16
+ gem install stackdriver_simple
17
+
18
+ or, in your Gemfile:
19
+
20
+ gem "stackdriver_simple"
21
+
22
+ ## Usage
23
+
24
+ ### Authentication
25
+
26
+ This is just a wrapper around an official google gem that knows the well-known
27
+ places to look for valid credentials.
28
+
29
+ One option is to have credentials in a JSON file. If you have the `gcloud` SDK
30
+ installed, the following command will save some credentials to disk in a place
31
+ where the gem will find them:
32
+
33
+ gcloud auth application-default login
34
+
35
+ If you have a JSON file with gcloud credentials in another location, set the GOOGLE_APPLICATION_CREDENTIALS environment variable like this:
36
+
37
+ GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json ruby my-script.rb
38
+
39
+ Alternatively, if you're running a program on Google Compute Engine the
40
+ instance metadata will contain valid credentials too.
41
+
42
+ ### Gauges
43
+
44
+ Submit a gauge like this:
45
+
46
+ require 'stackdriver_simple'
47
+
48
+ StackdriverSimple.new(google_cloud_project: "my-project-id").submit_gauge("gauge_name", 123)
49
+
50
+ ### Other metric types
51
+
52
+ There are other metrics types that could be submitted, I just haven't
53
+ implemented them yet. Pull requests are welcome!
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ require "google/cloud/monitoring"
4
+
5
+ class StackdriverSimple
6
+
7
+ def initialize(google_cloud_project:)
8
+ @google_cloud_project = google_cloud_project.to_s
9
+
10
+ raise ArgumentError if @google_cloud_project.include?(" ")
11
+ end
12
+
13
+ # This assumes the following two ENV vars are set:
14
+ #
15
+ # * GOOGLE_APPLICATION_CREDENTIALS=<path-to-credentials>
16
+ def submit_gauge(name, value)
17
+ raise ArgumentError, "name cannot include spaces" if name.to_s.include?(" ")
18
+ raise ArgumentError, "name cannot include /" if name.to_s.include?("/")
19
+ raise ArgumentError, "value cannot be nil" if value.nil?
20
+
21
+ # This might have optional arguments for providing the project and credentials?
22
+ client = Google::Cloud::Monitoring::Metric.new
23
+ project_name = Google::Cloud::Monitoring::V3::MetricServiceClient.project_path(@google_cloud_project)
24
+
25
+ series = Google::Monitoring::V3::TimeSeries.new
26
+ metric = Google::Api::Metric.new type: "custom.googleapis.com/#{name}"
27
+ series.metric = metric
28
+
29
+ resource = Google::Api::MonitoredResource.new type: "global"
30
+ series.resource = resource
31
+
32
+ point = Google::Monitoring::V3::Point.new
33
+ point.value = Google::Monitoring::V3::TypedValue.new double_value: value
34
+ now = Time.now
35
+ end_time = Google::Protobuf::Timestamp.new seconds: now.to_i, nanos: now.usec
36
+ point.interval = Google::Monitoring::V3::TimeInterval.new end_time: end_time
37
+ series.points << point
38
+
39
+ client.create_time_series project_name, [series]
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stackdriver_simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-cloud-monitoring
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: An easy way to submit metrics to stackdriver
42
+ email:
43
+ - james@yob.id.au
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - README.md
48
+ - MIT-LICENSE
49
+ files:
50
+ - MIT-LICENSE
51
+ - README.md
52
+ - lib/stackdriver_simple.rb
53
+ homepage: http://github.com/yob/stackdriver_simpler
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.9.3
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.0.1
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: An easy way to submit metrics to stackdriver
76
+ test_files: []