Skip to content

Commit

Permalink
chore: initial otel & prometheus config
Browse files Browse the repository at this point in the history
  • Loading branch information
kainlite committed Sep 24, 2024
1 parent 30bd65b commit 80b6920
Showing 20 changed files with 646 additions and 123 deletions.
2 changes: 2 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -118,3 +118,5 @@ config :git_hooks,
]
]
]

config :opentelemetry, traces_exporter: {:otel_exporter_stdout, []}
9 changes: 9 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
@@ -37,3 +37,12 @@ config :libcluster,
]
]
]

config :opentelemetry,
resource: %{service: %{name: "tr_app"}},
span_processor: :batch,
traces_exporter: :otlp

config :opentelemetry_exporter,
otlp_protocol: :http_protobuf,
otlp_endpoint: "http://opentelemetrycollector.monitoring.svc:4318"
4 changes: 4 additions & 0 deletions lib/tr/application.ex
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ defmodule Tr.Application do
def start(_type, _args) do
topologies = Application.get_env(:libcluster, :topologies, [])

:opentelemetry_cowboy.setup()
OpentelemetryPhoenix.setup(adapter: :cowboy2)
OpentelemetryEcto.setup([:dice_game, :repo])

children = [
# Start the Cluster supervisor for libcluster
{Cluster.Supervisor, [topologies, [name: Tr.ClusterSupervisor]]},
2 changes: 2 additions & 0 deletions lib/tr_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule TrWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :tr

plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]

