-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add HTTP telemetry events for Cog startup
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Cog.Telemetry do | ||
require Logger | ||
|
||
@telemetry_url "asdf;a8sw3rjasdfkl;asdf" | ||
|
||
def send_event(type) do | ||
if telemetry_enabled do | ||
spawn fn -> | ||
case build_event(type) do | ||
{:ok, event_body} -> | ||
post_telemetry(type, event_body) | ||
end | ||
end | ||
end | ||
end | ||
|
||
defp build_event(:start) do | ||
bundle_id = | ||
"operable" | ||
|> Cog.Queries.Bundles.bundle_id_from_name | ||
|> Cog.Repo.one | ||
|
||
case bundle_id do | ||
nil -> | ||
{:error, "Cannot find ID for embedded bundle."} | ||
bundle_id -> | ||
telemetry_id = Base.encode16(:crypto.hash(:sha256, bundle_id)) | ||
{:ok, version} = :application.get_key(:cog, :vsn) | ||
{:ok, %{ cog: %{ id: telemetry_id, version: to_string(version) }}} | ||
end | ||
end | ||
|
||
defp telemetry_enabled do | ||
Application.fetch_env!(:cog, :telemetry) | ||
end | ||
|
||
defp post_telemetry(event_type, event_body) do | ||
post_url = @telemetry_url <> "/events/" <> Atom.to_string(event_type) | ||
post_body = Poison.encode!(event_body) | ||
|
||
Logger.info "Sending telemetry data to Operable: #{post_body}" | ||
Logger.info "To disable telemetry, set the COG_TELEMETRY environment variable to false." | ||
|
||
HTTPotion.post(post_url, headers: ["Content-Type": "application/json", | ||
"Accepts": "application/json"], | ||
body: post_body) | ||
end | ||
end |