-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add telemetry information to supabase
- Loading branch information
1 parent
35e675e
commit ddfa7d6
Showing
13 changed files
with
287 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env bash | ||
|
||
infra_file="/tmp/infra.json" | ||
|
||
## Get cloudProvider details | ||
function get_cloud_provider() { | ||
release_details=$(uname -r || echo "") | ||
if [[ $release_details == *"amzn"* ]]; then | ||
cloud_provider="amazon" | ||
elif [[ $release_details == *"azure"* ]]; then | ||
cloud_provider="azure" | ||
elif [[ $release_details == *"cloud"* ]]; then | ||
cloud_provider="gcp" | ||
elif [[ $release_details == *"generic"* ]]; then | ||
cloud_provider="digitalocean" | ||
elif [[ $release_details == *"ecs"* ]]; then | ||
cloud_provider="alibaba" | ||
elif [[ -n "${DYNO:-}" ]]; then | ||
cloud_provider="heroku" | ||
else | ||
cloud_provider="others(including local)" | ||
fi | ||
} | ||
|
||
## Get deployment tool details | ||
function get_tool() { | ||
if [[ -z "${KUBERNETES_SERVICE_HOST:-}" ]]; then | ||
dep_tool="likely docker" | ||
else | ||
dep_tool="kubernetes" | ||
fi | ||
} | ||
|
||
## Check hostname | ||
function get_hostname() { | ||
if [[ -f /etc/hostname ]]; then | ||
hostname="$(cat /etc/hostname)" | ||
else | ||
hostname="$(hostname || echo "unknown")" | ||
fi | ||
} | ||
|
||
## Get current Time | ||
function get_current_time() { | ||
currentTime="$(date -u -Iseconds || echo "unknown")" | ||
} | ||
|
||
## Check if it's an ECS Fargate deployment | ||
function check_for_fargate() { | ||
if [[ $cloud_provider == "amazon" && $dep_tool == "likely docker" ]]; then | ||
dep_tool="ecs-fargate" | ||
fi | ||
} | ||
|
||
## Main Block | ||
get_cloud_provider || true | ||
get_tool || true | ||
get_hostname || true | ||
check_for_fargate || true | ||
get_current_time || true | ||
|
||
infra_json='{"cloudProvider":"'"${cloud_provider}"'","tool":"'"${dep_tool}"'","hostname":"'"${hostname}"'","currentTime":"'"${currentTime}"'"}' | ||
echo "${infra_json}" | ||
|
||
echo "${infra_json}" > "${infra_file}" | ||
|
||
exec dumb-init npm run prod | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 16 additions & 9 deletions
25
services/workflows-service/src/auth/local/local-auth.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { CanActivate, Injectable, ExecutionContext } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import type { Request } from 'express'; | ||
import { SupabaseService } from '../../supabase/supabase.service'; | ||
|
||
export class LocalAuthGuard extends AuthGuard('local') { | ||
async canActivate(context: ExecutionContext) { | ||
const result = await super.canActivate(context); | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
|
||
await super.logIn(request); | ||
@Injectable() | ||
export class LocalAuthGuard extends AuthGuard('local') implements CanActivate { | ||
constructor(private readonly supabaseService: SupabaseService) { | ||
super(); | ||
} | ||
|
||
return result as boolean; | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const result = super.canActivate(context) as boolean; | ||
const request = context.switchToHttp().getRequest(); | ||
if (result && request.user) { | ||
const fullUrl = request.protocol + '://' + request.get('host') + request.originalUrl; | ||
this.supabaseService.logSignIn(fullUrl); | ||
} | ||
super.logIn(request); | ||
return Promise.resolve(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
services/workflows-service/src/supabase/mock-supabase.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ISupabaseService } from './types'; | ||
|
||
@Injectable() | ||
export class MockSupabaseService implements ISupabaseService { | ||
private readonly logger = new Logger(MockSupabaseService.name); | ||
|
||
async logSignIn(url: string): Promise<void> { | ||
this.logger.debug(`Mock log: Sign-in recorded for URL: ${url}`); | ||
} | ||
|
||
async logInfraData(infradata: JSON): Promise<void> { | ||
this.logger.debug(`Mock log: ${infradata} Infra data logged`); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
services/workflows-service/src/supabase/supabase.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SupabaseService } from './supabase.service'; | ||
import { SentryModule } from '@/sentry/sentry.module'; | ||
|
||
@Module({ | ||
providers: [SupabaseService], | ||
exports: [SupabaseService], | ||
imports: [SentryModule], | ||
}) | ||
export class SupabaseModule {} |
77 changes: 77 additions & 0 deletions
77
services/workflows-service/src/supabase/supabase.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { createClient, SupabaseClient } from '@supabase/supabase-js'; | ||
import { ISupabaseService } from './types'; | ||
import { SentryService } from '../sentry/sentry.service'; | ||
|
||
@Injectable() | ||
export class SupabaseService implements ISupabaseService { | ||
private readonly supabaseClient!: SupabaseClient; | ||
private readonly logger = new Logger(SupabaseService.name); | ||
|
||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly SentryService: SentryService, | ||
) { | ||
const telemetryEnabled = this.configService.get('TELEMETRY_ENABLED'); | ||
const supabaseUrl = this.configService.get<string>('TELEMETRY_SUPABASE_URL'); | ||
const supabaseApiKey = this.configService.get<string>('TELEMETRY_SUPABASE_API_KEY'); | ||
if (telemetryEnabled) { | ||
if (!supabaseUrl || !supabaseApiKey) { | ||
throw new Error('Supabase URL or API key is missing in configuration'); | ||
} else { | ||
this.supabaseClient = createClient(supabaseUrl, supabaseApiKey, { | ||
db: { schema: 'public' }, | ||
}); | ||
this.logger.log('Supabase client initialized.'); | ||
// This log is created as part of the entrypoint script | ||
// which gather details of the infrastructure. | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const infradata = require('/tmp/infra.json'); | ||
void this.logInfraData(infradata); | ||
} catch (error: Error | any) { | ||
if (error.code === 'MODULE_NOT_FOUND') { | ||
this.logger.error(`file not present: ${error.message}`); | ||
this.SentryService.captureException(error.message); | ||
} else { | ||
this.logger.error(`Exception infra data not present: ${error.message}`); | ||
} | ||
} | ||
} | ||
} else { | ||
this.logger.log('Telemetry disabled.'); | ||
} | ||
} | ||
|
||
async logSignIn(url: string): Promise<void> { | ||
try { | ||
const { data, error } = await this.supabaseClient.from('logins').insert([{ url }]); | ||
|
||
if (error) { | ||
this.logger.error(`Failed to log sign-in: ${error.message}`); | ||
} else { | ||
this.logger.log(`Sign-in logged successfully: ${data}`); | ||
} | ||
} catch (error: Error | any) { | ||
this.logger.error(`Exception to log sign in data: ${error.message}`); | ||
this.SentryService.captureException(error.message); | ||
} | ||
} | ||
|
||
async logInfraData(infradata: JSON): Promise<void> { | ||
try { | ||
const { data, error } = await this.supabaseClient.from('infra').insert([infradata]); | ||
|
||
if (error) { | ||
this.logger.error(`Failed to log infra data: ${error.message}`); | ||
} else { | ||
this.logger.log(`logged infra data successfully: ${data}`); | ||
} | ||
} catch (error: Error | any) { | ||
this.logger.error(`Exception to log infra data: ${error.message}`); | ||
this.SentryService.captureException(error.message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ISupabaseService { | ||
logSignIn(url: string): Promise<void>; | ||
logInfraData(infradata: JSON): Promise<void>; | ||
} |