Skip to content

Commit

Permalink
refactor: Telemetry full log
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed May 28, 2023
1 parent a28ea08 commit 61ed745
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 8 additions & 3 deletions xmcl-runtime/lib/managers/LogManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { createWriteStream, WriteStream } from 'fs'
import { ensureDir } from 'fs-extra/esm'
import { readFile } from 'fs/promises'
import { basename, join, resolve } from 'path'
import { PassThrough, pipeline, Transform } from 'stream'
import { EventEmitter, PassThrough, pipeline, Transform } from 'stream'
import { format } from 'util'
import { Manager } from '.'
import LauncherApp from '../app/LauncherApp'
import { IS_DEV } from '../constant'
import { filterSensitiveData } from '../util/complaince'
import { Logger } from '../util/log'
import { ZipTask } from '../util/zip'
import { filterSensitiveData } from '../util/complaince'

function formatMsg(message: any, options: any[]) { return options.length !== 0 ? format(message, ...options.map(filterSensitiveData)) : format(message) }
function baseTransform(tag: string) { return new Transform({ transform(c, e, cb) { cb(undefined, `[${tag}] [${new Date().toLocaleString()}] ${c}`) } }) }
Expand All @@ -26,6 +26,8 @@ export default class LogManager extends Manager {

private hasError = false

readonly logBus = new EventEmitter()

constructor(app: LauncherApp) {
super(app)

Expand Down Expand Up @@ -76,17 +78,20 @@ export default class LogManager extends Manager {
}

getLogger(tag: string): Logger {
const { loggerEntries } = this
const { loggerEntries, logBus } = this
return {
log(message: any, ...options: any[]) {
logBus.emit('log', tag, formatMsg(message, options))
loggerEntries.log.write(`[${tag}] ${formatMsg(message, options)}`)
},
warn(message: any, ...options: any[]) {
if (message instanceof Error) { message = message.stack }
logBus.emit('warn', tag, formatMsg(message, options))
loggerEntries.warn.write(`[${tag}] ${formatMsg(message, options)}`)
},
error(message: any, ...options: any[]) {
if (message instanceof Error) { message = message.stack }
logBus.emit('error', tag, formatMsg(message, options))
loggerEntries.error.write(`[${tag}] ${formatMsg(message, options)}`)
},
}
Expand Down
34 changes: 33 additions & 1 deletion xmcl-runtime/lib/plugins/pluginTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
const tags = appInsight.defaultClient.context.tags
tags[contract.sessionId] = sessionId
tags[contract.userId] = sessionId
tags[contract.applicationVersion] = `${app.version}#${app.build}`
tags[contract.applicationVersion] = IS_DEV ? '0.0.0' : `${app.version}#${app.build}`
tags[contract.operationParentId] = 'root'

app.on('engine-ready', () => {
const baseService = app.serviceManager.get(BaseService)
Expand Down Expand Up @@ -83,6 +84,37 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
}
})

app.logManager.logBus.on('log', (tag, message) => {
if (baseService.state.disableTelemetry) return
appInsight.defaultClient.trackTrace({
message,
severity: appInsight.Contracts.SeverityLevel.Information,
tagOverrides: {
[contract.operationParentId]: tag,
},
})
})
app.logManager.logBus.on('error', (tag, message) => {
if (baseService.state.disableTelemetry) return
appInsight.defaultClient.trackTrace({
message,
severity: appInsight.Contracts.SeverityLevel.Error,
tagOverrides: {
[contract.operationParentId]: tag,
},
})
})
app.logManager.logBus.on('warn', (tag, message) => {
if (baseService.state.disableTelemetry) return
appInsight.defaultClient.trackTrace({
message,
severity: appInsight.Contracts.SeverityLevel.Warning,
tagOverrides: {
[contract.operationParentId]: tag,
},
})
})

app.serviceManager.get(UserService).on('user-login', (authService) => {
if (baseService.state.disableTelemetry) return
appInsight.defaultClient.trackEvent({
Expand Down

0 comments on commit 61ed745

Please sign in to comment.