Skip to content

Feat/stats prom#74

Open
julsemaan wants to merge 6 commits into
masterfrom
feat/stats-prom
Open

Feat/stats prom#74
julsemaan wants to merge 6 commits into
masterfrom
feat/stats-prom

Conversation

@julsemaan
Copy link
Copy Markdown
Owner

No description provided.

@github-actions
Copy link
Copy Markdown

Go coverage report (utils)

github.com/julsemaan/anyfile-notepad/utils/utils.go:16:	SendEmail		100.0%
github.com/julsemaan/anyfile-notepad/utils/utils.go:48:	sendMailWithTLSConfig	0.0%
total:							(statements)		46.5%

@github-actions
Copy link
Copy Markdown

Go coverage report (webserver)

github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:38:				renderEmailTemplate		85.7%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:52:				LoadSubscription		100.0%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:71:				LoadGoogleUser			96.2%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:122:				handleSubscriptionRead		100.0%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:126:				handleSubscriptionResume	100.0%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:153:				handleSubscriptionCancel	86.7%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:182:				handleSubscriptionUpgrade	100.0%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:231:				upgradeError			100.0%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:238:				handleLinkCancel		60.0%
github.com/julsemaan/anyfile-notepad/webserver/billing_handlers.go:294:				handleStripeHook		72.4%
github.com/julsemaan/anyfile-notepad/webserver/cluster_observer.go:16:				NewClusterObserver		100.0%
github.com/julsemaan/anyfile-notepad/webserver/cluster_observer.go:20:				hostURL				75.0%
github.com/julsemaan/anyfile-notepad/webserver/cluster_observer.go:28:				glpclient			100.0%
github.com/julsemaan/anyfile-notepad/webserver/cluster_observer.go:40:				Start				0.0%
github.com/julsemaan/anyfile-notepad/webserver/email.go:16:					sendEmailWithOptionalTLS	100.0%
github.com/julsemaan/anyfile-notepad/webserver/email.go:47:					sendMailWithTLSConfig		0.0%
github.com/julsemaan/anyfile-notepad/webserver/emails_update/update_customers_emails.go:24:	Next				0.0%
github.com/julsemaan/anyfile-notepad/webserver/emails_update/update_customers_emails.go:28:	Customer			0.0%
github.com/julsemaan/anyfile-notepad/webserver/emails_update/update_customers_emails.go:32:	Err				0.0%
github.com/julsemaan/anyfile-notepad/webserver/emails_update/update_customers_emails.go:42:	main				0.0%
github.com/julsemaan/anyfile-notepad/webserver/emails_update/update_customers_emails.go:47:	updateCustomerEmails		100.0%
github.com/julsemaan/anyfile-notepad/webserver/http_handler.go:12:				ServeHTTP			100.0%
github.com/julsemaan/anyfile-notepad/webserver/http_handler.go:24:				setupPlusPlusSession		75.0%
github.com/julsemaan/anyfile-notepad/webserver/http_handler.go:40:				ServeStaticApplication		100.0%
github.com/julsemaan/anyfile-notepad/webserver/main.go:51:					main				0.0%
github.com/julsemaan/anyfile-notepad/webserver/main.go:71:					setupSessionsPersistence	0.0%
github.com/julsemaan/anyfile-notepad/webserver/main.go:95:					setupHandlers			93.3%
github.com/julsemaan/anyfile-notepad/webserver/main.go:147:					setupSubscriptions		0.0%
github.com/julsemaan/anyfile-notepad/webserver/main.go:165:					setupClusterObserver		0.0%
github.com/julsemaan/anyfile-notepad/webserver/main.go:178:					setupBlockedUsersMap		100.0%
github.com/julsemaan/anyfile-notepad/webserver/main.go:184:					setup				0.0%
github.com/julsemaan/anyfile-notepad/webserver/oauth2_handlers.go:23:				getGoogleOauth2Conf		100.0%
github.com/julsemaan/anyfile-notepad/webserver/oauth2_handlers.go:43:				handleGoogleOauth2Authorize	100.0%
github.com/julsemaan/anyfile-notepad/webserver/oauth2_handlers.go:55:				handleGoogleOauth2Callback	100.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:26:				NewPlusPlusSession		100.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:38:				NewPlusPlusSessions		100.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:45:				GenerateSessionID		80.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:62:				Set				100.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:68:				Get				100.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:74:				Maintenance			100.0%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:92:				RestoreFromFile			88.9%
github.com/julsemaan/anyfile-notepad/webserver/plus_plus_session.go:108:			SaveToFile			68.4%
github.com/julsemaan/anyfile-notepad/webserver/realtime_handler.go:13:				publishRealtimeEvent		100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:23:				Next				0.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:27:				Sub				0.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:31:				Err				0.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:53:				NewSubscriptions		100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:60:				Empty				100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:64:				CanHaveAccess			100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:68:				ExtractUserId			75.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:76:				SetSubscription			100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:90:				DelSubscription			100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:97:				GetSubscription			100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:104:				GetSubscriptionByCustomer	100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:117:				Reload				100.0%
github.com/julsemaan/anyfile-notepad/webserver/subscriptions.go:145:				Maintenance			83.3%
github.com/julsemaan/anyfile-notepad/webserver/utils.go:10:					secureRandomString		60.0%
github.com/julsemaan/anyfile-notepad/webserver/utils.go:19:					InfoPrint			100.0%
github.com/julsemaan/anyfile-notepad/webserver/utils.go:24:					ErrPrint			100.0%
github.com/julsemaan/anyfile-notepad/webserver/utils.go:29:					EnvOrDefault			100.0%
total:												(statements)			73.1%

