File size: 7,237 Bytes
31e8457
051bada
 
 
 
 
 
 
31e8457
 
 
 
 
051bada
31e8457
 
051bada
31e8457
051bada
31e8457
051bada
31e8457
 
 
 
051bada
 
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051bada
 
 
 
 
 
 
31e8457
 
 
 
 
051bada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * Exports Effect log entries over OTLP/HTTP.
 *
 * The logger turns Effect log entries into OTLP log records and sends them to a
 * logs endpoint, such as an OpenTelemetry Collector or vendor OTLP endpoint. It
 * includes log levels, messages, annotations, causes, fiber ids, optional log
 * spans, and current trace/span ids when they are present.
 *
 * @since 4.0.0
 */
import * as Arr from "../../Array.js";
import * as Cause from "../../Cause.js";
import { Clock } from "../../Clock.js";
import * as Config from "../../Config.js";
import * as Duration from "../../Duration.js";
import * as Effect from "../../Effect.js";
import * as Layer from "../../Layer.js";
import * as Logger from "../../Logger.js";
import * as Option from "../../Option.js";
import { CurrentLogAnnotations, CurrentLogSpans } from "../../References.js";
import * as OtlpEnv from "./internal/otlpEnv.js";
import * as Exporter from "./OtlpExporter.js";
import * as OtlpResource from "./OtlpResource.js";
import { OtlpSerialization } from "./OtlpSerialization.js";
/**
 * Creates an Effect `Logger` that exports log records through OTLP.
 *
 * **Details**
 *
 * The logger serializes records with the configured resource, sends them
 * through the OTLP exporter, and requires `Scope` so pending records can be
 * flushed on shutdown.
 *
 * @category constructors
 * @since 4.0.0
 */
export const make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
  const serialization = yield* OtlpSerialization;
  const otelResource = yield* OtlpResource.fromConfig(options.resource);
  const scope = {
    name: OtlpResource.serviceNameUnsafe(otelResource)
  };
  const exporter = yield* Exporter.make({
    label: "OtlpLogger",
    url: options.url,
    headers: options.headers,
    maxBatchSize: options.maxBatchSize ?? 1000,
    exportInterval: options.exportInterval ?? Duration.seconds(1),
    body: data => serialization.logs({
      resourceLogs: [{
        resource: otelResource,
        scopeLogs: [{
          scope,
          logRecords: data
        }]
      }]
    }),
    shutdownTimeout: options.shutdownTimeout ?? Duration.seconds(3)
  });
  const opts = {
    excludeLogSpans: options.excludeLogSpans ?? false,
    clock: yield* Clock
  };
  return Logger.make(options => {
    exporter.push(makeLogRecord(options, opts));
  });
});
/**
 * Layer that installs the OTLP logger created by `make`.
 *
 * **Details**
 *
 * By default the OTLP logger is merged with any existing loggers.
 *
 * @category layers
 * @since 4.0.0
 */
export const layer = options => Logger.layer([make(options)], {
  mergeWithExisting: options.mergeWithExisting ?? true
});
/**
 * Creates an OTLP logs layer from OpenTelemetry configuration.
 *
 * @category layers
 * @since 4.0.0
 */
