Skip to content

Commit

Permalink
Merge pull request #1 from tsloughter/trace-mode
Browse files Browse the repository at this point in the history
initial trace module
  • Loading branch information
tsloughter authored Dec 18, 2018
2 parents 096ae5a + 065067f commit efec29d
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 20 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/_build
/cover
/deps
/doc
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez
*.beam
/config/*.secret.exs

# Ignore package tarball (built via "mix hex.build").
opencensus-*.tar
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# opencensus_elixir
# Opencensus

## Installation

```elixir
def deps do
[
{:opencensus_elixir, "~> 0.1.0"}
]
end
```

30 changes: 30 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure your application as:
#
# config :opencensus, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:opencensus, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env()}.exs"
32 changes: 32 additions & 0 deletions lib/opencensus/trace.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Opencensus.Trace do
@doc "Wraps the given block in a tracing child span with the given label/name and optional attributes"
defmacro with_child_span(label, attributes \\ quote(do: %{}), do: block) do
line = __CALLER__.line
module = __CALLER__.module
file = __CALLER__.file
function = format_function(__CALLER__.function)

quote do
attributes = Map.merge(unquote(attributes), %{
line: unquote(line),
module: unquote(module),
file: unquote(file),
function: unquote(function)
})

parent_span_ctx = :ocp.current_span_ctx()
new_span_ctx = :oc_trace.start_span(unquote(label), parent_span_ctx, unquote(attributes))
:ocp.with_span_ctx(new_span_ctx)

try do
unquote(block)
after
:oc_trace.finish_span(new_span_ctx)
:ocp.with_span_ctx(parent_span_ctx)
end
end
end

defp format_function(nil), do: nil
defp format_function({name, arity}), do: "#{name}/#{arity}"
end
27 changes: 27 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule OpencensusElixir.MixProject do
use Mix.Project

def project do
[
app: :opencensus_elixir,
version: "0.1.0",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:opencensus, "~> 0.7.0"}
]
end
end
8 changes: 8 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%{
"counters": {:hex, :counters, "0.2.1", "aa3d97e88f92573488987193d0f48efce0f3b2cd1443bf4ee760bc7f99322f0c", [:mix, :rebar3], [], "hexpm"},
"ctx": {:hex, :ctx, "0.5.0", "78e0f16712e12d707a7f34277381b8e193d7c71eaa24d37330dc02477c09eda5", [:rebar3], [], "hexpm"},
"jsx": {:hex, :jsx, "2.9.0", "d2f6e5f069c00266cad52fb15d87c428579ea4d7d73a33669e12679e203329dd", [:mix, :rebar3], [], "hexpm"},
"opencensus": {:hex, :opencensus, "0.7.0", "b4c5f6a96f2ba2154570f24bb08b65d6ee0ff5b61518acf706ca760b7696a4e1", [:rebar3], [{:counters, "~> 0.2", [hex: :counters, repo: "hexpm", optional: false]}, {:ctx, "~> 0.5", [hex: :ctx, repo: "hexpm", optional: false]}, {:jsx, "~> 2.9", [hex: :jsx, repo: "hexpm", optional: false]}, {:wts, "~> 0.3", [hex: :wts, repo: "hexpm", optional: false]}], "hexpm"},
"rfc3339": {:hex, :rfc3339, "0.9.0", "2075653dc9407541c84b1e15f8bda2abe95fb17c9694025e079583f2d19c1060", [:mix, :rebar], [], "hexpm"},
"wts": {:hex, :wts, "0.3.0", "5cdf22c775cb1ebae24c326a5db6074d753c42f4bd12a9aa47cc62d3e2c71ad1", [:rebar3], [{:rfc3339, "~>0.9.0", [hex: :rfc3339, repo: "hexpm", optional: false]}], "hexpm"},
}
8 changes: 8 additions & 0 deletions test/opencensus_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule OpencensusTest do
use ExUnit.Case
doctest Opencensus

test "do some span or something" do
assert true
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit efec29d

Please sign in to comment.