From aad55142de7596db0a9c4428066e0ee3de3c2521 Mon Sep 17 00:00:00 2001 From: Dillon Nys Date: Tue, 15 Oct 2024 16:58:19 -0700 Subject: [PATCH] chore(runtime): Remove assumptions about deployment environment Now that we support self-hosting, remove assumptions that we'll be in GCP when deployed. And namespace our Google metadata so that we don't use the metadata projects not running in GCP via Celest Cloud. --- packages/celest/CHANGELOG.md | 4 ++++ .../celest/lib/src/config/config_values.dart | 6 +++++ packages/celest/lib/src/core/context.dart | 9 +++++-- packages/celest/lib/src/runtime/gcp/gcp.dart | 24 ------------------- .../celest/lib/src/runtime/http/logging.dart | 14 ++++------- packages/celest/lib/src/runtime/serve.dart | 9 +------ packages/celest/pubspec.yaml | 2 +- 7 files changed, 23 insertions(+), 45 deletions(-) delete mode 100644 packages/celest/lib/src/runtime/gcp/gcp.dart diff --git a/packages/celest/CHANGELOG.md b/packages/celest/CHANGELOG.md index 10c49db..38825e0 100644 --- a/packages/celest/CHANGELOG.md +++ b/packages/celest/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.1-wip + +- chore: Remove assumptions about where the project is deployed + ## 1.0.0 The first release of Celest V1! This release includes: diff --git a/packages/celest/lib/src/config/config_values.dart b/packages/celest/lib/src/config/config_values.dart index 9b7c61c..2329a32 100644 --- a/packages/celest/lib/src/config/config_values.dart +++ b/packages/celest/lib/src/config/config_values.dart @@ -66,6 +66,12 @@ final class env extends ConfigurationValue { /// For example, `production`. static const env environment = env('CELEST_ENVIRONMENT'); + /// The GCP project ID, when running in Celest Cloud. + /// + /// This is only set when running in Celest Cloud but may be specified + /// manually to enable integration with Google Cloud services. + static const env googleProjectId = env('CELEST_GOOGLE_CLOUD_PROJECT'); + @override String toString() => 'env($name)'; diff --git a/packages/celest/lib/src/core/context.dart b/packages/celest/lib/src/core/context.dart index a2407a4..f44340d 100644 --- a/packages/celest/lib/src/core/context.dart +++ b/packages/celest/lib/src/core/context.dart @@ -3,7 +3,6 @@ import 'dart:io' show HandshakeException, HttpClient, SocketException; import 'package:celest/src/config/config_values.dart'; import 'package:celest/src/core/environment.dart'; -import 'package:celest/src/runtime/gcp/gcp.dart'; import 'package:celest_ast/celest_ast.dart'; import 'package:celest_core/_internal.dart'; // ignore: implementation_imports @@ -111,7 +110,13 @@ final class Context { get(ContextKey.fileSystem) ?? const LocalFileSystem(); /// Whether Celest is running in the cloud. - bool get isRunningInCloud => root.get(googleCloudProjectKey) != null; + bool get isRunningInCloud => get(env.googleProjectId) != null; + + /// The Google project ID for the current context. + /// + /// This will be set when running in Celest Cloud and will be `null` otherwise + /// unless explicitly set in the environment. + String? get googleProjectId => get(env.googleProjectId); /// The shelf [shelf.Request] object which triggered the current function invocation. shelf.Request get currentRequest => expect(ContextKey.currentRequest); diff --git a/packages/celest/lib/src/runtime/gcp/gcp.dart b/packages/celest/lib/src/runtime/gcp/gcp.dart deleted file mode 100644 index f9d726e..0000000 --- a/packages/celest/lib/src/runtime/gcp/gcp.dart +++ /dev/null @@ -1,24 +0,0 @@ -@internal -library; - -import 'package:celest/src/core/context.dart'; -import 'package:celest_core/_internal.dart'; -import 'package:google_cloud/google_cloud.dart'; -import 'package:logging/logging.dart'; -import 'package:meta/meta.dart'; - -/// The context key for the active GCP project ID. -const ContextKey googleCloudProjectKey = ContextKey(); - -/// Returns the GCP project ID for the active environment or -/// `null` if running locally. -Future googleCloudProject() async { - try { - return await computeProjectId(); - } on Object catch (e, st) { - if (kReleaseMode) { - Logger.root.warning('Failed to get GCP project ID', e, st); - } - return null; - } -} diff --git a/packages/celest/lib/src/runtime/http/logging.dart b/packages/celest/lib/src/runtime/http/logging.dart index c51dba5..57c91bf 100644 --- a/packages/celest/lib/src/runtime/http/logging.dart +++ b/packages/celest/lib/src/runtime/http/logging.dart @@ -7,7 +7,6 @@ import 'dart:developer' as developer; import 'dart:io'; import 'package:celest/src/core/context.dart'; -import 'package:celest/src/runtime/gcp/gcp.dart'; import 'package:celest_core/_internal.dart'; import 'package:cloud_http/cloud_http.dart'; import 'package:logging/logging.dart'; @@ -91,8 +90,10 @@ extension on LogRecord { 'message': message.toString().trim(), 'severity': severity, 'timestamp': time.toIso8601String(), - if (trace != null) ...{ - 'logging.googleapis.com/trace': trace.cloudTraceId, + if ((trace, Context.root.googleProjectId) + case (final trace?, final googleProjectId?)) ...{ + 'logging.googleapis.com/trace': + 'projects/$googleProjectId/traces/${trace.parentId}', 'logging.googleapis.com/spanId': trace.parentId, 'logging.googleapis.com/trace_sampled': trace.traceFlagSampled != 0, }, @@ -103,13 +104,6 @@ extension on LogRecord { } } -extension on Traceparent { - String get cloudTraceId { - final projectId = Context.root.expect(googleCloudProjectKey); - return 'projects/$projectId/traces/$parentId'; - } -} - extension on Frame { // https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogEntrySourceLocation Map get sourceLocation => { diff --git a/packages/celest/lib/src/runtime/serve.dart b/packages/celest/lib/src/runtime/serve.dart index c434d24..f4ef8f6 100644 --- a/packages/celest/lib/src/runtime/serve.dart +++ b/packages/celest/lib/src/runtime/serve.dart @@ -8,7 +8,6 @@ import 'package:async/async.dart'; import 'package:celest/celest.dart'; import 'package:celest/src/core/context.dart'; import 'package:celest/src/runtime/configuration.dart'; -import 'package:celest/src/runtime/gcp/gcp.dart'; import 'package:celest/src/runtime/http/cloud_middleware.dart'; import 'package:celest/src/runtime/http/middleware.dart'; import 'package:celest/src/runtime/json_utils.dart'; @@ -46,13 +45,7 @@ Future serve({ }) async { Context.root = Context.of(Zone.current); - await configure( - config: config, - ); - final projectId = await googleCloudProject(); - if (projectId != null) { - Context.root.put(googleCloudProjectKey, projectId); - } + await configure(config: config); final router = Router(); Context.root.put(ContextKey.router, router); diff --git a/packages/celest/pubspec.yaml b/packages/celest/pubspec.yaml index 8357c77..4b09d37 100644 --- a/packages/celest/pubspec.yaml +++ b/packages/celest/pubspec.yaml @@ -1,6 +1,6 @@ name: celest description: The Flutter cloud platform. Celest enables you to build your entire backend in Dart. -version: 1.0.0 +version: 1.0.1-wip homepage: https://celest.dev repository: https://github.com/celest-dev/celest/tree/main/packages/celest