@github-actions
Copy link
Copy Markdown

Go coverage report (api)

github.com/julsemaan/anyfile-notepad/api/cmd/api/main.go:8:				main				0.0%
github.com/julsemaan/anyfile-notepad/api/internal/app/config.go:24:			LoadConfigFromEnv		100.0%
github.com/julsemaan/anyfile-notepad/api/internal/app/config.go:51:			loadMaxContactRequestsPerDay	100.0%
github.com/julsemaan/anyfile-notepad/api/internal/app/email.go:17:			sendEmailWithOptionalTLS	100.0%
github.com/julsemaan/anyfile-notepad/api/internal/app/email.go:48:			sendMailWithTLSConfig		0.0%
github.com/julsemaan/anyfile-notepad/api/internal/app/run.go:21:			Run				0.0%
github.com/julsemaan/anyfile-notepad/api/internal/app/run.go:89:			logURL				100.0%
github.com/julsemaan/anyfile-notepad/api/internal/contact/service.go:39:		NewService			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/contact/service.go:48:		BeforeInsert			85.7%
github.com/julsemaan/anyfile-notepad/api/internal/contact/service.go:73:		AfterInsert			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/contact/service.go:87:		buildMessage			70.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/handler_stats.go:17:		NewStatsHandler			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/middleware_access_log.go:14:	WriteHeader			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/middleware_access_log.go:19:	withAccessLog			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/middleware_auth.go:10:	IsOpenResource			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/middleware_auth.go:30:	Authenticate			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/middleware_cors.go:5:		withCORS			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/httpapi/router.go:10:			NewRouter			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/logging/logging.go:12:		output				100.0%
github.com/julsemaan/anyfile-notepad/api/internal/logging/logging.go:16:		Error				60.0%
github.com/julsemaan/anyfile-notepad/api/internal/logging/logging.go:26:		Errorf				100.0%
github.com/julsemaan/anyfile-notepad/api/internal/logging/logging.go:30:		Fatalf				100.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/index.go:13:		BuildIndex			0.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/schema.go:19:		MimeTypeSchema			0.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/schema.go:43:		ExtensionSchema			0.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/schema.go:72:		SyntaxSchema			0.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/schema.go:91:		SettingSchema			0.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/schema.go:110:		ContactRequestSchema		0.0%
github.com/julsemaan/anyfile-notepad/api/internal/resources/schema.go:136:		Validate			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/stats/prometheus.go:23:		NewPrometheusMetrics		0.0%
github.com/julsemaan/anyfile-notepad/api/internal/stats/prometheus.go:27:		IncrementStatsHits		0.0%
github.com/julsemaan/anyfile-notepad/api/internal/stats/prometheus.go:31:		IncrementKey			0.0%
github.com/julsemaan/anyfile-notepad/api/internal/stats/service.go:56:			NewService			100.0%
github.com/julsemaan/anyfile-notepad/api/internal/stats/service.go:60:			ParsePayload			89.5%
github.com/julsemaan/anyfile-notepad/api/internal/stats/service.go:92:			Record				83.3%
github.com/julsemaan/anyfile-notepad/api/internal/stats/service.go:106:			normalizeIncrementMetricKey	100.0%
github.com/julsemaan/anyfile-notepad/api/internal/stats/service.go:120:			extractIP			60.0%
total:											(statements)			67.2%

