Skip to content

Commit

Permalink
Log/trace JSONs larger than 10MB in Assets client's getAppJSONByVendor
Browse files Browse the repository at this point in the history
The class `AppsConfigsCloader` in `vtex.pages-graphql` has an Assets clients that calls `getSettings`.

`getSettings` may call `getBuildJSONForApp` or `getSettingsFromFilesForApp`, and both of them call `getJSON`.

`getJSON` calls `getAppJSONByVendor`, which now logs to OS (`"Large JSON file"` message) and Honeycomb the JSON's file size (`json-size` field).
  • Loading branch information
filipewl committed Sep 22, 2023
1 parent 566900b commit 9c05646
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Log/trace JSON's size in the Assets client's `getAppJSONByVendor` method.

## [6.45.22] - 2023-09-20

### Removed
Expand Down
41 changes: 38 additions & 3 deletions src/clients/infra/Assets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { contains, filter, isEmpty, pick as ramdaPick, zipObj } from 'ramda'
import { Readable } from 'stream'

import { AppMetaInfo } from '../..'
import { AppMetaInfo, Logger } from '../..'
import { CacheType, inflightURL, InstanceOptions, RequestTracingConfig } from '../../HttpClient'
import {
IgnoreNotFoundRequestConfig,
Expand All @@ -10,6 +10,38 @@ import { IOContext } from '../../service/worker/runtime/typings'
import { parseAppId, ParsedLocator } from '../../utils'
import { InfraClient } from './InfraClient'

const LOGGER_JSON_SIZE_THRESHOLD_MB = 10

function jsonSizeInMB(jsonData: any) {
try {
const sizeInBytes = Buffer.from(JSON.stringify(jsonData)).length

return sizeInBytes / (1024 * 1024)
} catch (error) {
return 0
}
}

function logJsonSize<T extends object | null>(url: string, logger: Logger, tracingConfig?: RequestTracingConfig) {
return function log(jsonData: T) {
const size = jsonSizeInMB(jsonData)

if (size >= LOGGER_JSON_SIZE_THRESHOLD_MB) {
const logMessage = {
message: 'Large JSON file',
url,
size,
}

logger.debug(logMessage)
}

tracingConfig?.tracing?.rootSpan?.addTags({ 'json-size': size })

return jsonData
}
}

const dependsOnApp = (appsAtMajor: string[]) => (a: AppMetaInfo) => {
let dependsOn = false
appsAtMajor.forEach(appAtMajor => {
Expand Down Expand Up @@ -153,7 +185,10 @@ export class Assets extends InfraClient {
const vendor = locator.name.split('.')[0]
const inflightKey = inflightURL
const metric = 'assets-get-json-by-vendor'
return this.http.get<T>(this.routes.Files(vendor, locator, path), {
const url = this.routes.Files(vendor, locator, path)
const { logger } = this.context

return this.http.get<T>(url, {
cacheable: CacheType.Any,
inflightKey,
metric,
Expand All @@ -162,7 +197,7 @@ export class Assets extends InfraClient {
requestSpanNameSuffix: metric,
...tracingConfig?.tracing,
},
} as IgnoreNotFoundRequestConfig)
} as IgnoreNotFoundRequestConfig).then(logJsonSize(url, logger, tracingConfig))
}

protected getAppFileByAccount = (app: string, path: string, nullIfNotFound?: boolean, tracingConfig?: RequestTracingConfig) => {
Expand Down

0 comments on commit 9c05646

Please sign in to comment.