Last active
April 12, 2020 12:49
-
-
Save nrr/d068b057205b56a23f723cebc0430c24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "benchmark" | |
require "prometheus/client" | |
# XXX: I've actually not run this code, and my understanding of the Prometheus | |
# API for Ruby is from a quick perusal of how things are implemented, so this | |
# _might_ not actually work. It's meant to be more illustrative than anything, | |
# so if it's wrong, feel free to walk away with the glee that you understand | |
# what you're doing. | |
class PrometheusInstrumentedPostsRepository | |
include IPostsRepository | |
# @param [IPostsRepository] repository | |
# @param [::Prometheus::Client::Counter] request_counter | |
# @param [::Prometheus::Client::Histogram] request_histogram | |
def initialize(repository, request_counter, request_histogram) | |
@repository = repository | |
@request_counter = request_counter | |
@request_histogram = request_histogram | |
end | |
# @return [Array<::Domain::Post>] | |
def all | |
all_posts = [] | |
@request_histogram.observe( | |
Benchmark.realtime do | |
all_posts = @repository.all | |
end, | |
labels: [:all_posts] | |
) | |
all_posts | |
ensure | |
@request_counter.increment(labels: [:all_posts]) | |
end | |
# @return [Array<::Domain::Post>] | |
def all_published | |
all_published = [] | |
@request_histogram.observe( | |
Benchmark.realtime do | |
all_published ||= @repository.all_published | |
end, | |
labels: [:all_published] | |
) | |
all_published | |
ensure | |
@request_counter.increment(labels: [:all_published_posts]) | |
end | |
# @param [Integer] post_id | |
# @return [::Domain::Post] | |
def by_id(post_id) | |
by_id = ::Domain::Post.new | |
@request_histogram.observe( | |
Benchmark.realtime do | |
by_id ||= @repository.by_id(post_id) | |
end, | |
labels: [:by_id] | |
) | |
by_id | |
ensure | |
@request_counter.increment(labels: [:by_id]) | |
end | |
# @param [Array<::Domain::Post>] posts | |
# @return [Boolean] | |
# @raise [PostsRepositoryError] | |
def store(*posts) | |
result = false | |
@request_histogram.observe( | |
Benchmark.realtime do | |
result ||= @repository.store(*posts) | |
end, | |
labels: [:store] | |
) | |
result | |
ensure | |
@request_counter.increment(labels: [:store]) | |
end | |
end | |
# @return [Integer] | |
def main | |
# ... | |
posts_repo_req_count = ::Prometheus::Client::Counter.new( | |
:posts_repository_requests_total, | |
docstring: "...", | |
labels: [:repository], | |
) | |
posts_repo_latency = ::Prometheus::Client::Histogram.new( | |
:posts_repository_latency_seconds, | |
docstring: "...", | |
labels: [:repository] | |
) | |
repository = ActiveRecordPostsRepository.new(::Models::Post) | |
repository ||= PrometheusInstrumentedPostsRepository.new( | |
repository, | |
posts_repo_req_count, | |
posts_repo_latency, | |
) | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment