Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(traffic): stamp coolify_app_id + JSON access log on Caddy sites

Aditya Tripathibb7ad7ab129e
diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php
index 2589146254..b317383436 100644
--- a/bootstrap/helpers/docker.php
+++ b/bootstrap/helpers/docker.php
@@ -433,7 +433,7 @@ function isNoindexDomain(string $domain, ?Collection $noindex_domains): bool
->contains(ValidationPatterns::normalizeApplicationDomainUrl($domain));
}
-function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains, bool $is_force_https_enabled = false, $onlyPort = null, ?Collection $serviceLabels = null, ?bool $is_gzip_enabled = true, ?bool $is_stripprefix_enabled = true, ?string $service_name = null, ?string $image = null, string $redirect_direction = 'both', ?string $predefinedPort = null, bool $is_http_basic_auth_enabled = false, ?string $http_basic_auth_username = null, ?string $http_basic_auth_password = null, ?Collection $noindex_domains = null)
+function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains, bool $is_force_https_enabled = false, $onlyPort = null, ?Collection $serviceLabels = null, ?bool $is_gzip_enabled = true, ?bool $is_stripprefix_enabled = true, ?string $service_name = null, ?string $image = null, string $redirect_direction = 'both', ?string $predefinedPort = null, bool $is_http_basic_auth_enabled = false, ?string $http_basic_auth_username = null, ?string $http_basic_auth_password = null, ?Collection $noindex_domains = null, bool $is_traffic_analytics_enabled = false)
{
$labels = collect([]);
if ($serviceLabels) {
@@ -493,6 +493,11 @@ function fqdnLabelsForCaddy(string $network, string $uuid, Collection $domains,
if ($is_http_basic_auth_enabled) {
$labels->push("caddy_{$loop}.basicauth.{$http_basic_auth_username}=\"{$hashedPassword}\"");
}
+ if ($is_traffic_analytics_enabled) {
+ $labels->push("caddy_{$loop}.log.output=file /traffic/access.log");
+ $labels->push("caddy_{$loop}.log.format=json");
+ $labels->push("caddy_{$loop}.log_append=coolify_app_id {$uuid}");
+ }
}
return $labels->sort();
@@ -811,6 +816,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
http_basic_auth_username: $application->http_basic_auth_username,
http_basic_auth_password: $application->http_basic_auth_password,
noindex_domains: $noindexDomains,
+ is_traffic_analytics_enabled: $application->destination->server->isTrafficAnalyticsEnabled(),
));
break;
}
@@ -841,6 +847,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
http_basic_auth_username: $application->http_basic_auth_username,
http_basic_auth_password: $application->http_basic_auth_password,
noindex_domains: $noindexDomains,
+ is_traffic_analytics_enabled: $application->destination->server->isTrafficAnalyticsEnabled(),
));
}
}
@@ -882,6 +889,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
http_basic_auth_username: $application->http_basic_auth_username,
http_basic_auth_password: $application->http_basic_auth_password,
noindex_domains: $noindexDomains,
+ is_traffic_analytics_enabled: $application->destination->server->isTrafficAnalyticsEnabled(),
));
break;
}
@@ -910,6 +918,7 @@ function generateLabelsApplication(Application $application, ?ApplicationPreview
http_basic_auth_username: $application->http_basic_auth_username,
http_basic_auth_password: $application->http_basic_auth_password,
noindex_domains: $noindexDomains,
+ is_traffic_analytics_enabled: $application->destination->server->isTrafficAnalyticsEnabled(),
));
}
}
diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.php
index ae6dbf2357..a6c3b3ca7a 100644
--- a/bootstrap/helpers/proxy.php
+++ b/bootstrap/helpers/proxy.php
@@ -408,6 +408,7 @@ function generateDefaultProxyConfiguration(Server $server, array $custom_command
"{$proxy_path}/dynamic:/dynamic",
"{$proxy_path}/config:/config",
"{$proxy_path}/data:/data",
+ "{$proxy_path}:/traffic",
],
],
],
diff --git a/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.php b/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.php
new file mode 100644
index 0000000000..b5f0fadac5
--- /dev/null
+++ b/tests/Feature/TrafficAnalytics/CaddyTrafficLabelsTest.php
@@ -0,0 +1,13 @@
+<?php
+
+it('adds no traffic labels to caddy sites when disabled', function () {
+ $labels = fqdnLabelsForCaddy('coolify', 'app-uuid', collect(['https://example.com']), is_traffic_analytics_enabled: false);
+ expect($labels->filter(fn ($l) => str_contains($l, 'log_append'))->isEmpty())->toBeTrue();
+});
+
+it('stamps coolify_app_id and JSON access log on each caddy site when enabled', function () {
+ $labels = fqdnLabelsForCaddy('coolify', 'app-uuid', collect(['https://example.com']), is_traffic_analytics_enabled: true);
+ expect($labels->contains('caddy_0.log_append=coolify_app_id app-uuid'))->toBeTrue();
+ expect($labels->contains('caddy_0.log.output=file /traffic/access.log'))->toBeTrue();
+ expect($labels->contains('caddy_0.log.format=json'))->toBeTrue();
+});