Skip to content

Get X-Ray daemon address and port from environment variables #4

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

Merged
merged 3 commits into from
May 1, 2025
Merged
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 lib/aws_ex_ray.ex
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ defmodule AwsExRay do
name :: String.t,
annotations :: map,
opts :: keyword,
func :: fun
) :: :ok
func :: (String.t -> ret)
) :: ret when ret: term
def subsegment(name, annotations, opts, func) do

subsegment_state = start_subsegment(name, opts)
Expand Down
6 changes: 5 additions & 1 deletion lib/aws_ex_ray/aws_metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ defmodule AwsExRay.AwsMetadata do
case result do
%{"ec2" => _} ->
Application.put_env(:aws_ex_ray, :aws_metadata, result)
{{%RuntimeError{message: error_message = "Instance Meta Error" <> _}, _}, _} ->
# Failed to reach the instance metadata endpoint.
# Most likely we're not running on an EC2 instance.
:ok
_ ->
Logger.debug("Error during instance identity request: #{Exception.format_exit(result)}")
end
{:stop, :normal, state}
end

def request_aws_metadata() do
config = ExAws.Config.new(:ec2, require_imds_v2: true)
config = ExAws.Config.new(:ec2, require_imds_v2: true, access_key_id: "", secret_access_key: "", security_token: "")
result = ExAws.InstanceMeta.request(config, "http://169.254.169.254/latest/dynamic/instance-identity/document")
map = Jason.decode!(result)
:erlang.exit(%{"ec2" => %{
Expand Down
16 changes: 13 additions & 3 deletions lib/aws_ex_ray/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ defmodule AwsExRay.Config do
def daemon_address() do

address = get(:daemon_address,
@default_daemon_address)
System.get_env("_AWS_XRAY_DAEMON_ADDRESS",
@default_daemon_address))

charlist_address = address |> String.to_charlist()

Expand All @@ -74,8 +75,17 @@ defmodule AwsExRay.Config do

@spec daemon_port() :: non_neg_integer
def daemon_port() do
get(:daemon_port,
@default_daemon_port)
case get(:daemon_port, nil) do
nil ->
case System.get_env("_AWS_XRAY_DAEMON_PORT") do
port when is_binary(port) ->
String.to_integer(port)
nil ->
@default_daemon_port
end
port ->
port
end
end

@spec default_annotation() :: map
Expand Down