c7edb8a48c
* generate a phoenix API only project * adds /api/rolldice endpoint and opentelemetry configuration * change span attribute name to match implementations in other langs --------- Co-authored-by: Tristan Sloughter <t@crashfast.com> |
||
---|---|---|
.. | ||
config | ||
lib | ||
priv | ||
test | ||
.formatter.exs | ||
.gitignore | ||
README.md | ||
docker-compose.yml | ||
mix.exs | ||
mix.lock | ||
otel-collector-config.yaml |
README.md
RollDice
The following example uses a basic Phoenix web application. For more elaborate examples, see examples.
To start your Phoenix server:
- Run
mix setup
to install and setup dependencies - Start the opentelemetry collector and Jaeger front-end with
docker-compose up -d
- Set your service name
export OTEL_SERVICE_NAME="roll_dice"
- Start Phoenix endpoint with
mix phx.server
or inside IEx withiex -S mix phx.server
To generate and view traces:
- Visit the new API endpoint with
curl http://localhost:4000/api/rolldice
or in your browser - Visit
localhost:16686
to see the traces in Jaeger
For more information on the OpenTelemetry instrumentation in this example, check out the guide for getting started with OpenTelemetry
How did we get here?
To begin, use the phx
generator to create a new project. To keep things simple we'll leave out the database and html for now with the --no-ecto
and --no-html
flags.
In a real app, you'll probably want to include ecto configured for your preferred database with the --database
flag (postgres
by default).
mix phx.new --no-ecto --no-html roll_dice
Rolling The Dice
Now we'll add an API endpoint that will let us roll the dice and return a random number between 1 and 6.
To start, we'll add a route to the /api
scope in your router
# lib/roll_dice_web/router.ex
scope "/api", DiceGameWeb do
pipe_through :api
get "/rolldice", DiceController, :roll
end
Then we'll create a new file in the controllers folder for that module. We told
the router that we will define a roll function, so we'll do that. It will return
a 200
response code and the result of a dice_roll
function, which we will
emit a span for. We also want to set the value of the generated roll as an
attribute on the span.
# lib/roll_dice_web/controllers/dice_controller.ex
defmodule DiceGameWeb.DiceController do
use DiceGameWeb, :controller
require OpenTelemetry.Tracer, as: Tracer
def roll(conn, _params) do
send_resp(conn, 200, roll_dice())
end
defp roll_dice do
Tracer.with_span("roll_dice") do
roll = Enum.random(1..6)
Tracer.set_attribute("roll.value", roll)
to_string(roll)
end
end
end
If you point your browser/curl/etc. to localhost:4000/api/rolldice
you should get a random number in response.
Ready to run in production? Please check our deployment guides.
Learn more
- Official website: https://www.phoenixframework.org/
- Guides: https://hexdocs.pm/phoenix/overview.html
- Docs: https://hexdocs.pm/phoenix
- Forum: https://elixirforum.com/c/phoenix-forum
- Source: https://github.com/phoenixframework/phoenix