Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support otp-26 #7

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 25.0.3
elixir 1.14.0-otp-25
elixir 1.15.5-otp-26
erlang 26.1
11 changes: 4 additions & 7 deletions lib/fluxter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ defmodule Fluxter do
@doc """
Should be the same as `measure(measurement, [], [], fun_or_mfa)`.
"""
@callback measure(measurement, (() -> result) | mfa()) :: result when result: var
@callback measure(measurement, (-> result) | mfa()) :: result when result: var

@doc """
Should be the same as `measure(measurement, tags, [], fun_or_mfa)`.
"""
@callback measure(measurement, tags, (() -> result) | mfa()) :: result when result: var
@callback measure(measurement, tags, (-> result) | mfa()) :: result when result: var

@doc """
Measures the execution time of `fun_or_mfa` and writes it as a metric.
Expand Down Expand Up @@ -195,7 +195,7 @@ defmodule Fluxter do
2

"""
@callback measure(measurement, tags, fields, (() -> result) | mfa()) :: result when result: var
@callback measure(measurement, tags, fields, (-> result) | mfa()) :: result when result: var

@doc """
Should be the same as `start_counter(measurement, [], [])`.
Expand Down Expand Up @@ -312,10 +312,7 @@ defmodule Fluxter do

config = Fluxter.get_config(__MODULE__, options)

conn =
config.host
|> Fluxter.Conn.new(config.port)
|> Map.update!(:header, &[&1 | config.prefix])
conn = Fluxter.Conn.new(config.host, config.port, config.prefix)

@worker_names
|> Enum.map(fn name ->
Expand Down
27 changes: 17 additions & 10 deletions lib/fluxter/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ defmodule Fluxter.Conn do

require Logger

defstruct [:sock, :header]
defstruct [:sock, :address, :port, :prefix]

def new(host, port) when is_binary(host) do
new(String.to_charlist(host), port)
def new(host, port, prefix) when is_binary(host) do
new(String.to_charlist(host), port, prefix)
end

def new(host, port) when is_list(host) or is_tuple(host) do
{:ok, addr} = :inet.getaddr(host, :inet)
header = Packet.header(addr, port)
%__MODULE__{header: header}
def new(host, port, prefix) when is_list(host) or is_tuple(host) do
case :inet.getaddr(host, :inet) do
{:ok, address} ->
%__MODULE__{address: address, port: port, prefix: prefix}

{:error, reason} ->
raise(
"cannot get the IP address for the provided host " <>
"due to reason: #{:inet.format_error(reason)}"
)
end
end

def start_link(%__MODULE__{} = conn, worker) do
Expand All @@ -30,12 +37,12 @@ defmodule Fluxter.Conn do

def init(conn) do
{:ok, sock} = :gen_udp.open(0, active: false)
{:ok, %{conn | sock: sock}}
{:ok, %__MODULE__{conn | sock: sock}}
end

def handle_cast({:write, name, tags, fields}, conn) do
packet = Packet.build(conn.header, name, tags, fields)
send(conn.sock, {self(), {:command, packet}})
packet = Packet.build(conn.prefix, name, tags, fields)
:gen_udp.send(conn.sock, conn.address, conn.port, packet)
{:noreply, conn}
end

Expand Down
34 changes: 4 additions & 30 deletions lib/fluxter/packet.ex
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
defmodule Fluxter.Packet do
@moduledoc false

import Bitwise

otp_release = :erlang.system_info(:otp_release)
@addr_family if(otp_release >= '19', do: [1], else: [])

def header({n1, n2, n3, n4}, port) do
true = Code.ensure_loaded?(:gen_udp)

anc_data_part =
if function_exported?(:gen_udp, :send, 5) do
[0, 0, 0, 0]
else
[]
end

@addr_family ++
[
band(bsr(port, 8), 0xFF),
band(port, 0xFF),
band(n1, 0xFF),
band(n2, 0xFF),
band(n3, 0xFF),
band(n4, 0xFF)
] ++ anc_data_part
end

def build(header, name, tags, fields) do
def build(prefix, name, tags, fields) do
tags = encode_tags(tags)
fields = encode_fields(fields)
[header, encode_key(name), tags, ?\s, fields]
[prefix, encode_key(name), tags, ?\s, fields]
end

defp encode_tags([]), do: ""
Expand Down Expand Up @@ -61,7 +35,7 @@ defmodule Fluxter.Packet do
other
|> to_string()
|> String.trim_leading("_")
|> escape(' ,')
|> escape(~c" ,")
end
end

Expand All @@ -83,7 +57,7 @@ defmodule Fluxter.Packet do
|> String.trim()
|> case do
"" -> "\"empty\""
other -> [?\", escape(other, '"'), ?\"]
other -> [?\", escape(other, ~c"\""), ?\"]
end

true ->
Expand Down
4 changes: 2 additions & 2 deletions test/fluxter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule FluxterTest do
options = [port: 9092, prefix: "xyzzy"]
{:ok, _} = OtherFluxter.start_link(options)

OtherFluxter.write('foo', bar: 2)
OtherFluxter.write(~c"foo", bar: 2)
assert_receive {:echo, "xyzzy_foo bar=2i"}
after
:code.delete(OtherFluxter)
Expand Down Expand Up @@ -117,7 +117,7 @@ defmodule FluxterTest do
end

test "bool values" do
TestFluxter.write('foo', true)
TestFluxter.write(~c"foo", true)
assert_receive {:echo, "foo value=true"}
TestFluxter.write("foo", false)
assert_receive {:echo, "foo value=false"}
Expand Down