@julsemaan julsemaan requested a review from Copilot May 4, 2026 02:08
@julsemaan
Copy link
Copy Markdown
Owner Author

Addressed.

Changes made:

  • api/internal/app/config.go: changed the default metrics bind address from 127.0.0.1:9090 to :9090 so scrapes work by default outside local-only setups.
  • api/internal/app/run.go: made the metrics server best-effort. Metrics startup/runtime errors are now logged, while only the API listener remains fatal.
  • api/internal/stats/service.go: kept the existing other bucket but now log unknown increment keys explicitly so frontend/backend drift is visible instead of silent.

Regression coverage:

  • updated config test for the new default listen address
  • added serveMetrics / serveAPI tests covering non-fatal metrics startup errors vs fatal API listener errors
  • added a stats test asserting unknown keys are logged

Validation:

  • cd api && go test ./...

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates the API stats/metrics pipeline from StatsD-style counters to Prometheus, adding a dedicated /metrics HTTP server and tightening how increment keys are normalized/bucketed to control label cardinality.

Changes:

  • Add Prometheus counters for stats hits and increment events, and expose them via a new metrics server (/metrics) on a configurable listen address.
  • Update stats recording to increment a hits counter per accepted payload and bucket/allowlist increment keys (with a fallback other bucket).
  • Update docker dev compose and tests to cover the new metrics behavior and configuration.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
docker4dev/docker-compose.yaml Exposes the Prometheus metrics port and sets AFN_METRICS_LISTEN_ADDR.
api/internal/stats/service.go Changes metrics interface + normalizes/buckets increment keys; always increments “hits”.
api/internal/stats/service_test.go Updates tests for new metrics interface and key bucketing behavior.
api/internal/stats/prometheus.go Adds Prometheus-backed Metrics implementation and counters.
api/internal/app/run.go Runs two HTTP servers (API + Prometheus /metrics) concurrently.
api/internal/app/run_test.go Adds unit tests for URL logging helper used by the servers.
api/internal/app/config.go (+ config_test.go) Adds MetricsListenAddr config/env with default and tests.
api/go.mod + api/go.sum Adds Prometheus deps and updates module metadata (but currently inconsistent).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread api/go.mod
Comment on lines 5 to 10
require (
github.com/julsemaan/anyfile-notepad/utils v0.0.0-20230202010526-481b7f9b59a2
github.com/julsemaan/rest-layer-file v0.0.0-20230518012330-1c28ed9eb6a7
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.23.2
github.com/rs/rest-layer v0.0.0-20160505213648-cb84bc79b5b8
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Removed the unused github.com/julsemaan/anyfile-notepad/utils requirement from api/go.mod. I also re-ran cd api && go test ./... and confirmed the api module graph no longer includes that dependency.

Comment thread api/internal/app/run.go
Comment on lines +79 to +86
for {
err := <-errCh
if errors.Is(err, http.ErrServerClosed) {
continue
}

return err
}
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Verified this is already addressed in the current branch. Run no longer waits on the old error loop: metrics now run in a goroutine via serveMetrics, and serveAPI returns directly while treating http.ErrServerClosed as a clean shutdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants