@crombolo/server 0.45.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/settings.ts CHANGED
@@ -34,6 +34,7 @@ const SECTIONS = [
34
34
  'Public site',
35
35
  'Remote',
36
36
  'Logging',
37
+ 'Telemetry',
37
38
  'Command line',
38
39
  'AWS credentials',
39
40
  ] as const
@@ -835,6 +836,39 @@ const DECLARED = [
835
836
  description:
836
837
  'The header your CDN purges by, such as Surrogate-Key or Cache-Tag. Set, every answer lists the pages and media in it.',
837
838
  },
839
+ {
840
+ env: 'CROMBOLO_CACHE_MODE',
841
+ kind: 'enum',
842
+ values: ['lifetime', 'check'],
843
+ default: 'lifetime',
844
+ section: 'Public site',
845
+ description:
846
+ 'How a CDN keeps copies: for the lifetime set above, or, with check, kept and checked back with the site before each use, served at once while checking and while the site is down. Check suits a CDN with no purge set up.',
847
+ },
848
+ {
849
+ env: 'CROMBOLO_CACHE_STALE_IF_ERROR',
850
+ kind: 'number',
851
+ default: '604800',
852
+ section: 'Public site',
853
+ description:
854
+ 'Seconds a kept copy may still be served while the site is answering with errors. Applies whenever copies are kept.',
855
+ },
856
+ {
857
+ env: 'CROMBOLO_CACHE_CHECK_FLOOR',
858
+ kind: 'number',
859
+ default: '60',
860
+ section: 'Public site',
861
+ description:
862
+ 'With check, seconds a copy is kept before checking when checking means showing the page again, so each CDN location does so at most this often.',
863
+ },
864
+ {
865
+ env: 'CROMBOLO_CACHE_VALIDATOR',
866
+ kind: 'string',
867
+ default: 'body',
868
+ section: 'Public site',
869
+ description:
870
+ 'How a CDN checking a kept copy is answered: body renders the page and compares it; published answers from what was last published, without rendering. Use published only when no page shows the time or anything else that changes without a publish.',
871
+ },
838
872
  {
839
873
  env: 'CROMBOLO_DELIVERY_API_PREVIEW_URL',
840
874
  kind: 'string',
@@ -938,6 +972,89 @@ const DECLARED = [
938
972
  'Every this many seconds, prints the memory in use and the requests in flight. `0` is off.',
939
973
  },
940
974
 
975
+ // Telemetry (story 262): OpenTelemetry's own variables, read as its specification names them.
976
+ {
977
+ env: 'OTEL_EXPORTER_OTLP_ENDPOINT',
978
+ kind: 'string',
979
+ section: 'Telemetry',
980
+ description:
981
+ 'The OpenTelemetry collector traces, metrics and logs are sent to, such as `https://otlp.example.com`. Unset, nothing is sent.',
982
+ },
983
+ {
984
+ env: 'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT',
985
+ kind: 'string',
986
+ section: 'Telemetry',
987
+ description: 'Where traces alone are sent, in place of the collector above.',
988
+ },
989
+ {
990
+ env: 'OTEL_EXPORTER_OTLP_METRICS_ENDPOINT',
991
+ kind: 'string',
992
+ section: 'Telemetry',
993
+ description: 'Where metrics alone are sent, in place of the collector above.',
994
+ },
995
+ {
996
+ env: 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT',
997
+ kind: 'string',
998
+ section: 'Telemetry',
999
+ description: 'Where log lines alone are sent, in place of the collector above.',
1000
+ },
1001
+ {
1002
+ env: 'OTEL_METRIC_EXPORT_INTERVAL',
1003
+ kind: 'number',
1004
+ default: '60000',
1005
+ section: 'Telemetry',
1006
+ description: 'Milliseconds between sending the metrics.',
1007
+ },
1008
+ {
1009
+ env: 'OTEL_EXPORTER_OTLP_HEADERS',
1010
+ kind: 'string',
1011
+ section: 'Telemetry',
1012
+ description:
1013
+ 'Headers sent with every export, usually the API key, as `name=value` pairs separated by commas. Never shown in the back office.',
1014
+ },
1015
+ {
1016
+ env: 'OTEL_EXPORTER_OTLP_PROTOCOL',
1017
+ kind: 'enum',
1018
+ values: ['http/protobuf', 'http/json'],
1019
+ default: 'http/protobuf',
1020
+ section: 'Telemetry',
1021
+ description: 'How exports are written.',
1022
+ },
1023
+ {
1024
+ env: 'OTEL_SERVICE_NAME',
1025
+ kind: 'string',
1026
+ section: 'Telemetry',
1027
+ description: "The name the site's telemetry is filed under; the site's name when unset.",
1028
+ },
1029
+ {
1030
+ env: 'OTEL_RESOURCE_ATTRIBUTES',
1031
+ kind: 'string',
1032
+ section: 'Telemetry',
1033
+ description:
1034
+ 'More attributes for everything sent, such as `deployment.environment.name=production`, separated by commas.',
1035
+ },
1036
+ {
1037
+ env: 'OTEL_TRACES_SAMPLER',
1038
+ kind: 'string',
1039
+ default: 'parentbased_always_on',
1040
+ section: 'Telemetry',
1041
+ description:
1042
+ 'Which traces are kept: `parentbased_traceidratio` with `OTEL_TRACES_SAMPLER_ARG=0.1` keeps a tenth, for a busy site.',
1043
+ },
1044
+ {
1045
+ env: 'OTEL_TRACES_SAMPLER_ARG',
1046
+ kind: 'number',
1047
+ section: 'Telemetry',
1048
+ description: "The sampler's ratio, from 0 to 1.",
1049
+ },
1050
+ {
1051
+ env: 'CROMBOLO_TRUSTED_PROXIES',
1052
+ kind: 'list',
1053
+ section: 'Telemetry',
1054
+ description:
1055
+ 'The load balancers or CDN in front of the site, as addresses or ranges such as `10.0.0.0/8`, separated by commas. A trace a request arrives with is continued only from one of them.',
1056
+ },
1057
+
941
1058
  // Command line
942
1059
  {
943
1060
  env: 'CROMBOLO_BOOT_ATTEMPTS',
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The hash of each of the site's stylesheets and scripts (story 260), worked
3
+ * out as the server starts and again when a watched file changes, so
4
+ * `asset()` answers from memory. docs/rendering.md, "Stylesheets and scripts".
5
+ */
6
+ import { existsSync, type FSWatcher, readdirSync, readFileSync, statSync, watch } from 'node:fs'
7
+ import { join, relative, sep } from 'node:path'
8
+ import { contentHash, hashedAddress } from '@crombolo/render'
9
+
10
+ export interface AssetRoot {
11
+ /** Where its files are served: `/css`. */
12
+ prefix: string
13
+ dir: string
14
+ extension: string
15
+ }
16
+
17
+ export class SiteAssets {
18
+ readonly #roots: readonly AssetRoot[]
19
+ readonly #hashes = new Map<string, string>()
20
+ readonly #watchers: FSWatcher[] = []
21
+
22
+ constructor(roots: readonly AssetRoot[]) {
23
+ this.#roots = roots
24
+ }
25
+
26
+ /** Hashes every file, and watches each folder for changes. */
27
+ start(): void {
28
+ for (const root of this.#roots) {
29
+ if (!existsSync(root.dir)) continue
30
+ for (const file of filesIn(root.dir)) this.#hash(root, file)
31
+ try {
32
+ const watcher = watch(root.dir, { recursive: true }, (_event, name) => {
33
+ if (name) this.#hash(root, join(root.dir, name.toString()))
34
+ })
35
+ watcher.on('error', () => {})
36
+ this.#watchers.push(watcher)
37
+ } catch {
38
+ // A folder that cannot be watched keeps the hashes it started with.
39
+ }
40
+ }
41
+ }
42
+
43
+ /** The hashed address of a file the site has, or undefined. */
44
+ address(path: string): string | undefined {
45
+ const hash = this.#hashes.get(path)
46
+ return hash ? hashedAddress(path, hash) : undefined
47
+ }
48
+
49
+ close(): void {
50
+ for (const watcher of this.#watchers.splice(0)) watcher.close()
51
+ }
52
+
53
+ #hash(root: AssetRoot, file: string): void {
54
+ const path = `${root.prefix}/${relative(root.dir, file).split(sep).join('/')}`
55
+ if (!file.toLowerCase().endsWith(root.extension)) return
56
+ try {
57
+ if (!statSync(file).isFile()) return
58
+ this.#hashes.set(path, contentHash(readFileSync(file)))
59
+ } catch {
60
+ this.#hashes.delete(path)
61
+ }
62
+ }
63
+ }
64
+
65
+ function filesIn(dir: string, out: string[] = []): string[] {
66
+ for (const name of readdirSync(dir)) {
67
+ const path = join(dir, name)
68
+ if (statSync(path).isDirectory()) filesIn(path, out)
69
+ else out.push(path)
70
+ }
71
+ return out
72
+ }