Skip to content

Instantly share code, notes, and snippets.

@garthk
garthk / bundler.py
Created December 18, 2020 05:23
Poetry Lambda bundler
#!/usr/bin/env python3
"""
Bundle the converter.
Inspiration: https://github.com/python-poetry/poetry/pull/2682
"""
from argparse import ArgumentParser
from contextlib import contextmanager
@garthk
garthk / PASTE_ME.exs
Last active November 5, 2020 19:08
Watch all Phoenix.LiveView and LiveComponent handle_event callbacks
(fn gl ->
:dbg.stop_clear()
repr = fn
%Phoenix.LiveView.Socket{} -> "socket"
v -> inspect(v)
end
{:ok, _} =
:dbg.tracer(
@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 / smuggle.ex
Last active October 13, 2020 02:58
Smuggle Terms as Self-Extracting Elixir
defmodule Smuggle do
@moduledoc """
Make it easier to smuggle data from one system to another.
If your target is an `iex` prompt:
Smuggle.dump(value)
# copy
# paste into other iex
@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