Skip to content

Commit

Permalink
include documentation on how to create the app and api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdel committed May 18, 2023
1 parent ffb121d commit 85f7fd5
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion examples/dice_game/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
# DiceGame

The following example uses a basic [Phoenix](https://www.phoenixframework.org/)
web application. For more elaborate examples, see [examples](/docs/instrumentation/erlang/examples/).

To start your Phoenix server:

* Run `mix setup` to install and setup dependencies
* Start Phoenix endpoint with `mix phx.server` or inside IEx with `iex -S mix phx.server`

Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.

For more information on the OpenTelemetry instrumentation and dice roll API in this example, check out the guide for getting started with [OpenTelemetry in Phoenix](https://opentelemetry.io/docs/instrumentation/erlang/getting-started/phoenix/)
For more information on the OpenTelemetry instrumentation in this example, check out the guide for getting started with [OpenTelemetry](https://opentelemetry.io/docs/instrumentation/erlang/getting-started/)

## How did we get here?

To begin, use the `phx` generator to create a new project. To keep things simple we'll leave the database out for now with the `--no-ecto` flag.
In a real app, you'll probably want to include ecto configured for your preferred database with the `--database` flag (`postgres` by default).

```shell
mix phx.new --no-ecto dice_game
```

### 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

```elixir
# lib/dice_game_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.

```elixir
# lib/dice_game_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, dice_roll())
end

defp dice_roll do
Tracer.with_span("dice_roll") do
roll = Enum.random(1..6)

Tracer.set_attribute(:roll, roll)

to_string(roll)
end
end
end

```

If you point your browser/curl/etc. to [`localhost:4000/api/rolldice`](http://localhost:4000/api/rolldice) you should get a random number in response.

Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html).

Expand Down

0 comments on commit 85f7fd5

Please sign in to comment.