Skip to content

Instantly share code, notes, and snippets.

View AndrewDryga's full-sized avatar

Andrew Dryga AndrewDryga

View GitHub Profile
@AndrewDryga
AndrewDryga / erl-observe.sh
Last active July 11, 2023 01:20
Script for connecting Erlang Observer to remote node hosted in Kubernetes
#!/bin/bash
# This script provides easy way to debug remote Erlang nodes that is running in a kubernetes cluster.
# Usage: ./erl-observe.sh -l app=my_all -n default -c erlang_cookie
#
# Don't forget to include `:runtime_tools` in your mix.exs application dependencies.
set -e
# Trap exit so we can try to kill proxies that has stuck in background
function cleanup {
echo " - Stopping kubectl proxy."
defmodule OpenIDConnect do
@moduledoc """
Handles a majority of the life-cycle concerns with [OpenID Connect](http://openid.net/connect/)
"""
alias OpenIDConnect.Document
@typedoc """
URL to a [OpenID Discovery Document](https://openid.net/specs/openid-connect-discovery-1_0.html) endpoint.
"""
@type discovery_document_uri :: String.t()
defmodule Firezone.OpenAPIDocsWriter do
@keep_req_headers []
@keep_resp_headers ["content-type", "location"]
def write(conns, path) do
file = File.open!(path, [:write, :utf8])
open_api_spec = %{
openapi: "3.0.0",
info: %{
@AndrewDryga
AndrewDryga / config.ex
Last active December 12, 2022 16:10
Overriding Elixir application configuration in test env by using process dictionary
defmodule MyApp.Config do
@moduledoc """
This module provides set of helper functions that are useful when reading application runtime configuration overrides
in test environment.
"""
if Mix.env() != :test do
def maybe_put_env_override(_key, _value), do: :ok
def fetch_env!(app, key), do: Application.fetch_env!(app, key)
else
@AndrewDryga
AndrewDryga / dynamic_changeset.ex
Last active November 6, 2022 11:20
Dynamic embeds with Ecto
defmodule DynamicChangeset do
@moduledoc """
This module provides helper functions to extend `Ecto.Changeset` to support
dynamic embeds.
"""
alias Ecto.Changeset
@doc """
Casts the given embed with the embedded changeset and field which is used to define it's type.
@AndrewDryga
AndrewDryga / rules.txt
Created April 9, 2018 13:45
Grok parser for Elixir logs
## You can use this set or rules to parse Elixir logs in DataDog Logger
# 16:01:37.511 request_id=2khj5fsrc3lpk86dh8000g5h [debug] Processing with TwilioProxy.RequestController.create
router %{date("HH:mm:ss.SSS"):date} (%{data::keyvalue("=", " ")} )?\[+%{word:level}\] Processing (with|by) +%{data:controller.callback}
# 16:04:07.995 request_id=u8m11nptsmsjc282k3l1133u6evmljer [info] Sent 200 in 16ms
# 16:04:07.995 request_id=u8m11nptsmsjc282k3l1133u6evmljer [info] Sent 200 in 16µs
# 00:05:30.048 request_id=sXax4ynq8i+zU90Emg84 [info] Sent 400 in 1ms
@AndrewDryga
AndrewDryga / mix_test_watch.sh
Last active November 4, 2020 17:27
Watch for file changes and run tests for stale modules
#!/bin/bash
command -v fswatch >/dev/null 2>&1 || { echo >&2 "fswatch is not installed. To install it use 'brew install fswatch'. Aborting."; exit 1; }
command -v mix >/dev/null 2>&1 || { echo >&2 "mix is not installed. Aborting."; exit 1; }
PROJECT_PATH=$(git rev-parse --show-toplevel)
[[ "$?" != "0" ]] && echo "Stale test watch works only within git project." && exit 1;
declare -a IGNORED_PATHS
declare -i throttle_by=2
@AndrewDryga
AndrewDryga / twiml.xml
Created July 6, 2020 18:44
TwiML experiments
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>+15671112233</Number>
<Number>+15671112234</Number>
</Dial>
</Response>
@AndrewDryga
AndrewDryga / xref.ex
Created May 7, 2018 14:38
Find unused code
defmodule Mix.Tasks.Xref2 do
use Mix.Task
import Mix.Compilers.Elixir,
only: [read_manifest: 2, source: 0, source: 1, source: 2, module: 1]
@shortdoc "Performs cross reference checks"
@recursive true
@manifest "compile.elixir"
@AndrewDryga
AndrewDryga / booking_sage_multiple.ex
Last active December 1, 2018 17:47
Sage: Make multiple bookings
def book_trip(attrs) do
with {:ok, exchange_rates} <- BillingAPI.fetch_currency_exchange_rates(attrs),
{:ok, card_authorization} <- BillingAPI.authorize_card(exchange_rates, attrs),
{:booking, {:ok, hotel_booking}, _} <- {:booking, HotelsBookingAPI.book_hotel(attrs), []},
{:booking, {:ok, car_booking}, _} <- {:booking, CarsBookingAPI.book_hotel(attrs), [hotel_booking]},
{:booking, {:ok, flight_booking}, _} <- {:booking, FlightsBookingAPI.book_hotel(attrs), [hotel_booking, car_booking]},
:ok <- Mailer.send_email_confirmation(card_authorization, hotel_booking, attrs),
{:charge, {:ok, charge}, _} <- {:charge, BillingAPI.charge_card(card_authorization), [...]} do
{:ok, %{charge: charge, bookings: [hotel_booking, car_booking, flight_booking]}}
else