This repository was archived by the owner on Mar 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathentry.ts
More file actions
215 lines (206 loc) · 6.5 KB
/
Copy pathentry.ts
File metadata and controls
215 lines (206 loc) · 6.5 KB
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
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Merge} from 'type-fest';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const EventId = require('eventid');
import * as extend from 'extend';
import * as is from 'is';
import {google} from '../protos/protos';
import {objToStruct, structToObj} from './common';
const eventId = new EventId();
export type Timestamp = google.protobuf.ITimestamp | Date | string;
export type LogSeverity = google.logging.type.LogSeverity | string;
export type LogEntry = Merge<
google.logging.v2.ILogEntry,
{
timestamp?: Timestamp | null;
severity?: LogSeverity | null;
}
>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Data = any;
export interface EntryJson {
timestamp: Timestamp;
insertId: number;
jsonPayload?: google.protobuf.IStruct;
textPayload?: string;
}
export interface ToJsonOptions {
removeCircular?: boolean;
}
/**
* Create an entry object to define new data to insert into a log.
*
* Note, [Cloud Logging Quotas and limits]{@link https://cloud.google.com/logging/quotas}
* dictates that the maximum log entry size, including all
* [LogEntry Resource properties]{@link https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry},
* cannot exceed _approximately_ 256 KB.
*
* @see [LogEntry JSON representation]{@link https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry}
*
* @class
*
* @param {?object} [metadata] See a
* [LogEntry
* Resource](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
* @param {object|string} data The data to use as the value for this log
* entry.
*
* If providing an object, these value types are supported:
* - `String`
* - `Number`
* - `Boolean`
* - `Buffer`
* - `Object`
* - `Array`
*
* Any other types are stringified with `String(value)`.
*
* @example
* const {Logging} = require('@google-cloud/logging');
* const logging = new Logging();
* const syslog = logging.log('syslog');
*
* const metadata = {
* resource: {
* type: 'gce_instance',
* labels: {
* zone: 'global',
* instance_id: '3'
* }
* }
* };
*
* const entry = syslog.entry(metadata, {
* delegate: 'my_username'
* });
*
* syslog.alert(entry, (err, apiResponse) => {
* if (!err) {
* // Log entry inserted successfully.
* }
* });
*
* //-
* // You will also receive `Entry` objects when using
* // Logging#getEntries() and Log#getEntries().
* //-
* logging.getEntries((err, entries) => {
* if (!err) {
* // entries[0].data = The data value from the log entry.
* }
* });
*/
class Entry {
metadata: LogEntry;
data: Data;
constructor(metadata?: LogEntry, data?: Data) {
/**
* @name Entry#metadata
* @type {object}
* @property {Date} timestamp
* @property {number} insertId
*/
this.metadata = extend(
{
timestamp: new Date(),
},
metadata
);
// JavaScript date has a very coarse granularity (millisecond), which makes
// it quite likely that multiple log entries would have the same timestamp.
// The Logging API doesn't guarantee to preserve insertion order for entries
// with the same timestamp. The service does use `insertId` as a secondary
// ordering for entries with the same timestamp. `insertId` needs to be
// globally unique (within the project) however.
//
// We use a globally unique monotonically increasing EventId as the
// insertId.
this.metadata.insertId = this.metadata.insertId || eventId.new();
/**
* @name Entry#data
* @type {object}
*/
this.data = data;
}
/**
* Serialize an entry to the format the API expects.
*
* @param {object} [options] Configuration object.
* @param {boolean} [options.removeCircular] Replace circular references in an
* object with a string value, `[Circular]`.
*/
toJSON(options: ToJsonOptions = {}) {
const entry = (extend(true, {}, this.metadata) as {}) as EntryJson;
// Format log message
if (is.object(this.data)) {
entry.jsonPayload = objToStruct(this.data, {
removeCircular: !!options.removeCircular,
stringify: true,
});
} else if (is.string(this.data)) {
entry.textPayload = this.data;
}
// Format log timestamp
if (is.date(entry.timestamp)) {
const seconds = (entry.timestamp as Date).getTime() / 1000;
const secondsRounded = Math.floor(seconds);
entry.timestamp = {
seconds: secondsRounded,
nanos: Math.floor((seconds - secondsRounded) * 1e9),
};
} else if (is.string(entry.timestamp)) {
// Convert RFC3339 "Zulu" timestamp into a format that can be parsed to Date
const zuluTime = entry.timestamp as string;
const ms = Date.parse(zuluTime.split(/[.,Z]/)[0] + 'Z');
const reNano = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.(\d{0,9})Z$/;
const nanoSecs = zuluTime.match(reNano)?.[1];
entry.timestamp = {
seconds: ms ? Math.floor(ms / 1000) : 0,
nanos: nanoSecs ? Number(nanoSecs.padEnd(9, '0')) : 0,
};
}
return entry;
}
/**
* Create an Entry object from an API response, such as `entries:list`.
*
* @private
*
* @param {object} entry An API representation of an entry. See a
* [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
* @returns {Entry}
*/
static fromApiResponse_(entry: google.logging.v2.LogEntry) {
let data = entry[entry.payload!];
if (entry.payload === 'jsonPayload') {
data = structToObj(data);
}
const serializedEntry = new Entry(entry, data);
if (entry.timestamp) {
let ms = Number(entry.timestamp.seconds) * 1000;
ms += Number(entry.timestamp.nanos) / 1e6;
serializedEntry.metadata.timestamp = new Date(ms);
}
return serializedEntry;
}
}
/**
* Reference to the {@link Entry} class.
* @name module:@google-cloud/logging.Entry
* @see Entry
*/
export {Entry};