Cross-Cutting Principles Across Languages
Five concerns — logging, exception handling, tracing, metrics, and security — are never left to each feature to reinvent. Every language’s core lib (go-lang-core-lib, rust-lang-core-lib, node-lang-core-lib) implements the same principle with a different concrete mechanism, and every service/client app consumes that mechanism rather than hand-rolling its own. This doc is the cross-language map; the deep, per-mechanism detail for one language lives in that language’s own doc.
- Layered Architecture Across Languages
- Coding Standard: Go
- Coding Standard: Rust
- Coding Standard: Node (NestJS)
- Coding Standard: Flutter (ui-application)
- Coding Standard: React (ui-admin-application)
- Coding Standard — Deployment & Local Infrastructure
- Coding Standard — Audit Trail (Pub/Sub → document-service)
1. Logging — never a raw print, always trace-aware
The principle: every log line carries trace_id/span_id/correlation_id automatically, without the call site passing them explicitly. Never console.log/println!/fmt.Println.
| Language | Mechanism | Rule |
|---|---|---|
| Go | pkg/utils/log/ — LogInfoWithTrace/LogDebugWithTrace/LogWarnWithTrace/LogErrorWithTrace |
Reads trace_id/span_id/correlation_id off ctx automatically once any transport middleware has run. |
| Rust | log module — log_info!/log_debug!/log_warn!/log_error! macros |
Span-context-aware via FlatJsonFormat; call log_info! at entry/exit of every public service/repository method, log_error! before any AppError::Internal. |
| Node | node/core-logger.js (Node/NestJS), firebase/core-logger.js (Cloud Functions) |
Two separate copy-pasted implementations, not shared — a fix to one does not propagate to the other. |
Sensitive-data carve-out (Rust, generalizes everywhere): a value standing in for real secret material with no real provider behind it yet (e.g. a placeholder OTP) is debug-level only, never info-level, so it’s structurally absent from production logs (LOG_LEVEL=info).
2. Exception / error handling — typed, never ad hoc
The principle: a service never throws/returns a bare string error or native exception — every error is a typed value the framework already knows how to turn into the right HTTP/gRPC status.
| Language | Typed error module | Global handler |
|---|---|---|
| Go | pkg/utils/errors/ — FieldValidation, NotFound, ConfigError, DuplicateError, InternalError. Never errors.New(). |
EchoGlobalExceptionHandler (REST), GrpcGlobalInterceptor (gRPC). |
| Rust | error module — AppError enum + AppResult<T>, implements IntoResponse directly. |
grpc::error maps AppError → tonic::Status. |
| Node | core/errors.js typed errors. Coverage is inconsistent — some files still throw native Error (known gap, prefer the typed constructor anyway). |
node/exception-handler.js’s AllExceptionsFilter. |
3. Tracing — the same four identifiers, everywhere
The principle: one trace, one correlation id, follows a request across every hop, in every transport, in every language.
| Identifier | Answers | Crosses service boundaries? |
|---|---|---|
traceparent (+ tracestate) |
“Show me the whole distributed trace.” | Yes — REST header, gRPC metadata, Pub/Sub MessageHeader.traceparent. |
correlation_id |
“Show me every log line for this operation” — the one to put in a support ticket. | Yes — same three transports as traceparent. |
gateway_request_id |
Nginx’s own id for one REST hop. | No — REST-entry-point only, never gRPC, never Pub/Sub. |
request_id (Go’s Echo)/Rust’s request_logger equivalent |
A per-request UUID for one hop’s access log. | No — purely local to that service. |
| Language | Span helper (the “wrap every boundary” mechanism) |
|---|---|
| Go | otel.WithSpan[T](ctx, "SpanName", func(ctx) (T, error)), plus gowrap-generated tracing decorators — never hand-written. |
| Rust | #[tracing::instrument(skip(...))] on every public service/repository method — skip() every struct/reference/trait-object parameter. |
| Node | runWithRequestContext(name, fn, initialStore), backed by AsyncLocalStorage — hand-written at each layer boundary, no codegen equivalent of gowrap exists yet. |
| Flutter / React (browser/mobile clients) | An HTTP client interceptor injects traceparent + X-Correlation-ID on every outbound call — never X-Tenant-ID, since tenant scope comes from the Firebase JWT, decoded server-side by Nginx. |
4. Metrics — automatic, zero call-site changes
The principle: every service gets the same shape of histogram/gauge by using the shared constructor — a service never hand-rolls an equivalent metric.
| Language | Server-duration metric | DB pool metric |
|---|---|---|
| Go | http.server.request.duration (Echo), rpc.server.request.duration (gRPC interceptor) |
db.pgxpool.* gauges/counters |
| Rust | http.server.request.duration (Axum services only), rpc.server.request.duration (gRPC-only services, coarse ok/error status) |
db.sqlx_pool.size/num_idle gauges |
| Node | No server-side HTTP request-duration metric yet — a known, flagged gap, not silently assumed to exist | No Mongo/connection-pool metric yet |
Cardinality guardrail — identical rule, every language: no label may ever be tenant_id, correlation_id, or any other unbounded per-request value. Route/method templates only, never a raw path with an ID interpolated. Stated near-verbatim in all three core libs — a hard rule, not a style preference.
5. Security — the same auth contract, three backend languages
The principle: every protected route trusts exactly three headers, synthesized by Nginx from a verified Firebase JWT — never the request body, never re-derived downstream.
| Header | Claim | Go | Rust | Node |
|---|---|---|---|---|
X-Tenant-ID |
tenant_id |
auth.GetTenantID(ctx) |
HeaderAuthClaims (rust_lang_core_lib::auth) |
req.tenantId (authMiddleware) |
X-User-Id |
user_id |
auth.GetUserID(ctx) |
same extractor | req.userId |
X-Role |
role |
auth.GetRole(ctx) |
same extractor | req.role |
All three fields are mandatory in every language — the extractor rejects (401) if any is absent. Never trust tenant_id/user_id/role from a request body or a gRPC payload — only from the header/metadata contract above.
gRPC metadata propagation: every outbound RPC carries the same three values as gRPC metadata, sourced from the calling service’s own auth context — Go’s withAuthMetadata(ctx) pattern, Rust’s extract_auth_claims(request.metadata()). Never re-derived, never trusted from the payload.
Client apps never send these headers themselves. ui-application/ui-admin-application carry tenant scope inside the Firebase JWT; Nginx’s /_auth_verify subrequest decodes it and injects X-Tenant-Id upstream. React’s own rule generalizes to Flutter too: never inject X-Tenant-ID client-side, never pass tenantId through a component tree as a shortcut around the auth-store → controller → service → repository chain.
Firebase App Check is enforced once at the Nginx gateway level (auth_request pattern) for every backend language — not a per-service, per-route check — with a client-side mirror in both Flutter and React (FirebaseAppCheck.instance.activate(...) / initializeAppCheck(...)), using the same debug-token value across all three services in non-production environments.
Multi-tenancy — the one rule underneath all five
TenantID must be propagated at every service boundary — a DTO field, a gRPC metadata key, a Pub/Sub envelope field, a log line — never omitted. This is what ties logging, tracing, metrics-cardinality, and security together: the same identifier that must never become a metric label (the cardinality guardrail in section 4 above) is the same identifier every auth extractor above treats as mandatory.