UNPKG

13.4 kBTypeScriptView Raw
1import { AuditEventAction, TraceTags, TypeSpanContext, EventTraceMetadata, EventStateMetadata, HttpRequestOptions } from './model/EventMessage';
2import { IEventRecorder } from './Recorder';
3declare type PartialWithDefaultRecorder<T> = {
4 [P in keyof T]?: T[P];
5} & {
6 defaultRecorder: IEventRecorder;
7};
8/**
9 * Defines Recorders type.
10 * @param defaultRecorder a recorder that will be used by default by the span if the others are not present.
11 * @param logRecorder a recorder that will be used to log any logging level events
12 * @param auditRecorder a recorder that will be used to log audit events
13 * @param traceRecorder a recorder that will be used to log trace events
14 */
15declare type Recorders = PartialWithDefaultRecorder<{
16 defaultRecorder: IEventRecorder;
17 logRecorder?: IEventRecorder;
18 auditRecorder?: IEventRecorder;
19 traceRecorder?: IEventRecorder;
20}>;
21/**
22 * Defines messages allowed to be sent to the Event framework
23 */
24declare type TypeOfMessage = {
25 [key: string]: NonNullable<any>;
26} | string;
27/**
28 * defines options to the injectContextToMessage and extractContextFromMessage
29 * @param type the carrier type
30 * @param path in the carrier where the trace context should be injected or extracted from
31 */
32declare type ContextOptions = {
33 type?: string;
34 path?: string;
35};
36/**
37 * Defines Span interface operations
38 * @param {TypeSpanContext} spanContext the context of the span
39 * @param {Recorders} recorders object that holds span recorders which are going to be used for different operations. defaultRecorder is obligatory
40 * @param info Defines a method to log at info level a message from the span perspective
41 * @param debug Defines a method to log at debug level a message from the span perspective
42 * @param verbose Defines a method to log at verbose level a message from the span perspective
43 * @param performance Defines a method to log at performance level a message from the span perspective
44 * @param warning Defines a method to log at warning level a message from the span perspective
45 * @param error Defines a method to log at error level a message from the span perspective
46 * @param audit Defines a method to send audit event to the auditing environment
47 * @param defaultTagsSetter Defines a method to set default tags. Currently has null implementation
48 * @param getContext Defines method to get the span context as JS object
49 * @param finish Defines a method to finish the current span and send tracing information to the tracing environment
50 * @param getChild Defines a method to get child span
51 * @param setTags Defines a method to set tags to the span
52 * @param getTags Returns current tags
53 * @param setTracestateTags Sets tags to configured vendor and passes them to the tracestate header as base64 encoded JSON
54 * @param getTracestateTags Gets the tags stored into configured vendor tracestate as JSON
55 * @param getTracestates Gets the tracestate as key value pairs as key is the vendor, and for the configured vendor, the value is another key value pairs list, decoded from the tracestate header
56 * @param injectContextToMessage Defnies a method to inject current span context into message carrier
57 * @param injectContextToHttpRequest Defnies a method to inject current span context into http request
58 */
59interface ISpan {
60 spanContext: TypeSpanContext;
61 recorders: Recorders;
62 info: (message: TypeOfMessage) => Promise<any>;
63 debug: (message: TypeOfMessage) => Promise<any>;
64 verbose: (message: TypeOfMessage) => Promise<any>;
65 performance: (message: TypeOfMessage) => Promise<any>;
66 warning: (message: TypeOfMessage) => Promise<any>;
67 error: (message: TypeOfMessage) => Promise<any>;
68 audit: (message: TypeOfMessage) => Promise<any>;
69 defaultTagsSetter: (message?: TypeOfMessage) => Span;
70 getContext: () => TypeSpanContext;
71 finish: (message?: TypeOfMessage, state?: EventStateMetadata, finishTimestamp?: TypeSpanContext["finishTimestamp"]) => Promise<any>;
72 getChild: (service: string, recorders?: Recorders) => ISpan;
73 setTags: (tags: TraceTags) => Span;
74 getTags: () => TraceTags;
75 setTracestateTags: (tags: TraceTags) => Span;
76 getTracestateTags: () => TraceTags;
77 getTracestates: () => {
78 [key: string]: TraceTags | string;
79 };
80 injectContextToMessage: (message: {
81 [key: string]: any;
82 }, injectOptions: ContextOptions) => {
83 [key: string]: any;
84 };
85 injectContextToHttpRequest: (request: {
86 [key: string]: any;
87 }, type?: HttpRequestOptions) => {
88 [key: string]: any;
89 };
90}
91declare class Span implements Partial<ISpan> {
92 spanContext: TypeSpanContext;
93 recorders: Recorders;
94 isFinished: boolean;
95 /**
96 * Creates new span. Normally this is not used directly, but by a Tracer.createSpan method
97 * @param spanContext context of the new span. Service is obligatory. Depending on the rest provided values, the new span will be created as a parent or child span
98 * @param {Recorders} recorders different recorders to be used for different logging methods
99 * @param defaultTagsSetter the tags setter method can be passed here
100 */
101 constructor(spanContext: EventTraceMetadata, recorders?: Recorders, defaultTagsSetter?: (message: TypeOfMessage) => any);
102 /**
103 * A method to set tags by default.
104 * @param message the message which tags will be extracted from
105 */
106 defaultTagsSetter(message?: TypeOfMessage): Span;
107 setTracestates(tracestates: {
108 [key: string]: TraceTags | string;
109 }): this;
110 /**
111 * Gets trace context from the current span
112 */
113 getContext(): TypeSpanContext;
114 /**
115 * Creates and returns new child span of the current span and changes the span service name
116 * @param service the name of the service of the new child span
117 * @param recorders the recorders which are be set to the child span. If omitted, the recorders of the parent span are used
118 */
119 getChild(service: string, recorders?: Recorders): Span;
120 /**
121 * Injects trace context into a carrier with optional path.
122 * @param carrier any kind of message or other object with keys of type String.
123 * @param injectOptions type and path of the carrier. Type is not implemented yet. Path is the path to the trace context.
124 */
125 injectContextToMessage(carrier: {
126 [key: string]: any;
127 }, injectOptions?: ContextOptions): {
128 [key: string]: any;
129 };
130 /**
131 * Injects trace context into a http request headers.
132 * @param request HTTP request.
133 * @param type type of the headers that will be created - 'w3c' or 'xb3'.
134 */
135 injectContextToHttpRequest(request: {
136 [key: string]: any;
137 }, type?: HttpRequestOptions): {
138 [key: string]: any;
139 };
140 /**
141 * Sets tags to the current span. If child span is created, the tags are passed on.
142 * @param tags key value pairs of tags. Tags can be changed on different child spans
143 */
144 setTags(tags: TraceTags): this;
145 private _setTagTracestate;
146 /**
147 * Returns tags values
148 */
149 getTags(): TraceTags;
150 /**
151 * Sets tags, persisted in the tracestate header as key value pairs as base64 encoded string
152 * @param tags key-value pairs with tags
153 */
154 setTracestateTags(tags: TraceTags): this;
155 /**
156 * Returns the tracestates object per vendor, as configured vendor tracestate is decoded key value pair with tags
157 */
158 getTracestates(): {
159 [key: string]: TraceTags | string;
160 };
161 /**
162 * Returns the tracestate tags for the configured vendor as key value pairs
163 */
164 getTracestateTags(): TraceTags;
165 /**
166 * Finishes the current span and its trace and sends the data to the tracing framework.
167 * @param message optional parameter for a message to be passed to the tracing framework.
168 * @param finishTimestamp optional parameter for the finish time. If omitted, current time is used.
169 */
170 finish(message?: TypeOfMessage, state?: EventStateMetadata, finishTimestamp?: string | Date): Promise<this>;
171 /**
172 * Finishes the trace by adding finish timestamp to the current span.
173 * @param finishTimestamp optional parameter for the finish time. If omitted, current time is used.
174 */
175 private _finishSpan;
176 /**
177 * Sends trace message to the tracing framework
178 * @param message
179 * @param spanContext optional parameter. Can be used to trace previous span. If not set, the current span context is used.
180 * @param action optional parameter for action. Defaults to 'span'
181 * @param state optional parameter for state. Defaults to 'success'
182 */
183 private trace;
184 /**
185 * Sends audit type message to the event logging framework.
186 * @param message message to be recorded as audit event
187 * @param action optional parameter for action. Defaults to 'default'
188 * @param state optional parameter for state. Defaults to 'success'
189 */
190 audit(message: TypeOfMessage, action?: AuditEventAction, state?: EventStateMetadata): Promise<any>;
191 /**
192 * Logs INFO type message.
193 * @param message if message is a string, the message is added to a message property of context of an event message.
194 * If message is not following the event framework message format, the message is added as it is to the context of an event message.
195 * If message follows the event framework message format, only the metadata is updated and if message lacks an UUID it is created.
196 * @param state optional parameter for state. Defaults to 'success'
197 */
198 info(message: TypeOfMessage, state?: EventStateMetadata): Promise<any>;
199 /**
200 * Logs DEBUG type message.
201 * @param message if message is a string, the message is added to a message property of context of an event message.
202 * If message is not following the event framework message format, the message is added as it is to the context of an event message.
203 * If message follows the event framework message format, only the metadata is updated and if message lacks an UUID it is created.
204 * @param state optional parameter for state. Defaults to 'success'
205 */
206 debug(message: TypeOfMessage, state?: EventStateMetadata): Promise<any>;
207 /**
208 * Logs VERBOSE type message.
209 * @param message if message is a string, the message is added to a message property of context of an event message.
210 * If message is not following the event framework message format, the message is added as it is to the context of an event message.
211 * If message follows the event framework message format, only the metadata is updated and if message lacks an UUID it is created.
212 * @param state optional parameter for state. Defaults to 'success'
213 */
214 verbose(message: TypeOfMessage, state?: EventStateMetadata): Promise<any>;
215 /**
216 * Logs PERFORMANCE type message.
217 * @param message if message is a string, the message is added to a message property of context of an event message.
218 * If message is not following the event framework message format, the message is added as it is to the context of an event message.
219 * If message follows the event framework message format, only the metadata is updated and if message lacks an UUID it is created.
220 * @param state optional parameter for state. Defaults to 'success'
221 */
222 performance(message: TypeOfMessage, state?: EventStateMetadata): Promise<any>;
223 /**
224 * Logs WARNING type message.
225 * @param message if message is a string, the message is added to a message property of context of an event message.
226 * If message is not following the event framework message format, the message is added as it is to the context of an event message.
227 * If message follows the event framework message format, only the metadata is updated and if message lacks an UUID it is created.
228 * @param state optional parameter for state. Defaults to 'success'
229 */
230 warning(message: TypeOfMessage, state?: EventStateMetadata): Promise<any>;
231 /**
232 * Logs ERROR type message.
233 * @param message if message is a string, the message is added to a message property of context of an event message.
234 * If message is not following the event framework message format, the message is added as it is to the context of an event message.
235 * If message follows the event framework message format, only the metadata is updated and if message lacks an UUID it is created.
236 * @param state optional parameter for state. Defaults to 'success'
237 */
238 error(message: TypeOfMessage, state?: EventStateMetadata): Promise<any>;
239 /**
240 * Sends Event message to recorders
241 * @param message the Event message that needs to be recorded
242 * @param type type of Event
243 * @param action optional parameter for action. The default is based on type defaults
244 * @param state optional parameter for state. Defaults to 'success'
245 */
246 private recordMessage;
247 /**
248 * Helper function to create event message, based on message and event types, action and state.
249 */
250 private createEventMessage;
251}
252declare const setHttpHeader: (context: TypeSpanContext, type: HttpRequestOptions, headers: {
253 [key: string]: any;
254}) => {
255 [key: string]: any;
256};
257declare const createW3CTracestate: (spanContext: TypeSpanContext, tracestate?: string | undefined) => string;
258export { Span, ContextOptions, Recorders, setHttpHeader, createW3CTracestate };