Skip to content

Commit

Permalink
Rename values to fields
Browse files Browse the repository at this point in the history
Because this is how they're called in InfluxDB.
  • Loading branch information
ChrisBr authored and hennevogel committed Jul 18, 2023
1 parent 8e70e7a commit 5d2b9bd
Show file tree
Hide file tree
Showing 25 changed files with 63 additions and 57 deletions.
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Automatically instrument your Ruby on Rails applications and write the metrics d
Add the gem to your `Gemfile`:

```console
echo 'gem "influxdb-rails"' >>Gemfile
echo 'gem "influxdb-rails"' >> Gemfile
bundle install
```

Expand All @@ -50,7 +50,7 @@ Reported ActiveSupport instrumentation hooks:
- [start\_processing.action\_controller](https://guides.rubyonrails.org/active_support_instrumentation.html#start-processing-action-controller)
- [process\_action.action\_controller](https://guides.rubyonrails.org/active_support_instrumentation.html#process-action-action-controller)

Reported values:
Reported fields:

```ruby
controller: 48.467,
Expand Down Expand Up @@ -83,7 +83,7 @@ Reported ActiveSupport instrumentation hooks:
- [render\_partial.action\_view](https://guides.rubyonrails.org/active_support_instrumentation.html#render-partial-action-view)
- [render\_collection.action\_view](https://guides.rubyonrails.org/active_support_instrumentation.html#render-collection-action-view)

Reported values:
Reported fields:

```ruby
value: 48.467,
Expand All @@ -109,7 +109,7 @@ Reported ActiveSupport instrumentation hooks:
- [sql.active\_record](https://guides.rubyonrails.org/active_support_instrumentation.html#sql-active-record)
- [instantiation.active\_record](https://guides.rubyonrails.org/active_support_instrumentation.html#instantiation-active-record)

Reported SQL values:
Reported fields:

```ruby
sql: "SELECT \"posts\".* FROM \"posts\"",
Expand Down Expand Up @@ -153,7 +153,7 @@ Reported ActiveSupport instrumentation hooks:
- [enqueue.active\_job](https://guides.rubyonrails.org/active_support_instrumentation.html#enqueue-active-job)
- [perform.active\_job](https://guides.rubyonrails.org/active_support_instrumentation.html#perform-active-job)

Reported values:
Reported fields:

```ruby
value: 89.467
Expand All @@ -177,7 +177,7 @@ Reported ActiveSupport instrumentation hooks:

- [deliver.action\_mailer](https://guides.rubyonrails.org/active_support_instrumentation.html#deliver-action-mailer)

Reported values:
Reported fields:

```ruby
value: 1
Expand All @@ -194,12 +194,13 @@ Reported tags:

## Configuration

The only setting you actually need to configure is the name of the database
within the InfluxDB server instance (don't forget to create this database!).
The only settings you actually need to configure are the organization, bucket and access token.

```ruby
InfluxDB::Rails.configure do |config|
config.client.database = "rails"
config.client.org = "org"
config.client.bucket = "bucket"
config.client.token = "token"
end
```

Expand Down Expand Up @@ -234,7 +235,7 @@ class ApplicationController

def set_influx_data
InfluxDB::Rails.current.tags = { user: current_user.id }
InfluxDB::Rails.current.values = { redis_value: redis_value }
InfluxDB::Rails.current.fields = { redis_value: redis_value }
end
end
```
Expand All @@ -243,7 +244,7 @@ end
If you want to add custom instrumentation, you can wrap any code into a block instrumentation

```ruby
InfluxDB::Rails.instrument "expensive_operation", tags: { }, values: { } do
InfluxDB::Rails.instrument "expensive_operation", tags: { }, fields: { } do
expensive_operation
end
```
Expand All @@ -258,23 +259,23 @@ Reported tags:
name: "expensive_operation"
```

Reported values:
Reported fields:
```ruby
value: 100 # execution time of the block in ms
```

You can also overwrite the `value`

```ruby
InfluxDB::Rails.instrument "user_count", values: { value: 1 } do
InfluxDB::Rails.instrument "user_count", fields: { value: 1 } do
User.create(name: 'mickey', surname: 'mouse')
end
```

or call it even without a block

```ruby
InfluxDB::Rails.instrument "temperature", values: { value: 25 }
InfluxDB::Rails.instrument "temperature", fields: { value: 25 }
```

### Custom client configuration
Expand All @@ -285,15 +286,17 @@ to your InfluxDB server. You can access this client as well, and perform
arbitrary operations on your data:

```ruby
InfluxDB::Rails.client.write_point "events",
InfluxDB::Rails.write_api.write(
name: "events",
tags: { url: "/foo", user_id: current_user.id, location: InfluxDB::Rails.current.location },
values: { value: 0 }
fields: { value: 0 }
)
```

If you do that, it might be useful to add the current context to these custom
data points which can get accessed with `InfluxDB::Rails.current.location`.

See [influxdb-ruby](http://github.com/influxdata/influxdb-ruby) for a
See [influxdb-client](https://github.com/influxdata/influxdb-client-ruby) for a
full list of configuration options and detailed usage.

### Disabling hooks
Expand Down
10 changes: 5 additions & 5 deletions lib/influxdb/rails/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Rails
class Context
def reset
Thread.current[:_influxdb_rails_tags] = {}
Thread.current[:_influxdb_rails_values] = {}
Thread.current[:_influxdb_rails_fields] = {}
end

def tags
Expand All @@ -14,12 +14,12 @@ def tags=(tags)
Thread.current[:_influxdb_rails_tags] = self.tags.merge(tags)
end

def values
Thread.current[:_influxdb_rails_values].to_h
def fields
Thread.current[:_influxdb_rails_fields].to_h
end

def values=(values)
Thread.current[:_influxdb_rails_values] = self.values.merge(values)
def fields=(fields)
Thread.current[:_influxdb_rails_fields] = self.fields.merge(fields)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/influxdb/rails/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module InfluxDB
module Rails
class Metric
def initialize(configuration:, timestamp:, tags: {}, values: {})
def initialize(configuration:, timestamp:, tags: {}, fields: {})
@configuration = configuration
@timestamp = timestamp
@tags = tags
@values = values
@fields = fields
end

def write
Expand All @@ -16,11 +16,11 @@ def write

private

attr_reader :configuration, :tags, :values, :timestamp
attr_reader :configuration, :tags, :fields, :timestamp

def data
{
fields: values.merge(InfluxDB::Rails.current.values),
fields: fields.merge(InfluxDB::Rails.current.fields),
tags: Tags.new(tags: tags, config: configuration).to_h,
name: configuration.measurement_name,
time: timestamp.utc,
Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/middleware/action_mailer_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Middleware
class ActionMailerSubscriber < Subscriber # :nodoc:
private

def values
def fields
{ value: 1 }
end

Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/middleware/active_job_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ActiveJobSubscriber < Subscriber # :nodoc:
}.freeze
private_constant :JOB_STATE

def values
def fields
{
value: value,
}
Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/middleware/active_record_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Middleware
class ActiveRecordSubscriber < Subscriber # :nodoc:
private

def values
def fields
{
value: duration,
record_count: payload[:record_count],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module Middleware
class BlockInstrumentationSubscriber < Subscriber
private

def values
def fields
{
value: duration,
}.merge(payload[:values].to_h)
}.merge(payload[:fields].to_h)
end

def tags
Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/middleware/render_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Middleware
class RenderSubscriber < Subscriber # :nodoc:
private

def values
def fields
{
value: duration,
count: payload[:count],
Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/middleware/request_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def tags
}
end

def values
def fields
{
controller: duration,
view: (payload[:view_runtime] || 0).ceil,
Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/middleware/sql_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Middleware
class SqlSubscriber < Subscriber # :nodoc:
private

def values
def fields
{
value: duration,
sql: InfluxDB::Rails::Sql::Normalizer.new(payload[:sql]).perform,
Expand Down
4 changes: 2 additions & 2 deletions lib/influxdb/rails/middleware/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def write

def metric
InfluxDB::Rails::Metric.new(
values: values,
fields: fields,
tags: tags,
configuration: configuration,
timestamp: finish
Expand All @@ -50,7 +50,7 @@ def tags
raise NotImplementedError, "must be implemented in subclass"
end

def values
def fields
raise NotImplementedError, "must be implemented in subclass"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Railtie < ::Rails::Railtie # :nodoc:
ActiveSupport.on_load(:action_controller) do
before_action do
current = InfluxDB::Rails.current
current.values = { request_id: request.request_id } if request.respond_to?(:request_id)
current.fields = { request_id: request.request_id } if request.respond_to?(:request_id)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/influxdb_rails_sends_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
body: "rails,hook=block_instrumentation,location=raw,name=name,server=hostname value=1i 1514797200000000000"
)

InfluxDB::Rails.instrument "name", values: { value: 1 }
InfluxDB::Rails.instrument "name", fields: { value: 1 }

assert_requested(request, times: 1)
end
Expand Down
9 changes: 6 additions & 3 deletions spec/requests/action_controller_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
format: :html,
http_method: "GET"
),
values: a_hash_including(
fields: a_hash_including(
view: be_between(1, 500),
db: be_between(1, 500),
controller: be_between(1, 500)
Expand All @@ -46,8 +46,11 @@
tags_middleware: :tags_middleware
),
fields: a_hash_including(
additional_value: :value,
request_id: :request_id
additional_field: :value,
request_id: :request_id,
view: be_between(1, 500),
db: be_between(1, 500),
controller: be_between(1, 500)
)
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/action_mailer_deliver_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
mailer: "MetricMailer"
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: 1
)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/action_view_collection_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
filename: include("spec/support/views/metrics/_item.html.erb")
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
count: 3,
request_id: :request_id,
value: be_between(1, 500)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/action_view_partial_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
filename: include("spec/support/views/metrics/_item.html.erb")
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: be_between(1, 500)
)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/action_view_template_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
filename: include("spec/support/views/metrics/index.html.erb")
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: be_between(1, 500)
)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/active_job_enqueue_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
state: "queued"
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: 1
)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/active_record_instantiation_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class_name: "Metric"
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: be_between(1, 500),
record_count: 1
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/active_record_sql_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
operation: "INSERT"
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: be_between(1, 500),
sql: "INSERT INTO \"metrics\" (\"name\", \"created_at\", \"updated_at\") VALUES (xxx)"
Expand All @@ -47,7 +47,7 @@
operation: "INSERT"
),
fields: a_hash_including(
additional_value: :value,
additional_field: :value,
request_id: :request_id,
value: be_between(1, 500),
sql: "INSERT INTO \"metrics\" (\"name\", \"created_at\", \"updated_at\") VALUES (xxx)"
Expand Down
Loading

0 comments on commit 5d2b9bd

Please sign in to comment.