if Application.compile_env(:tr, :sql_sandbox) do
plug Phoenix.Ecto.SQL.Sandbox
end
58 changes: 52 additions & 6 deletions lib/tr_web/live/beard_live.ex
Original file line number Diff line number Diff line change
@@ -72,18 +72,64 @@ defmodule TrWeb.BeardLive do
</ul>
</p>
<br>
<h4 class="mx-auto"><%= gettext("Wondering how we can help you?") %></h4>
<p class="text-lg mt-2">
<ul class="list-outside mt-2 text-left inline-block ml-60">
<li>
<%= gettext("Audit and validate your architecture, ask yourself:") %>
<ul>
<li>
<%= gettext("is our infrastructure secure? do we have the right security groups, access control lists,
policies? encryption in place?") %>
</li>
<li>
<%= gettext("Does our network have the capacity to grow and connect to other environments?") %>
</li>
<li>
<%= gettext("is our infrastructure cost efficient?") %>
</li>
</ul>
</li>
<li>
<%= gettext("Migrate projects to the cloud or back on-premises") %>
</li>
<li>
<%= gettext("Optimize and automate processes to free your team from repetitive, time-consuming tasks") %>
</li>
<li>
<%= gettext("Establish conventions and best practices to ensure consistency and scalability") %>
</li>
<li>
<%= gettext("Set up observability and monitoring") %>
<ul>
<li>
<%= gettext("is our app fast and reliable?") %>
</li>
<li>
<%= gettext("are we caching data efficiently (or at all)?") %>
</li>
<li>
<%= gettext("Is the database properly sized? Do we have working backups?") %>
</li>
</ul>
</li>
<li>
<%= gettext("We can help you find answers to these questions and so much more. Schedule a call and let’s get started!") %>
</li>
</ul>
</p>
<br />
<h3 class="text-xl font-bold">
<% locale = if Gettext.get_locale(TrWeb.Gettext) == "en", do: "es", else: "en" %>
<%= gettext("Looking for the ") %>
<.link aria-label="language-toggle" navigate={~p"/#{locale}"}>
<%= gettext "spanish" %>
</.link>
<%= gettext "version?" %>
<.link aria-label="language-toggle" navigate={~p"/#{locale}"}>
<%= gettext("spanish") %>
</.link>
<%= gettext("version?") %>
<%= gettext("Or the ") %><a href={~p"/#{Gettext.get_locale(TrWeb.Gettext)}/blog"}>blog?</a>
</h3>
</section>
<section class="mt-12 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 px-8">
104 changes: 55 additions & 49 deletions lib/tr_web/live/post_live.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
defmodule TrWeb.PostLive do
use TrWeb, :live_view

require OpenTelemetry.Tracer, as: Tracer

alias Tr.Blog
alias Tr.Post
alias TrWeb.CommentComponent
@@ -9,60 +12,63 @@ defmodule TrWeb.PostLive do

@impl true
def mount(params, _session, socket) do
changeset = Tr.Post.change_comment(%Tr.Post.Comment{})
post_id = Map.get(params, "id")
Tracer.with_span("post") do
changeset = Tr.Post.change_comment(%Tr.Post.Comment{})
post_id = Map.get(params, "id")

if connected?(socket) do
# Comments PubSub
Post.subscribe(post_id)
if connected?(socket) do
# Comments PubSub
Post.subscribe(post_id)

# Presence PubSub
Phoenix.PubSub.subscribe(Tr.PubSub, post_id)
# Presence PubSub
Phoenix.PubSub.subscribe(Tr.PubSub, post_id)

# Track browser session
{:ok, _} =
Presence.track(self(), post_id, @topic, %{})
end
# Track browser session
{:ok, _} =
Presence.track(self(), post_id, @topic, %{})
end

connected_users = calculate_connected_users(post_id)

post = Blog.get_post_by_id!(Gettext.get_locale(TrWeb.Gettext), post_id)

socket =
socket
|> assign(:page_title, post.title)
|> assign(:params, params)
|> assign(:post, post)
|> assign(check_errors: false)
|> assign_form(changeset)
|> assign(:comments, Tr.Post.get_parent_comments_for_post(post_id))
|> assign(:children, Tr.Post.get_children_comments_for_post(post_id))
|> assign(:parent_comment_id, nil)
|> assign(:connected_users, connected_users)
|> assign(:diff, nil)
|> assign(:reactions, Tr.Post.get_reactions(post_id))
|> assign(
:rocket_launch,
get_styled_reaction(post.id, "rocket-launch", socket.assigns.current_user)
)
|> assign(
:hand_thumb_up,
get_styled_reaction(post.id, "hand-thumb-up", socket.assigns.current_user)
)
|> assign(
:heart,
get_styled_reaction(post.id, "heart", socket.assigns.current_user)
)
|> assign(
:oauth_google_url,
ElixirAuthGoogle.generate_oauth_url(TrWeb.Endpoint.url())
)
|> assign(
:oauth_github_url,
ElixirAuthGithub.login_url(%{scopes: ["user:email"]})
)
connected_users = calculate_connected_users(post_id)

{:ok, socket, temporary_assigns: [form: nil]}
post = Blog.get_post_by_id!(Gettext.get_locale(TrWeb.Gettext), post_id)

socket =
socket
|> assign(:page_title, post.title)
|> assign(:params, params)
|> assign(:post, post)
|> assign(check_errors: false)
|> assign_form(changeset)
|> assign(:comments, Tr.Post.get_parent_comments_for_post(post_id))
|> assign(:children, Tr.Post.get_children_comments_for_post(post_id))
|> assign(:parent_comment_id, nil)
|> assign(:connected_users, connected_users)
|> assign(:diff, nil)
|> assign(:reactions, Tr.Post.get_reactions(post_id))
|> assign(
:rocket_launch,
get_styled_reaction(post.id, "rocket-launch", socket.assigns.current_user)
)
|> assign(
:hand_thumb_up,
get_styled_reaction(post.id, "hand-thumb-up", socket.assigns.current_user)
)
|> assign(
:heart,
get_styled_reaction(post.id, "heart", socket.assigns.current_user)
)
|> assign(
:oauth_google_url,
ElixirAuthGoogle.generate_oauth_url(TrWeb.Endpoint.url())
)
|> assign(
:oauth_github_url,
ElixirAuthGithub.login_url(%{scopes: ["user:email"]})
)

Tracer.set_attribute(:post, post.id)
{:ok, socket, temporary_assigns: [form: nil]}
end
end

@impl true
4 changes: 4 additions & 0 deletions manifests/monitoring/00-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: monitoring
33 changes: 33 additions & 0 deletions manifests/monitoring/01-rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/proxy
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups:
- extensions
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: default
namespace: monitoring
46 changes: 46 additions & 0 deletions manifests/monitoring/02-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-k8s-conf
labels:
name: prometheus-k8s-conf
namespace: monitoring
data:
prometheus.yml: |-
global:
scrape_interval: 10s
scrape_configs:
- job_name: 'monitoring-job'
static_configs:
- targets: ['opentelemetrycollector.monitoring.svc.cluster.local:9090']
---
apiVersion: v1
kind: ConfigMap
metadata:
name: collector-config
namespace: monitoring
data:
collector.yaml: |
receivers:
otlp:
protocols:
grpc:
processors:
batch:
send_batch_size: 10000
timeout: 5s
exporters:
prometheus:
endpoint: ":9090"
logging:
loglevel: debug
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheus, logging]
46 changes: 46 additions & 0 deletions manifests/monitoring/03-prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-k8s
namespace: monitoring
labels:
app: prometheus-k8s
spec:
replicas: 1
selector:
matchLabels:
app: prometheus-k8s
template:
metadata:
labels:
app: prometheus-k8s
spec:
containers:
- name: prometheus
image: quay.io/prometheus/prometheus
imagePullPolicy: IfNotPresent
args:
- "--storage.tsdb.retention.time=24h"
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus/"
ports:
- containerPort: 9090
resources:
requests:
cpu: 500m
memory: 500M
limits:
cpu: 1
memory: 1Gi
volumeMounts:
- name: prometheus-config-volume
mountPath: /etc/prometheus/
- name: prometheus-storage-volume
mountPath: /prometheus/
volumes:
- name: prometheus-config-volume
configMap:
defaultMode: 420
name: prometheus-k8s-conf
- name: prometheus-storage-volume
emptyDir: {}
15 changes: 15 additions & 0 deletions manifests/monitoring/04-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: prometheus-service
namespace: monitoring
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '9090'
spec:
selector:
app: prometheus-k8s
type: NodePort
ports:
- port: 9090
targetPort: 9090
Loading

0 comments on commit 80b6920

Please sign in to comment.