Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Code in this repository is licensed under the Apache 2.0. See [LICENSE](LICENSE)
|[datastore](datastore)|**Samples**: Basic examples for creating and querying Datastore using Diplomat.|
|[pubsub](pubsub)|**Samples**: Basic examples for using Google Cloud Pub/Sub API.|
|[speech](speech)|**Samples**: Basic examples for using Google Cloud Speech API.|
|[stackdriver_logging](stackdriver_logging)|**Samples**: Basic examples for using Google Cloud Stackdriver Logging API. |
|[storage](storage)|**Samples**: Basic examples for using Google Cloud Storage API.|
4 changes: 4 additions & 0 deletions stackdriver_logging/.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}"]
]
24 changes: 24 additions & 0 deletions stackdriver_logging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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 third-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

# Ignore package tarball (built via "mix hex.build").
stackdriver_logging_samples-*.tar

52 changes: 52 additions & 0 deletions stackdriver_logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Google Cloud Stackdriver Logging Samples

This project includes samples for running [Google Cloud Stackdriver Logging](https://cloud.google.com/logging/).

## Installing Dependencies

Install the dependencies, namely `stackdriver_logging_samples`, using Mix:

```sh
mix deps.get
```

## Authentication

Authentication is typically done through [Application Default Credentials][adc]
which means you do not have to change the code to authenticate as long as
your environment has credentials. Start by creating a
[Service Account key file][service_account_key_file]. This file can be used to
authenticate to Google Cloud Platform services from any environment. To use
the file, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to
the path to the key file, for example:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

## Running the Samples

Use Interactive Elixir and Mix to compile and run the samples

```sh
iex -S mix
```

Now you can run the samples! For example, to write a log,
type the following into the Interactive Elixir shell:

```elixir
iex(1)> project_id = "project_id_which_you_want_to_send"
"project_id_which_you_want_to_send"
iex(2)> log_id = "log_id_which_you_named"
"log_id_which_you_named"
iex(3)> GoogleApi.StackdriverLogging.Samples.write(project_id, log_id, "Hello! Stackdriver Logging")
{:ok, %GoogleApi.Logging.V2.Model.WriteLogEntriesResponse{}}
```

Then you check [log viewer](log_viewer), you get logs you've sent.

![](images/viewer.png)


[adc]: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
[service_account_key_file]: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
[log_viewer]: https://console.cloud.google.com/logs/viewer
Binary file added stackdriver_logging/images/viewer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule GoogleApi.StackdriverLogging.Samples do
@moduledoc """
A Sample module having function which sends to Stackdriver Logging.
"""

@doc """
Write a log.

## Examples

iex> GoogleApi.StackdriverLogging.Samples.write("your_project_id", "your_log_id", "Hello, Stackdriver!")

"""
def write(project_id, log_id, text)
when is_binary(project_id) and is_binary(log_id) and is_binary(text) do
# Get token which is able to send to Stackdriver Logging with using Goth.
# Valid scopes are listed up at https://cloud.google.com/logging/docs/access-control#scopes
scope = "https://www.googleapis.com/auth/logging.write"
{:ok, %{token: token}} = Goth.Token.for_scope(scope)
conn = GoogleApi.Logging.V2.Connection.new(token)

# Resource types and corresponding labels are listed up at https://cloud.google.com/logging/docs/api/v2/resource-list
monitored_resource = %GoogleApi.Logging.V2.Model.MonitoredResource{
type: "global",
labels: %{project_id: project_id}
}

# Make an individual log to send to Stackdriver Logging
# Proper logname is described at logName field in https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
# You don't need creating space to store logs. see https://cloud.google.com/logging/docs/api/tasks/creating-logs#creating_logs
log_name = "projects/#{project_id}/logs/#{log_id}"

entry = %GoogleApi.Logging.V2.Model.LogEntry{
resource: monitored_resource,
logName: log_name,
textPayload: text
}

# Make a log group to send to Stackdriver Logging
# https://hexdocs.pm/google_api_logging/0.17.0/GoogleApi.Logging.V2.Model.WriteLogEntriesRequest.html
# By the document above, you may put together logs to make efficient logging and avoid quota limit.
# However it doesn't matter in a sample, So we send just an individual log.
entries = %GoogleApi.Logging.V2.Model.WriteLogEntriesRequest{entries: [entry]}

# Send logs to Stack Driver
GoogleApi.Logging.V2.Api.Entries.logging_entries_write(conn, body: entries)
end
end
28 changes: 28 additions & 0 deletions stackdriver_logging/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule GoogleApi.StackdriverLogging.Samples.MixProject do
use Mix.Project

def project do
[
app: :stackdriver_logging_samples,
version: "0.1.0",
elixir: "~> 1.9",
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
[
{:goth, "~> 1.1"},
{:google_api_logging, "~> 0.17"}
]
end
end
21 changes: 21 additions & 0 deletions stackdriver_logging/mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%{
"base64url": {:hex, :base64url, "0.0.1", "36a90125f5948e3afd7be97662a1504b934dd5dac78451ca6e9abf85a10286be", [:rebar], [], "hexpm"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"google_api_logging": {:hex, :google_api_logging, "0.17.0", "7020437a4b4ffb9f8d02a931afb6b17d2b731a43a9654d2834e617c4bc45fc31", [:mix], [{:google_gax, "~> 0.2", [hex: :google_gax, repo: "hexpm", optional: false]}], "hexpm"},
"google_gax": {:hex, :google_gax, "0.2.0", "2017530e1b38cd157e7681452763c56bfdb3c2081ef65a38456452654791d496", [:mix], [{:poison, ">= 1.0.0 and < 4.0.0", [hex: :poison, repo: "hexpm", optional: false]}, {:tesla, "~> 1.2", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm"},
"goth": {:hex, :goth, "1.1.0", "85977656822e54217bc0472666f1ce15dc3921495ef5f4f0774ef15503bae207", [:mix], [{:httpoison, "~> 0.11 or ~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:joken, "~> 2.0", [hex: :joken, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.6.1", "2ce5bf6e535cd0ab02e905ba8c276580bab80052c5c549f53ddea52d72e81f33", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"joken": {:hex, :joken, "2.1.0", "bf21a73105d82649f617c5e59a7f8919aa47013d2519ebcc39d998d8d12adda9", [:mix], [{:jose, "~> 1.9", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm"},
"jose": {:hex, :jose, "1.9.0", "4167c5f6d06ffaebffd15cdb8da61a108445ef5e85ab8f5a7ad926fdf3ada154", [:mix, :rebar3], [{:base64url, "~> 0.0.1", [hex: :base64url, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"},
"tesla": {:hex, :tesla, "1.3.0", "f35d72f029e608f9cdc6f6d6fcc7c66cf6d6512a70cfef9206b21b8bd0203a30", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 0.4", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.3", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule GoogleApi.StackdriverLogging.SamplesTest do
use ExUnit.Case
end
1 change: 1 addition & 0 deletions stackdriver_logging/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()