Skip to content

Instantly share code, notes, and snippets.

@garthk
garthk / lv_call_time.ex
Last active October 13, 2020 02:51
LiveView call time
defmodule LVCallTime do
@moduledoc """
Best used sparingly.
LVCallTime.start()
:timer.sleep(:timer.seconds(20))
LVCallTime.dump()
LVCallTime.stop()
or just
@garthk
garthk / troubleshooting.exs
Created October 1, 2020 04:46
Watch Elasticsearch.post/3 et al
:dbg.stop_clear()
{:ok, _} =
:dbg.tracer(
:process,
{fn
{:trace, pid, :call, {mod, fun, args} = call}, stacks ->
mfa = Exception.format_mfa(mod, fun, Enum.count(args)) |> Pretty.inspect(label: ":call")
Smuggle.dump({mod, fun, args}, wrapper: :gfm)
stack = Map.get(stacks, pid, [])
@garthk
garthk / troubleshooting.exs
Created September 9, 2020 07:23
Watch Phoenix.PubSub.broadcast/2 calls from your context with :dbg
{:ok, _} =
:dbg.tracer(
:process,
{fn
{:trace, pid, :call, {mod, fun, args} = call}, stacks ->
mfa = Exception.format_mfa(mod, fun, Enum.count(args))
stack = Map.get(stacks, pid, [])
if match?({Phoenix.PubSub, :broadcast, _}, call) do
Pretty.inspect(stack, label: "broadcast stack")
@garthk
garthk / pid_snitch.ex
Created July 10, 2020 00:22
Elixir test PID capture
defmodule PidSnitchFormatter do
@moduledoc """
Report test PIDs?
```bash
mix test --formatter PidSnitchFormatter
```
See `ExUnit.CLIFormatter` for protocol details.
"""
# docker build -t start_span .
# docker run --rm -ti start_span
FROM elixir:1.10.3-alpine
RUN cd /usr/local/src && mix new start_span
WORKDIR /usr/local/src/start_span
COPY mix.exs README.md ./
COPY start_span.ex lib/
RUN mix local.hex --force && mix local.rebar --force && mix deps.get
CMD ["mix", "dialyzer"]
@garthk
garthk / telemetry_tracing_experiment_test.exs
Last active July 7, 2020 23:42
Using telemetry to capture OpenCensus trace span in Elixir
defmodule TelemetryTracingExperimentTest do
use ExUnit.Case, async: true
@doc """
Dump a variable to standard output, ANSI formatted, and pretty.
"""
def dump(v) do
opts = IEx.Config.inspect_opts()
[:reset, "\n", get_in(opts, [:syntax_colors, :reset]) || :reset, inspect(v, opts), :reset]
@garthk
garthk / auto_records.ex
Created April 10, 2020 09:43
Automatic record macro for Elixir
defmodule AutoRecords do
defmacro __using__(_opts) do
quote do
require OpenTelemetry.Records.Auto
import OpenTelemetry.Records.Auto, only: [auto_record: 2]
end
end
defmacro auto_record(name, from_lib) do
@garthk
garthk / Dockerfile
Last active April 8, 2020 01:32
Demonstration of problem installing Scalyr Agent 2 on Amazon Linux 2
FROM amazonlinux:2
COPY scalyr.key /etc/pki/rpm-gpg/RPM-GPG-KEY-scalyr-1
COPY scalyr.repo /etc/yum.repos.d/
RUN yum install -y jq systemd-python python2-pip scalyr-agent-2
RUN pip2 install docker-py
CMD ["python2", "-c", "import docker; docker.APIClient"]
@garthk
garthk / find-emoji-in-git-log.exs
Created March 12, 2020 04:11
Find Emoji in git log output using Elixir
#! /usr/bin/env elixir
defmodule FindEmoji do
def find_emoji_in_git_log do
:ok =
stream_executable("git", ["log"])
|> Stream.filter(&contains_emoji?/1)
|> Stream.each(&IO.puts/1)
|> Stream.run()
end
@garthk
garthk / profile
Created June 21, 2015 23:51
boot2docker 1.7.0 cert fix
wait4eth1() {
CNT=0
until ip a show eth1 | grep -q UP
do
[ $((CNT++)) -gt 60 ] && break || sleep 1
done
sleep 1
}
wait4eth1