Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic auth support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 28 additions & 5 deletions lib/loki_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule LokiLogger do
@behaviour :gen_event
@moduledoc false

@derive {Inspect, except: [:basic_auth_user, :basic_auth_password]}
defstruct buffer: [],
buffer_size: 0,
format: nil,
Expand All @@ -10,7 +11,9 @@ defmodule LokiLogger do
metadata: nil,
loki_labels: nil,
loki_host: nil,
loki_scope_org_id: nil
loki_scope_org_id: nil,
basic_auth_user: nil,
basic_auth_password: nil

def init(LokiLogger) do
config = Application.get_env(:logger, :loki_logger)
Expand Down Expand Up @@ -93,6 +96,9 @@ defmodule LokiLogger do
loki_host = Keyword.get(config, :loki_host, "http://localhost:3100")
loki_scope_org_id = Keyword.get(config, :loki_scope_org_id, "fake")

basic_auth_user = Keyword.get(config, :basic_auth_user)
basic_auth_password = Keyword.get(config, :basic_auth_password)

%{
state
| format: format,
Expand All @@ -101,7 +107,9 @@ defmodule LokiLogger do
max_buffer: max_buffer,
loki_labels: loki_labels,
loki_host: loki_host,
loki_scope_org_id: loki_scope_org_id
loki_scope_org_id: loki_scope_org_id,
basic_auth_user: basic_auth_user,
basic_auth_password: basic_auth_password
}
end

Expand Down Expand Up @@ -135,16 +143,31 @@ defmodule LokiLogger do
%{state | buffer: buffer, buffer_size: buffer_size + 1}
end

defp async_io(loki_host, loki_labels, loki_scope_org_id, output) do
defp async_io(loki_host, loki_labels, loki_scope_org_id, output, state) do
bin_push_request = generate_bin_push_request(loki_labels, output)

http_headers = [
{"Content-Type", "application/x-protobuf"},
{"X-Scope-OrgID", loki_scope_org_id}
]

basic_auth =
case not is_nil(state.basic_auth_user) and not is_nil(state.basic_auth_password) do
true -> %{user: state.basic_auth_user, password: state.basic_auth_password}
false -> nil
end

opts =
case basic_auth do
%{user: user, password: password} ->
[hackney: [basic_auth: {user, password}]]

_ ->
[]
end

# TODO: replace with async http call
case HTTPoison.post("#{loki_host}/api/prom/push", bin_push_request, http_headers) do
case HTTPoison.post("#{loki_host}/api/prom/push", bin_push_request, http_headers, opts) do
{:ok, %HTTPoison.Response{status_code: 204}} ->
# expected
:noop
Expand Down Expand Up @@ -235,7 +258,7 @@ defmodule LokiLogger do
buffer: buffer
} = state
) do
async_io(loki_host, loki_labels, loki_scope_org_id, buffer)
async_io(loki_host, loki_labels, loki_scope_org_id, buffer, state)
%{state | buffer: [], buffer_size: 0}
end

Expand Down