Commit 575bcf68 authored by Annie Ke's avatar Annie Ke Committed by GitHub

feat[dtl]: add node env (#735)

* move verbose log to debug

* feat: add node env to dtl

* changeset and new line

* conditional sentry and metrics initialization
parent 8168765e
---
'@eth-optimism/common-ts': patch
'@eth-optimism/data-transport-layer': patch
---
add environment and network to dtl, move metric init to app from base-service
...@@ -26,7 +26,6 @@ export class BaseService<T> { ...@@ -26,7 +26,6 @@ export class BaseService<T> {
this.name = name this.name = name
this.options = mergeDefaultOptions(options, optionSettings) this.options = mergeDefaultOptions(options, optionSettings)
this.logger = new Logger({ name }) this.logger = new Logger({ name })
this.metrics = new Metrics({ prefix: name })
} }
/** /**
...@@ -39,9 +38,7 @@ export class BaseService<T> { ...@@ -39,9 +38,7 @@ export class BaseService<T> {
this.logger.info('Service is initializing...') this.logger.info('Service is initializing...')
await this._init() await this._init()
this.logger.info('Service has initialized.', { this.logger.info('Service has initialized.')
options: this.options,
})
this.initialized = true this.initialized = true
} }
......
# General options # General options
DATA_TRANSPORT_LAYER__NODE_ENV=development
# Leave blank during local development
DATA_TRANSPORT_LAYER__ETH_NETWORK_NAME=
DATA_TRANSPORT_LAYER__DB_PATH=./db DATA_TRANSPORT_LAYER__DB_PATH=./db
DATA_TRANSPORT_LAYER__ADDRESS_MANAGER= DATA_TRANSPORT_LAYER__ADDRESS_MANAGER=
DATA_TRANSPORT_LAYER__POLLING_INTERVAL=5000 DATA_TRANSPORT_LAYER__POLLING_INTERVAL=5000
...@@ -26,3 +29,4 @@ DATA_TRANSPORT_LAYER__LEGACY_SEQUENCER_COMPATIBILITY=false ...@@ -26,3 +29,4 @@ DATA_TRANSPORT_LAYER__LEGACY_SEQUENCER_COMPATIBILITY=false
# Monitoring # Monitoring
# Leave the SENTRY_DSN variable unset during local development # Leave the SENTRY_DSN variable unset during local development
DATA_TRANSPORT_LAYER__SENTRY_DSN= DATA_TRANSPORT_LAYER__SENTRY_DSN=
DATA_TRANSPORT_LAYER__SENTRY_TRACE_RATE=
...@@ -10,6 +10,9 @@ import { validators } from '../../utils' ...@@ -10,6 +10,9 @@ import { validators } from '../../utils'
import { L2IngestionService } from '../l2-ingestion/service' import { L2IngestionService } from '../l2-ingestion/service'
export interface L1DataTransportServiceOptions { export interface L1DataTransportServiceOptions {
nodeEnv: string
ethNetworkName?: 'mainnet' | 'kovan' | 'goerli'
release: string
addressManager: string addressManager: string
confirmations: number confirmations: number
dangerouslyCatchAllErrors?: boolean dangerouslyCatchAllErrors?: boolean
...@@ -26,6 +29,7 @@ export interface L1DataTransportServiceOptions { ...@@ -26,6 +29,7 @@ export interface L1DataTransportServiceOptions {
transactionsPerPollingInterval: number transactionsPerPollingInterval: number
legacySequencerCompatibility: boolean legacySequencerCompatibility: boolean
sentryDsn?: string sentryDsn?: string
sentryTraceRate?: number
defaultBackend: string defaultBackend: string
} }
......
...@@ -10,8 +10,10 @@ interface Bcfg { ...@@ -10,8 +10,10 @@ interface Bcfg {
str: (name: string, defaultValue?: string) => string str: (name: string, defaultValue?: string) => string
uint: (name: string, defaultValue?: number) => number uint: (name: string, defaultValue?: number) => number
bool: (name: string, defaultValue?: boolean) => boolean bool: (name: string, defaultValue?: boolean) => boolean
ufloat: (name: string, defaultValue?: number) => number
} }
type ethNetwork = 'mainnet' | 'kovan' | 'goerli'
;(async () => { ;(async () => {
try { try {
dotenv.config() dotenv.config()
...@@ -23,6 +25,9 @@ interface Bcfg { ...@@ -23,6 +25,9 @@ interface Bcfg {
}) })
const service = new L1DataTransportService({ const service = new L1DataTransportService({
nodeEnv: config.str('node-env', 'development'),
ethNetworkName: config.str('eth-network-name') as ethNetwork,
release: `data-transport-layer@${process.env.npm_package_version}`,
dbPath: config.str('db-path', './db'), dbPath: config.str('db-path', './db'),
port: config.uint('server-port', 7878), port: config.uint('server-port', 7878),
hostname: config.str('server-hostname', 'localhost'), hostname: config.str('server-hostname', 'localhost'),
...@@ -49,6 +54,7 @@ interface Bcfg { ...@@ -49,6 +54,7 @@ interface Bcfg {
), ),
defaultBackend: config.str('default-backend', 'l1'), defaultBackend: config.str('default-backend', 'l1'),
sentryDsn: config.str('sentry-dsn'), sentryDsn: config.str('sentry-dsn'),
sentryTraceRate: config.ufloat('sentry-trace-rate', 0.05),
}) })
await service.start() await service.start()
......
/* Imports: External */ /* Imports: External */
import { BaseService } from '@eth-optimism/common-ts' import { BaseService, Metrics } from '@eth-optimism/common-ts'
import express, { Request, Response } from 'express' import express, { Request, Response } from 'express'
import promBundle from 'express-prom-bundle' import promBundle from 'express-prom-bundle'
import cors from 'cors' import cors from 'cors'
...@@ -106,29 +106,50 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> { ...@@ -106,29 +106,50 @@ export class L1TransportServer extends BaseService<L1TransportServerOptions> {
private _initializeApp() { private _initializeApp() {
// TODO: Maybe pass this in as a parameter instead of creating it here? // TODO: Maybe pass this in as a parameter instead of creating it here?
this.state.app = express() this.state.app = express()
if (this.options.ethNetworkName) {
this._initMonitoring()
}
this.state.app.use(cors())
this._registerAllRoutes()
// Sentry error handling must be after all controllers
// and before other error middleware
if (this.options.ethNetworkName) {
this.state.app.use(Sentry.Handlers.errorHandler())
}
}
/**
* Initialize Sentry and Prometheus metrics for deployed instances
*/
private _initMonitoring() {
// Init Sentry options // Init Sentry options
Sentry.init({ Sentry.init({
dsn: this.options.sentryDsn, dsn: this.options.sentryDsn,
release: `data-transport-layer@${process.env.npm_package_version}`, release: this.options.release,
integrations: [ integrations: [
new Sentry.Integrations.Http({ tracing: true }), new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ new Tracing.Integrations.Express({
app: this.state.app, app: this.state.app,
}), }),
], ],
tracesSampleRate: 0.05, tracesSampleRate: this.options.sentryTraceRate,
}) })
this.state.app.use(Sentry.Handlers.requestHandler()) this.state.app.use(Sentry.Handlers.requestHandler())
this.state.app.use(Sentry.Handlers.tracingHandler()) this.state.app.use(Sentry.Handlers.tracingHandler())
// Init metrics // Init metrics
this.metrics = new Metrics({
prefix: this.name,
labels: {
environment: this.options.nodeEnv,
network: this.options.ethNetworkName,
release: this.options.release,
},
})
const metricsMiddleware = promBundle({ const metricsMiddleware = promBundle({
includeMethod: true, includeMethod: true,
includePath: true, includePath: true,
}) })
this.state.app.use(metricsMiddleware) this.state.app.use(metricsMiddleware)
this.state.app.use(cors())
this._registerAllRoutes()
this.state.app.use(Sentry.Handlers.errorHandler())
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment