-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a behaviour around metadata plugins to give integrators an option…
… to add to the telemetry metadata
- Loading branch information
1 parent
189fe09
commit 661ed3b
Showing
6 changed files
with
271 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule OpenTelemetryAbsinthe.MetadataPlugin do | ||
@moduledoc """ | ||
A MetadataPlugin is used to allow library integrators to add their own | ||
metadata to the broadcasted telemetry events. | ||
Note: plugins are run after `OpenTelemetryAbsinthe.MetadataPlugin.StandardMetadata` | ||
so they should avoid the keys: | ||
``` | ||
operation_name | ||
operation_type | ||
schema | ||
errors | ||
status | ||
``` | ||
""" | ||
alias Absinthe.Blueprint | ||
|
||
@type metadata :: %{ | ||
atom() => any() | ||
} | ||
|
||
@callback metadata(Blueprint.t()) :: metadata() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule OpenTelemetryAbsinthe.StandardMetadataPlugin do | ||
@moduledoc """ | ||
An implementation of `OpenTelemetryAbsinthe.MetadataPlugin` behaviour that | ||
returns standard metadata: | ||
``` | ||
operation_name | ||
operation_type | ||
schema | ||
errors | ||
status | ||
``` | ||
""" | ||
@behaviour OpenTelemetryAbsinthe.MetadataPlugin | ||
|
||
alias Absinthe.Blueprint | ||
|
||
@impl OpenTelemetryAbsinthe.MetadataPlugin | ||
def metadata(%Blueprint{} = blueprint) do | ||
operation_type = get_operation_type(blueprint) | ||
operation_name = get_operation_name(blueprint) | ||
|
||
errors = blueprint.result[:errors] | ||
status = status(errors) | ||
|
||
%{ | ||
operation_name: operation_name, | ||
operation_type: operation_type, | ||
schema: blueprint.schema, | ||
errors: errors, | ||
status: status | ||
} | ||
end | ||
|
||
defp status(nil), do: :ok | ||
defp status([]), do: :ok | ||
defp status(_error), do: :error | ||
|
||
@spec get_operation_type(Absinthe.Blueprint.t()) :: any() | ||
def get_operation_type(%Blueprint{} = blueprint) do | ||
blueprint |> Absinthe.Blueprint.current_operation() |> Kernel.||(%{}) |> Map.get(:type) | ||
end | ||
|
||
@spec get_operation_name(Absinthe.Blueprint.t()) :: any() | ||
def get_operation_name(%Blueprint{} = blueprint) do | ||
blueprint |> Absinthe.Blueprint.current_operation() |> Kernel.||(%{}) |> Map.get(:name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule OpenTelemetryAbsinthe.StandardMetadataPluginTest do | ||
use ExUnit.Case | ||
|
||
alias OpenTelemetryAbsinthe.StandardMetadataPlugin | ||
|
||
test "should include operation name" do | ||
assert %{operation_name: "FindByUUID"} = | ||
[operations: [BlueprintArchitect.operation(name: "FindByUUID")]] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include operation type" do | ||
assert %{operation_type: :mutation} = | ||
[operations: [BlueprintArchitect.operation(type: :mutation)]] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include schema" do | ||
assert %{schema: __MODULE__} = | ||
[schema: __MODULE__] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include nil errors" do | ||
assert %{errors: nil} = | ||
[result: %{}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
|
||
assert %{errors: nil} = | ||
[result: %{errors: nil}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include empty errors" do | ||
assert %{errors: []} = | ||
[result: %{errors: []}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include errors" do | ||
assert %{errors: [:stuff_went_wrong, :wrong_number]} = | ||
[result: %{errors: [:stuff_went_wrong, :wrong_number]}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include ok status" do | ||
assert %{status: :ok} = | ||
[result: %{}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
|
||
assert %{status: :ok} = | ||
[result: %{errors: nil}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
|
||
assert %{status: :ok} = | ||
[result: %{errors: []}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
|
||
test "should include error status when there are errors" do | ||
assert %{status: :error} = | ||
[result: %{errors: [:stuff_went_wrong, :wrong_number]}] | ||
|> BlueprintArchitect.blueprint() | ||
|> StandardMetadataPlugin.metadata() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule BlueprintArchitect do | ||
@moduledoc false | ||
|
||
alias Absinthe.Blueprint | ||
|
||
@spec blueprint(keyword()) :: Blueprint.t() | ||
def blueprint(overrides \\ []) do | ||
%{ | ||
operations: [operation()] | ||
} | ||
|> Map.merge(Enum.into(overrides, %{})) | ||
|> then(&struct!(Blueprint, &1)) | ||
end | ||
|
||
@spec operation(keyword()) :: Blueprint.Document.Operation.t() | ||
def operation(overrides \\ []) do | ||
%{ | ||
name: "TestOperation", | ||
type: :query, | ||
current: true | ||
} | ||
|> Map.merge(Enum.into(overrides, %{})) | ||
|> then(&struct!(Blueprint.Document.Operation, &1)) | ||
end | ||
end |