Skip to content

Commit

Permalink
Code for step 8
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jan 22, 2020
1 parent ddda199 commit 2e2854d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/elixir_monitoring_prom/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ defmodule ElixirMonitoringProm.Application do
nil
)

ElixirMonitoringProm.Metrics.setup()

# List all child processes to be supervised
children = [
# Start the Ecto repository
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir_monitoring_prom/zip_codes/zip_codes.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule ElixirMonitoringProm.ZipCodes do
alias ElixirMonitoringProm.{Repo, ZipCodes.ZipCode}

import Ecto.Query

def get_zip_codes_in_radius(zip_code, radius_in_miles) do
# Our raw Postgres query to get all the zip codes within a radius
query =
Expand Down Expand Up @@ -30,5 +32,9 @@ defmodule ElixirMonitoringProm.ZipCodes do
end
end

def get_zip_code_info(zip) do
Repo.one(from zip_code in ZipCode, where: zip_code.zip_code == ^zip)
end

defp miles_to_meters(miles), do: miles * 1609.344
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
defmodule ElixirMonitoringPromWeb.BreweryController do
use ElixirMonitoringPromWeb, :controller

alias ElixirMonitoringProm.Breweries
alias ElixirMonitoringProm.{Breweries, Metrics}

def index(conn, %{"zip_code" => zip_code, "mile_radius" => radius}) do
results = Breweries.get_breweries_in_radius(zip_code, String.to_integer(radius))
Metrics.increment_zip_code_search(zip_code)
Metrics.observe_radius_search(radius)

json(conn, results)
end
Expand Down
39 changes: 39 additions & 0 deletions lib/metrics.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule ElixirMonitoringProm.Metrics do
use Prometheus.Metric

alias ElixirMonitoringProm.{ZipCode, ZipCodes.ZipCode}

def setup do
# Our counter for the number of searches against a particular zipcode.
# Given that there is a label 'geohash' each zip code gets its own counter.
Counter.new(
name: :elixir_app_zip_code_search,
help: "Counter for brewery zip code searches",
labels: [:zip_code, :geohash]
)

# The provided radius will fall into one of the defined buckets
Histogram.new(
name: :elixir_app_radius_search,
buckets: [1, 2, 5, 10, 20, 50],
help: "The radius that people are searching within for breweries"
)
end

def increment_zip_code_search(zip_code) do
zip_code
|> ElixirMonitoringProm.ZipCodes.get_zip_code_info()
|> case do
%ZipCode{point: %Geo.Point{coordinates: {long, lat}}} ->
geo_hash = Geohash.encode(lat, long)
Counter.inc(name: :elixir_app_zip_code_search, labels: [zip_code, geo_hash])

_ ->
:noop
end
end

def observe_radius_search(radius) do
Histogram.observe([name: :elixir_app_radius_search], String.to_integer(radius))
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ defmodule ElixirMonitoringProm.MixProject do
{:prometheus_ecto, "~> 1.4.3"},
{:prometheus_phoenix, "~> 1.3.0"},
{:prometheus_plugs, "~> 1.1.5"},
{:prometheus_process_collector, "~> 1.4.3"}
{:prometheus_process_collector, "~> 1.4.3"},
{:geohash, "~> 1.0.0"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"},
"geo": {:hex, :geo, "3.1.0", "727e005262430d037e870ff364e65d80ca5ca21d5ac8eddd57a1ada72c3f83b0", [:mix], [], "hexpm"},
"geo_postgis": {:hex, :geo_postgis, "3.1.0", "d06c8fa5fd140a52a5c9dab4ad6623a696dd7d99dd791bb361d3f94942442ff9", [:mix], [{:geo, "~> 3.1", [hex: :geo, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0 or ~> 4.0", [hex: :poison, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm"},
"geohash": {:hex, :geohash, "1.0.2", "d02f8c974704105d3c6cc7e722647319aa601c86169693710866a550277dc41e", [:mix], [], "hexpm"},
"gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
Expand Down

0 comments on commit 2e2854d

Please sign in to comment.