Skip to content

Commit

Permalink
feat: Configure runtime container for Railway (#54)
Browse files Browse the repository at this point in the history
Adds detection for the Railway environment to report the runtime container as the Railway Replica ID.
(The Railway integration is currently in early-testing)
  • Loading branch information
carlosantoniodasilva authored Jan 15, 2025
1 parent b176a55 commit 8ca929d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/node-core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ function getDefaultOptions() {
const defaultLogLevel = process.env.JUDOSCALE_LOG_LEVEL || 'info'

let apiBaseUrl = process.env.JUDOSCALE_URL
let containerID = process.env.DYNO
let containerID

if (process.env.RENDER_INSTANCE_ID) {
containerID = process.env.RENDER_INSTANCE_ID.replace(process.env.RENDER_SERVICE_ID, '').replace('-', '')
if (process.env.DYNO) {
containerID = process.env.DYNO
} else if (process.env.RENDER_INSTANCE_ID) {
apiBaseUrl ||= `https://adapter.judoscale.com/api/${process.env.RENDER_SERVICE_ID}`
}

if (process.env.ECS_CONTAINER_METADATA_URI) {
containerID = process.env.RENDER_INSTANCE_ID.replace(`${process.env.RENDER_SERVICE_ID}-`, '')
} else if (process.env.ECS_CONTAINER_METADATA_URI) {
const parts = process.env.ECS_CONTAINER_METADATA_URI.split('/')
containerID = parts[parts.length - 1]
} else if (process.env.RAILWAY_REPLICA_ID) {
containerID = process.env.RAILWAY_REPLICA_ID
}

return {
Expand Down
18 changes: 16 additions & 2 deletions packages/node-core/test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ const Config = require('../src/config')

describe('Config', () => {
beforeEach(() => {
delete process.env.JUDOSCALE_URL
delete process.env.JUDOSCALE_LOG_LEVEL
delete process.env.DYNO
delete process.env.RENDER_INSTANCE_ID
delete process.env.RENDER_SERVICE_ID
delete process.env.DYNO
delete process.env.JUDOSCALE_URL
delete process.env.ECS_CONTAINER_METADATA_URI
delete process.env.RAILWAY_REPLICA_ID
})

test('has logger property', () => {
expect(new Config()).toHaveProperty('logger')
})

test('has log_level property that can be overridden via JUDOSCALE_LOG_LEVEL', () => {
expect(new Config()).toHaveProperty('log_level', 'info')

process.env.JUDOSCALE_LOG_LEVEL = 'warn'
expect(new Config()).toHaveProperty('log_level', 'warn')
})

test('has now property', () => {
expect(new Config()).toHaveProperty('now', null)
})
Expand Down Expand Up @@ -51,6 +60,11 @@ describe('Config', () => {
expect(new Config()).toHaveProperty('container', 'web.123')
})

test('has container property for Railway', () => {
process.env.RAILWAY_REPLICA_ID = 'random-replica-uuid'
expect(new Config()).toHaveProperty('container', 'random-replica-uuid')
})

test('has version property', () => {
expect(new Config().version).toMatch(/\d+\.\d+\.\d+/)
})
Expand Down

0 comments on commit 8ca929d

Please sign in to comment.