Skip to content

Commit

Permalink
Support federated tracing v1
Browse files Browse the repository at this point in the history
  • Loading branch information
kdawgwilk committed Jan 5, 2022
1 parent 93b6230 commit f93960d
Show file tree
Hide file tree
Showing 18 changed files with 1,768 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
line_length: 120,
import_deps: [:absinthe]
import_deps: [:absinthe, :protobuf]
]
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ To get started with the project, run `mix deps.get` in the root directory to ins
```sh
$ mix deps.get
```

To run tests:

```sh
$ mix test
```

To generate tracing proto:

```sh
$ protoc --elixir_out=./lib/absinthe/federation reports.proto
```

### Commit message convention

We follow the [conventional commits specification](https://www.conventionalcommits.org/en) for our commit messages:
Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,49 @@ defmodule MySchema do
end
```

## Tracing

### Register Middlware

```elixir
defmodule MyApp.MySchema do
use Absinthe.Schema
use Absinthe.Federation.Schema
+ use Absinthe.Federation.Tracing

query do
...
end
end
```

If you have a custom middleware stack, add the federation tracing middleware to the **beginning** of your middleware stack:

```elixir
def middleware(middleware, _field, _object),
do: [Absinthe.Federation.Tracing.Middleware] ++ [...your other middlewares]
```

### Register the Pipeline

Specify the pipeline in your Absinthe.Plug endpoint:

```elixir
forward "/graphql", Absinthe.Plug,
schema: MyApp.MySchema,
pipeline: {Absinthe.Federation.Tracing.Pipeline, :plug}
```

If you have your own pipeline function, you can add the phases directly:

```elixir
def my_pipeline_creator(config, pipeline_opts) do
config.schema_mod
|> Absinthe.Pipeline.for_document(pipeline_opts)
|> add_my_phases() # w.e your custom phases are
|> Absinthe.Federation.Tracing.Pipeline.add_phases(pipeline_opts) # Add at the end
end
```
## More Documentation

See additional documentation, including guides, in the [Absinthe.Federation hexdocs](https://hexdocs.pm/absinthe_federation).
Expand Down
27 changes: 27 additions & 0 deletions lib/absinthe/federation/plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Absinthe.Federation.Plug do
@behaviour Plug
import Plug.Conn

@impl Plug
defdelegate init(opts), to: Absinthe.Plug

@impl Plug
@spec call(Plug.Conn.t(), map) :: Plug.Conn.t() | no_return
def call(conn, config) do
IO.inspect(config, label: "config.before")

config =
conn
|> get_req_header("apollo-federation-include-trace")
|> case do
[include_trace] ->
put_in(config, [:context, :apollo_federation_include_trace], include_trace)

_ ->
config
end
|> IO.inspect(label: "config.after")

Absinthe.Plug.call(conn, config)
end
end
Loading

0 comments on commit f93960d

Please sign in to comment.