Skip to content

Instantly share code, notes, and snippets.

@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
@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 / Collisions-in-Key-Sortable-Unique-Identifiers-KSUID-in-Elixir.md
Last active January 30, 2020 22:29
Birthday Paradox for Random Collision Avoidance in Elixir

Collisions in Key-Sortable Unique Identifiers (KSUIDs)

Expanding on my thread, it’s been nagging me that the random tail on small key sortable unique IDs (KSUID) might not provide as much protection vs collision as I’d trusted. Segment were able to throw bits at the problem, but I needed to pack my identifiers in smaller spaces.

After sparing 32 bits for the timestamp I might have room for only 32, 40, or 96 random bits. There’s that cryptographers’ rule of thumb about collision needing half the bit count… but how likely is it we’ll get at least one collision?

My math being a bit rusty, I decided to brute force it (birthday.exs). I didn't like my results.exs:

  • 39% failure for 2¹⁶ random choices from 2³² possible keys (3443 rounds)
  • 45% failure for 2²⁰ random choices from 2⁴⁰ possible keys (163 rounds)
@garthk
garthk / pascal_case.ex
Created January 17, 2020 00:45
Pascal case conversion in Elixir
@doc "Convert a string or atom to a pascal case string"
@spec pascal_case(String.t() | atom()) :: String.t()
def pascal_case(name)
def pascal_case(name) when is_atom(name), do: name |> Atom.to_string() |> pascal_case()
def pascal_case(name) when is_binary(name) do
~r{(\W|_)+}
|> Regex.split(name)
|> Enum.map(&capitalise/1)
|> Enum.to_list()
@garthk
garthk / Dockerfile
Last active January 13, 2020 00:28
Demonstrates a way to trigger Credo.Code.ParserError
FROM elixir:1.9.4-alpine
ADD . /root
WORKDIR /root
RUN mix do local.hex --force, deps.get
ENTRYPOINT []
CMD ["mix", "credo", "--strict", "credo732.ex"]