mirror of
https://github.com/expo/turtle.git
synced 2026-11-14 19:12:55 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da7eb28102 |
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
node_modules
|
||||
yarn-error.log
|
||||
google-cloud-credentials.json
|
||||
|
||||
/build/
|
||||
/workingdir/*
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import EventEmitter from 'events';
|
||||
|
||||
import { IJob } from 'turtle/job';
|
||||
|
||||
// type error when using import
|
||||
// https://github.com/googleapis/nodejs-logging-bunyan/issues/241
|
||||
// tslint:disable-next-line:no-var-requires
|
||||
const { LoggingBunyan } = require('@google-cloud/logging-bunyan');
|
||||
|
||||
export interface IGCloudConfig {
|
||||
name: string;
|
||||
resource: {
|
||||
type: string;
|
||||
labels: {
|
||||
node_id: string;
|
||||
location: string;
|
||||
namespace: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
interface IGCloudExtraFields {
|
||||
jobID: string;
|
||||
experienceName: string;
|
||||
}
|
||||
|
||||
export default class GCloudStream extends EventEmitter {
|
||||
private logsStream: any;
|
||||
private extraFields: IGCloudExtraFields | null;
|
||||
|
||||
constructor(gcloudConfig: IGCloudConfig) {
|
||||
super();
|
||||
|
||||
const { name, resource } = gcloudConfig;
|
||||
this.logsStream = new LoggingBunyan({ name, resource });
|
||||
this.extraFields = null;
|
||||
}
|
||||
|
||||
public init(job: IJob) {
|
||||
this.extraFields = {
|
||||
jobID: job.id,
|
||||
experienceName: job.experienceName,
|
||||
};
|
||||
}
|
||||
|
||||
public cleanup() {
|
||||
this.extraFields = null;
|
||||
}
|
||||
|
||||
public write(rec: any) {
|
||||
const { msg: message, ...other } = rec;
|
||||
this.logsStream.write({
|
||||
message,
|
||||
...other,
|
||||
data: this.extraFields || {},
|
||||
});
|
||||
}
|
||||
}
|
||||
+23
-12
@@ -6,14 +6,10 @@ import { Client as RavenClient } from 'raven';
|
||||
import config from 'turtle/config';
|
||||
import * as constants from 'turtle/constants/logger';
|
||||
import { IJob } from 'turtle/job';
|
||||
import GCloudStream from 'turtle/logger/gcloudStream';
|
||||
import S3Stream from 'turtle/logger/s3Stream';
|
||||
import { isOffline } from 'turtle/turtleContext';
|
||||
|
||||
// type error when using import
|
||||
// https://github.com/googleapis/nodejs-logging-bunyan/issues/241
|
||||
// tslint:disable-next-line:no-var-requires
|
||||
const { LoggingBunyan } = require('@google-cloud/logging-bunyan');
|
||||
|
||||
interface IStream {
|
||||
stream: any;
|
||||
type?: string;
|
||||
@@ -22,6 +18,7 @@ interface IStream {
|
||||
}
|
||||
|
||||
export const s3logger = new S3Stream();
|
||||
let gcloudStream: GCloudStream;
|
||||
|
||||
const streams: IStream[] = [];
|
||||
|
||||
@@ -51,15 +48,23 @@ if (config.sentry.dsn && config.deploymentEnv !== 'development') {
|
||||
}
|
||||
|
||||
if (config.google.credentials) {
|
||||
const resource = {
|
||||
type: 'generic_node',
|
||||
labels: {
|
||||
node_id: config.hostname,
|
||||
location: '', // default value
|
||||
namespace: '', // default value
|
||||
const gcloudConfig = {
|
||||
name: 'turtle',
|
||||
resource: {
|
||||
type: 'generic_node',
|
||||
labels: {
|
||||
node_id: config.hostname,
|
||||
location: '', // default value
|
||||
namespace: '', // default value
|
||||
},
|
||||
},
|
||||
};
|
||||
streams.push(new LoggingBunyan({ name: 'turtle', resource }).stream('info'));
|
||||
gcloudStream = new GCloudStream(gcloudConfig);
|
||||
streams.push({
|
||||
level: 'info',
|
||||
type: 'raw',
|
||||
stream: gcloudStream,
|
||||
});
|
||||
}
|
||||
|
||||
const logger = bunyan.createLogger({
|
||||
@@ -73,12 +78,18 @@ const logger = bunyan.createLogger({
|
||||
logger.withFields = (extraFields: any) => withFields(logger, extraFields);
|
||||
|
||||
logger.init = async (job: IJob) => {
|
||||
if (gcloudStream) {
|
||||
gcloudStream.init(job);
|
||||
}
|
||||
return await s3logger.init(job);
|
||||
};
|
||||
|
||||
logger.cleanup = async () => {
|
||||
// a little hacky, but works
|
||||
logger.info({ lastBuildLog: true }, 'this is the last log from this build');
|
||||
if (gcloudStream) {
|
||||
gcloudStream.cleanup();
|
||||
}
|
||||
await s3logger.waitForLogger();
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user