Skip to content

Commit

Permalink
Support JSON and GCL log formats
Browse files Browse the repository at this point in the history
Co-authored-by: Ariel Simulevski <Azer0s@users.noreply.github.com>
  • Loading branch information
kevinbader and Azer0s committed Dec 15, 2020
1 parent 0767fc5 commit 9a483cd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added detailed features summary on the website with architecture diagrams. [#284](https://github.com/Accenture/reactive-interaction-gateway/issues/284)
- Added [documentation section](https://accenture.github.io/reactive-interaction-gateway/docs/jwt-blacklisting.html) for the JWT Blacklist feature. [#156](https://github.com/Accenture/reactive-interaction-gateway/issues/156)
- Added longpolling examples to the `/examples` folder [#235](https://github.com/Accenture/reactive-interaction-gateway/issues/235)
- Support JSON and Google Cloud Logger (GCL) log formats. Set the new environment variable `LOG_FMT` to `JSON` or `GCL` to see this in action.
[#298](https://github.com/Accenture/reactive-interaction-gateway/pull/298)

### Changed

Expand Down
7 changes: 6 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ config :logger, :console,
format: "\n$time [$level] $levelpad$message\n$metadata\n",
metadata: metadata |> Enum.uniq()

# The logger is configured on Application startup.
config :logger,
backends: []

config :rig, Rig.Application,
log_level: {:system, :atom, "LOG_LEVEL", :debug},
log_level: {:system, "LOG_LEVEL", "debug"},
log_fmt: {:system, "LOG_FMT", "erlang"},
schema_registry_host: {:system, "KAFKA_SCHEMA_REGISTRY_HOST", nil}

# --------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use Mix.Config

# Do not print debug messages in production
config :rig, Rig.Application, log_level: {:system, :atom, "LOG_LEVEL", :warn}
config :rig, Rig.Application, log_level: {:system, "LOG_LEVEL", "warn"}

# --------------------------------------
# Peerage
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ config :rig, Rig.Kafka, enabled?: true
config :rig, Rig.KafkaTest, topic: "rig"

# Print only warnings and errors during test
config :rig, Rig.Application, log_level: {:system, :atom, "LOG_LEVEL", :warn}
config :rig, Rig.Application, log_level: {:system, "LOG_LEVEL", "warn"}

jwt_secret_key = "mysecret"
jwt_alg = "HS256"
Expand Down
5 changes: 3 additions & 2 deletions docs/rig-ops-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Variable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
`KINESIS_LOG_LEVEL` | The log level for the (Java) Kinesis-client subsystem. Allowed values: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL. | "INFO"
`KINESIS_OTP_JAR` | Path to the `OtpErlang.jar` file that contains the `JInterface` implementation. If left empty, RIG picks the file from its Erlang environment (Erlang must be compiled with Java support enabled). | nil
`KINESIS_STREAM` | The name of the Kinesis stream to consume. | "RIG-outbound"
`LOG_LEVEL` | Controls logging level for RIG, available values are: "debug", "info", "warn", "error". Production is using "warn" level. | :warn
`LOG_LEVEL` | Log level. One of: `debug`, `info`, `warn`, `error`. | "warn"
`LOG_FMT` | Log format. May be set to `json` or `gcl` (Google Cloud Logger LogEntry format). | "erlang"
`NATS_SERVERS` | List of [NATS](https://nats.io) servers RIG should connect to, delimited by comma (e.g., `localhost:4222,example.com:4222`). | []
`NATS_SOURCE_TOPICS` | List of NATS topics to subscribe to, delimited by comma. | ["rig"]
`NATSTOFILTER_QUEUE_GROUP` | NATS [queue group](https://docs.nats.io/developing-with-nats/receiving/queues) used for forwarding events according to subscriptions over SSE and WS connections. Make sure to use the same value on all RIG instances that belong to the same cluster. The default should be fine. | "rig-nats-to-filter"
Expand All @@ -64,7 +65,7 @@ Variable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
`PROXY_KAFKA_RESPONSE_TIMEOUT` | In case an endpoint has `target` set to `http` and `response_from` set to `kafka`, this is the maximum delay between an HTTP request and the corresponding Kafka response message. | 5000
`PROXY_KINESIS_RESPONSE_TIMEOUT` | In case an endpoint has `target` set to `http` and `response_from` set to `kinesis`, this is the maximum delay between an HTTP request and the corresponding Kinesis response message. | 5000
`PROXY_KINESIS_REQUEST_REGION` | AWS region for Kinesis stream publishing events from proxy. | "eu-west-1"
`PROXY_NATS_RESPONSE_TIMEOUT` | If `response_from` is set to `nats`, this defines how long RIG waits for the response. | 60_000
`PROXY_NATS_RESPONSE_TIMEOUT` | If `response_from` is set to `nats`, this defines how long RIG waits for the response. | 60000
`REQUEST_LOG` | Type of loggers to use to log requests processed by API Proxy, delimited by comma. | []
`SUBMISSION_CHECK` | Select if and how submitting/publishing events should be denied. Can be either `no_check` (submissions are always allowed), `jwt_validation` (submissions are allowed if at least one authorization token is valid - using JWT_SECRET_KEY - and not blacklisted), or an URL that points to an external service that decides whether to allow or deny the submissions. Such an external service is expected to accept POST requests. The CloudEvent is passed as a JSON map in the body. The original request's `Authorization` headers are reused for this request. The submission is allowed if the service returns 2xx and denied otherwise; return either 401 or 403 to reject a submission request. | "NO_CHECK"
`SUBSCRIPTION_CHECK` | Select if and how creating subscriptions should be denied. Can be either `no_check` (subscriptions are always allowed), `jwt_validation` (subscription are allowed if at least one authorization token is valid - using JWT_SECRET_KEY - and not blacklisted), or an URL that points to an external service that decides whether to allow or deny the subscription. Such an external service is expected to accept POST requests. The subscription parameters are passed in the body. The original request's `Authorization` headers are reused for this request. The subscription is allowed if the service returns 2xx and denied otherwise; return either 401 or 403 to reject a subscription request. | "NO_CHECK"
Expand Down
42 changes: 39 additions & 3 deletions lib/rig/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ defmodule Rig.Application do
@moduledoc false

use Application
use Rig.Config, [:log_level, :schema_registry_host]
use Rig.Config, [:log_level, :log_fmt, :schema_registry_host]

alias RIG.Discovery
alias RIG.Tracing
alias RigOutboundGateway.Kinesis

alias LoggerJSON.Formatters.BasicLogger

def start(_type, _args) do
alias Supervisor.Spec

# Override application logging with environment variable
Logger.configure([{:level, config().log_level}])
setup_logger()

Tracing.start()
Discovery.start()
Expand Down Expand Up @@ -55,6 +56,41 @@ defmodule Rig.Application do
Supervisor.start_link(children, opts)
end

defp setup_logger do
conf = config() |> IO.inspect(label: "config()")
level = conf.log_level |> String.downcase() |> String.to_atom()
format = conf.log_fmt |> String.downcase()

# Override application logging with environment variable
Logger.configure([{:level, level}])
setup_logger_backend(format)
end

defp setup_logger_backend(format)

defp setup_logger_backend("json") do
Logger.add_backend(LoggerJSON)

Logger.configure_backend(LoggerJSON,
formatter: LoggerJSON.Formatters.BasicLogger,
metadata: :all
)
end

# Google Cloud Logger format
defp setup_logger_backend("gcl") do
Logger.add_backend(LoggerJSON)

Logger.configure_backend(LoggerJSON,
formatter: LoggerJSON.Formatters.GoogleCloudLogger,
metadata: :all
)
end

defp setup_logger_backend("erlang") do
Logger.add_backend(:console)
end

@doc """
This function is called by an application after a code replacement, if the configuration parameters have changed.
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ defmodule RIG.MixProject do
[
mod: {Rig.Application, []},
extra_applications: [
:logger,
:runtime_tools,
:prometheus_ex,
:prometheus_plugs
Expand Down Expand Up @@ -179,7 +178,9 @@ defmodule RIG.MixProject do
{:opencensus_jaeger, "~> 0.0.1"},
{:opencensus_zipkin, "~> 0.3"},
# NATS client:
{:gnat, "~> 1.0"}
{:gnat, "~> 1.0"},
# JSON Log backend
{:logger_json, "~> 4.0"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"jsone": {:hex, :jsone, "1.4.6", "644d6d57befb22c8e19b324dee19d73b1c004565009861a8f64c68b7b9e64dbf", [:rebar3], [], "hexpm", "78eee8bb38f0bee2e73673d71bc75fc6fb01f56f0d23e769a26eee3655487a38"},
"jsx": {:hex, :jsx, "2.9.0", "d2f6e5f069c00266cad52fb15d87c428579ea4d7d73a33669e12679e203329dd", [:mix, :rebar3], [], "hexpm", "8ee1db1cabafdd578a2776a6aaae87c2a8ce54b47b59e9ec7dab5d7eb71cd8dc"},
"kafka_protocol": {:hex, :kafka_protocol, "2.3.6", "df076a8ef49fffae3535c805cb00f3a057ce1895e63398bf8a10569eeeac02f8", [:rebar, :rebar3], [{:crc32cer, "0.1.4", [hex: :crc32cer, repo: "hexpm", optional: false]}, {:snappyer, "1.2.5", [hex: :snappyer, repo: "hexpm", optional: false]}], "hexpm", "7cb061fe46babc7fd269d2c0e5b4dba5d1efc4f7dacce85b17a9cca973106b23"},
"logger_json": {:hex, :logger_json, "4.1.0", "76037738cd0b71bfb6230c8e3dc505c4f5789bb47e7a2e3d4a0421c6778d48e4", [:mix], [{:ecto, "~> 2.1 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "eab61e32e8efe185dc679785c50cb71a91320b7c94104fa16d8d27d6d5984e95"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.1", "4f0e96847c63c17841d42c08107405a005a2680eb9c7ccadfd757bd31dabccfb", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f2438b1a80eaec9ede832b5c41cd4f373b38fd7aa33e3b22d9db79e640cbde11"},
"memoize": {:hex, :memoize, "1.3.2", "f928aea1597b063d148158198a53590c9e5bf8d0c1c8aca8e20b78e566163679", [:mix], [], "hexpm", "c757710fd8afb5a305da997f0a8c7a1cb54dd700af04d3208198553f9b4269c3"},
Expand Down

0 comments on commit 9a483cd

Please sign in to comment.