Skip to content

Commit

Permalink
chore(runtime): Remove assumptions about deployment environment (#222)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dnys1 authored Oct 16, 2024
1 parent 5fa2e2a commit 17b76ba
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 45 deletions.
4 changes: 4 additions & 0 deletions packages/celest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
6 changes: 6 additions & 0 deletions packages/celest/lib/src/config/config_values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)';

Expand Down
9 changes: 7 additions & 2 deletions packages/celest/lib/src/core/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 0 additions & 24 deletions packages/celest/lib/src/runtime/gcp/gcp.dart

This file was deleted.

14 changes: 4 additions & 10 deletions packages/celest/lib/src/runtime/http/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
Expand All @@ -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<String, Object?> get sourceLocation => {
Expand Down
9 changes: 1 addition & 8 deletions packages/celest/lib/src/runtime/serve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -46,13 +45,7 @@ Future<CelestService> 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);
Expand Down
2 changes: 1 addition & 1 deletion packages/celest/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 17b76ba

Please sign in to comment.