opentelemetry-erlang-contrib/examples/roll_dice
Marc Delagrammatikas c7edb8a48c
Dice game example (#173)
* 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>
2023-10-27 13:23:07 -06:00
..
config Dice game example (#173) 2023-10-27 13:23:07 -06:00
lib Dice game example (#173) 2023-10-27 13:23:07 -06:00
priv Dice game example (#173) 2023-10-27 13:23:07 -06:00
test Dice game example (#173) 2023-10-27 13:23:07 -06:00
.formatter.exs Dice game example (#173) 2023-10-27 13:23:07 -06:00
.gitignore Dice game example (#173) 2023-10-27 13:23:07 -06:00
README.md Dice game example (#173) 2023-10-27 13:23:07 -06:00
docker-compose.yml Dice game example (#173) 2023-10-27 13:23:07 -06:00
mix.exs Dice game example (#173) 2023-10-27 13:23:07 -06:00
mix.lock Dice game example (#173) 2023-10-27 13:23:07 -06:00
otel-collector-config.yaml Dice game example (#173) 2023-10-27 13:23:07 -06:00

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 with iex -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