export const layerFromConfig = options => Effect.gen(function* () {
  const {
    disabled,
    endpoint,
    exporters
  } = yield* Config.all({
    disabled: Config.boolean("OTEL_SDK_DISABLED").pipe(Config.withDefault(false)),
    endpoint: OtlpEnv.endpoint("LOGS"),
    exporters: OtlpEnv.exporters("LOGS")
  });
  if (disabled || !endpoint || !exporters.includes("otlp")) {
    return Layer.empty;
  }
  const {
    baseTimeout,
    logsTimeout,
    exportTimeout,
    scheduleDelay,
    maxBatchSize
  } = yield* Config.all({
    baseTimeout: Config.option(Config.int("OTEL_EXPORTER_OTLP_TIMEOUT")),
    logsTimeout: Config.option(Config.int("OTEL_EXPORTER_OTLP_LOGS_TIMEOUT")),
    exportTimeout: Config.option(Config.int("OTEL_BLRP_EXPORT_TIMEOUT")),
    scheduleDelay: Config.option(Config.int("OTEL_BLRP_SCHEDULE_DELAY")),
    maxBatchSize: Config.option(Config.int("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE"))
  });
  const shutdownTimeout = Option.firstSomeOf([logsTimeout, baseTimeout, exportTimeout]).pipe(Option.map(_ => Duration.millis(_)));
  const exportInterval = Option.map(scheduleDelay, _ => Duration.millis(_));
  return layer({
    url: endpoint.toString(),
    resource: options?.resource,
    headers: options?.headers ?? (yield* OtlpEnv.headers("LOGS")),
    exportInterval: Option.getOrUndefined(exportInterval),
    maxBatchSize: Option.getOrUndefined(maxBatchSize),
    shutdownTimeout: Option.getOrUndefined(shutdownTimeout),
    excludeLogSpans: options?.excludeLogSpans,
    mergeWithExisting: options?.mergeWithExisting
  });
}).pipe(Effect.orDie, Layer.unwrap);
// internal
const makeLogRecord = (options, opts) => {
  const now = opts.clock.currentTimeNanosUnsafe();
  const nanosString = now.toString();
  const nowMillis = options.date.getTime();
  const attributes = OtlpResource.entriesToAttributes(Object.entries(options.fiber.getRef(CurrentLogAnnotations)));
  attributes.push({
    key: "fiberId",
    value: {
      intValue: options.fiber.id
    }
  });
  if (!opts.excludeLogSpans) {
    for (const [label, startTime] of options.fiber.getRef(CurrentLogSpans)) {
      attributes.push({
        key: `logSpan.${label}`,
        value: {
          stringValue: `${nowMillis - startTime}ms`
        }
      });
    }
  }
  if (options.cause.reasons.length > 0) {
    attributes.push({
      key: "log.error",
      value: {
        stringValue: Cause.pretty(options.cause)
      }
    });
  }
  const message = Arr.ensure(options.message);
  const logRecord = {
    severityNumber: logLevelToSeverityNumber(options.logLevel),
    severityText: options.logLevel,
    timeUnixNano: nanosString,
    observedTimeUnixNano: nanosString,
    attributes,
    body: OtlpResource.unknownToAttributeValue(message.length === 1 ? message[0] : message),
    droppedAttributesCount: 0
  };
  if (options.fiber.currentSpan) {
    logRecord.traceId = options.fiber.currentSpan.traceId;
    logRecord.spanId = options.fiber.currentSpan.spanId;
  }
  return logRecord;
};
const logLevelToSeverityNumber = logLevel => {
  switch (logLevel) {
    case "Trace":
      return ESeverityNumber.SEVERITY_NUMBER_TRACE;
    case "Debug":
      return ESeverityNumber.SEVERITY_NUMBER_DEBUG;
    case "Info":
      return ESeverityNumber.SEVERITY_NUMBER_INFO;
    case "Warn":
      return ESeverityNumber.SEVERITY_NUMBER_WARN;
    case "Error":
      return ESeverityNumber.SEVERITY_NUMBER_ERROR;
    case "Fatal":
      return ESeverityNumber.SEVERITY_NUMBER_FATAL;
    default:
      return ESeverityNumber.SEVERITY_NUMBER_UNSPECIFIED;
  }
};
/**
 * Numerical value of the severity, normalized to values described in Log Data Model.
 */
const ESeverityNumber = {
  /** Unspecified. Do NOT use as default */
  SEVERITY_NUMBER_UNSPECIFIED: 0,
  SEVERITY_NUMBER_TRACE: 1,
  SEVERITY_NUMBER_TRACE2: 2,
  SEVERITY_NUMBER_TRACE3: 3,
  SEVERITY_NUMBER_TRACE4: 4,
  SEVERITY_NUMBER_DEBUG: 5,
  SEVERITY_NUMBER_DEBUG2: 6,
  SEVERITY_NUMBER_DEBUG3: 7,
  SEVERITY_NUMBER_DEBUG4: 8,
  SEVERITY_NUMBER_INFO: 9,
  SEVERITY_NUMBER_INFO2: 10,
  SEVERITY_NUMBER_INFO3: 11,
  SEVERITY_NUMBER_INFO4: 12,
  SEVERITY_NUMBER_WARN: 13,
  SEVERITY_NUMBER_WARN2: 14,
  SEVERITY_NUMBER_WARN3: 15,
  SEVERITY_NUMBER_WARN4: 16,
  SEVERITY_NUMBER_ERROR: 17,
  SEVERITY_NUMBER_ERROR2: 18,
  SEVERITY_NUMBER_ERROR3: 19,
  SEVERITY_NUMBER_ERROR4: 20,
  SEVERITY_NUMBER_FATAL: 21,
  SEVERITY_NUMBER_FATAL2: 22,
  SEVERITY_NUMBER_FATAL3: 23,
  SEVERITY_NUMBER_FATAL4: 24
};
//# sourceMappingURL=OtlpLogger.js.map