Skip to content
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
101 changes: 94 additions & 7 deletions config/side-infra/config.alloy.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,112 @@ logging {
format = "logfmt"
}

local.file_match "spring_logs" {
path_targets = [{ __path__ = "/var/log/spring/*.log" }] // 서비스 로그 파일 경로
// <1. 각 로그 레벨별로 파일 매칭
local.file_match "spring_info" {
path_targets = [{
__path__ = "/var/log/spring/info/*.log",
level = "info",
}]
}

loki.source.file "spring_source" {
targets = local.file_match.spring_logs.targets // 위에서 정의한 로그 파일 경로 사용
forward_to = [loki.process.spring_labels.receiver] // 읽은 로그를 처리 단계로 전달
local.file_match "spring_warn" {
path_targets = [{
__path__ = "/var/log/spring/warn/*.log",
level = "warn",
}]
}

loki.process "spring_labels" {
forward_to = [loki.write.grafana_loki.receiver] // 처리된 로그를 Loki로 전송
local.file_match "spring_error" {
path_targets = [{
__path__ = "/var/log/spring/error/*.log",
level = "error",
}]
}

local.file_match "spring_api_perf" {
path_targets = [{
__path__ = "/var/log/spring/api-perf/*.log",
level = "info",
log_type = "performance",
}]
}
// 1>

// <2. 각 소스 정의
loki.source.file "spring_info" {
targets = local.file_match.spring_info.targets
forward_to = [loki.process.spring_info_labels.receiver]
}

loki.source.file "spring_warn" {
targets = local.file_match.spring_warn.targets
forward_to = [loki.process.spring_warn_labels.receiver]
}

loki.source.file "spring_error" {
targets = local.file_match.spring_error.targets
forward_to = [loki.process.spring_error_labels.receiver]
}

loki.source.file "spring_api_perf" {
targets = local.file_match.spring_api_perf.targets
forward_to = [loki.process.spring_api_perf_labels.receiver]
}
// 2>

// <3. 각 프로세서 정의 (레벨별 라벨 추가)
loki.process "spring_info_labels" {
forward_to = [loki.write.grafana_loki.receiver]

stage.static_labels {
values = {
service = "backend",
env = sys.env("ALLOY_ENV"),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

Grafana Alloy sys.env function behavior undefined environment variable

💡 Result:

Short answer: sys.env returns an empty string ("") when the named environment variable does not exist — not null or an error. Use coalesce or test for "" to provide a default. [1][2]

Examples:

  • sys.env("DOES_NOT_EXIST") => "" . [1]
  • coalesce(sys.env("DOES_NOT_EXIST"), "default") => "default". [2]

Note: if you see errors like "component 'sys.env' does not exist or is out of scope", that usually means the expression was used in a context where functions/stdlib aren’t available or referenced incorrectly — check placement/scope in your config. [3]

Sources: Grafana Alloy docs for sys.env and coalesce; community forum thread. [1][2][3]


🏁 Script executed:

# Search for the config file and examine the context around lines 70, 83, 96, 109
find . -name "config.alloy.tftpl" -o -name "*.alloy" | head -20

Repository: solid-connection/solid-connection-infra

Length of output: 121


🏁 Script executed:

# Examine the config file around the mentioned lines
cat -n ./config/side-infra/config.alloy.tftpl | sed -n '50,120p'

Repository: solid-connection/solid-connection-infra

Length of output: 2099


배포 환경에서 ALLOY_ENV 환경 변수 설정 확인

sys.env("ALLOY_ENV")가 설정되지 않은 경우 빈 문자열을 반환하므로, 로그의 env 레이블이 비어있게 됩니다. 배포 환경에서 ALLOY_ENV 환경 변수가 항상 설정되어 있는지 확인하세요. 필요시 coalesce(sys.env("ALLOY_ENV"), "unknown")를 사용하여 기본값을 설정할 수 있습니다.

또한 적용됨: 83-83, 96-96, 109-109

🤖 Prompt for AI Agents
In @config/side-infra/config.alloy.tftpl at line 70, The template currently uses
sys.env("ALLOY_ENV") which returns an empty string if unset; update each
occurrence of sys.env("ALLOY_ENV") (including the instances used for the env
label) to wrap it with coalesce(sys.env("ALLOY_ENV"), "unknown") so the env
label defaults to "unknown" when the ALLOY_ENV variable is not set; apply this
change to all occurrences mentioned in the review.

level = "info",
log_type = "application",
}
}
}

loki.process "spring_warn_labels" {
forward_to = [loki.write.grafana_loki.receiver]

stage.static_labels {
values = {
service = "backend",
env = sys.env("ALLOY_ENV"),
level = "warn",
log_type = "application",
}
}
}

loki.process "spring_error_labels" {
forward_to = [loki.write.grafana_loki.receiver]

stage.static_labels {
values = {
service = "backend",
env = sys.env("ALLOY_ENV"),
level = "error",
log_type = "application",
}
}
}

loki.process "spring_api_perf_labels" {
forward_to = [loki.write.grafana_loki.receiver]

stage.static_labels {
values = {
service = "backend",
env = sys.env("ALLOY_ENV"),
level = "info",
log_type = "performance",
}
}
}
// 3>

loki.write "grafana_loki" {
endpoint {
Expand Down