Skip to content

Commit

Permalink
Rework "Document" -> "Capture" and update tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 3, 2023
1 parent 310d224 commit 156c1a9
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
ruby-version: "3.2"
bundler-cache: true

- name: Installing packages
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-external.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- "2.7"
- "3.0"
- "3.1"
- "3.2"

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- "2.7"
- "3.0"
- "3.1"
- "3.2"

experimental: [false]

Expand Down
11 changes: 0 additions & 11 deletions bake/metrics.rb

This file was deleted.

14 changes: 14 additions & 0 deletions bake/metrics/capture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

def capture
ENV['METRICS_BACKEND'] = 'metrics/backend/capture'
require 'metrics'
end

# Generate a list of metrics using the document backend.
def list
Metrics::Backend::Capture.metrics.sort_by!{|metric| metric.name}
end
8 changes: 4 additions & 4 deletions guides/document/readme.md → guides/capture/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

This guide explains how to use `metrics` for exporting metric definitions from your application.

## Extraction
## Capture Metrics

If your application defines one or more metrics, you can export them using the `bake metrics:document` command. This command will generate a list of metrics which you can export.

```bash
$ bake metrics:document output --format json
$ bake metrics:capture environment metrics:capture:list output --format json
[
{
"name": "my_metric",
Expand All @@ -27,12 +27,12 @@ $ bake metrics:document output --format json
]
```

## Capture
## Capture Metrics with Test Data

If your application has a test suite which emits metrics, you can capture those as samples for the purpose of your documentation. This includes fields like tags.

```bash
$ bake metrics:document capture --environment test --format json
$ bake metrics:capture test metrics:capture:list output --format json
[
{
"name": "my_metric",
Expand Down
2 changes: 1 addition & 1 deletion guides/links.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
getting-started:
order: 1
document:
capture:
order: 2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

module Metrics
module Backend
module Document
module Capture
class Metric < Metrics::Metric
def initialize(...)
super
Expand Down Expand Up @@ -45,23 +45,21 @@ def to_json(...)
end
end

class << self
def metrics
@metrics ||= []
end
def self.metrics
@metrics ||= []
end

module Interface
def metric(name, type, description: nil, unit: nil, &block)
metric = Metric.new(name, type, description, unit)

Document.metrics << metric
Capture.metrics << metric

return metric
end
end
end

Interface = Document::Interface
Interface = Capture::Interface
end
end
2 changes: 1 addition & 1 deletion lib/metrics/backend/console.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2021-2022, by Samuel Williams.
# Copyright, 2021-2023, by Samuel Williams.

require 'console'
require_relative '../metric'
Expand Down
2 changes: 1 addition & 1 deletion license.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright, 2021-2022, by Samuel Williams.
Copyright, 2021-2023, by Samuel Williams.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions test/metrics/backend/capture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

require 'metrics/provider'

describe Metrics::Provider do
let(:document_root) {File.expand_path(".capture", __dir__)}
let(:environment) {{'METRICS_BACKEND' => nil}}

it "runs without metrics" do
pid = Process.spawn(environment, "bundle", "exec", "bake", "run", chdir: document_root)
pid, status = Process.wait2(pid)

expect(status).to be(:success?)
end

it "can list all metrics" do
input, output = IO.pipe

pid = Process.spawn(environment, "bundle", "exec", "bake", "metrics:capture", "environment", "metrics:capture:list", "output", "--format", "json", chdir: document_root, out: output)
output.close
Process.wait(pid)

metrics = JSON.parse(input.read)

expect(metrics).to be_a(Array)
metric = metrics[0]

expect(metric).to have_keys(
"name" => be == "my_metric",
"type" => be == "gauge",
"description" => be == "My metric",
"unit" => be == "seconds",
"values" => be == [],
"tags" => be == [],
"sample_rates" => be == []
)
end

it "can collect metrics" do
input, output = IO.pipe

pid = Process.spawn(environment, "bundle", "exec", "bake", "metrics:capture", "run", "metrics:capture:list", "output", "--format", "json", chdir: document_root, out: output)
output.close
Process.wait(pid)

metrics = JSON.parse(input.read)

expect(metrics).to be_a(Array)
metric = metrics[0]

expect(metric).to have_keys(
"name" => be == "my_metric",
"type" => be == "gauge",
"description" => be == "My metric",
"unit" => be == "seconds",
"values" => be == [1],
"tags" => be == ['environment:test'],
"sample_rates" => be == [1.0]
)
end
end
71 changes: 0 additions & 71 deletions test/metrics/backend/document.rb

This file was deleted.

0 comments on commit 156c1a9

Please sign in to comment.