-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextInfo.ts
158 lines (142 loc) · 4.47 KB
/
ContextInfo.ts
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
/** @module info */
/** @hidden */
let os = require('os');
import { StringValueMap } from 'pip-services-commons-node';
import { ConfigParams } from 'pip-services-commons-node';
import { IReconfigurable } from 'pip-services-commons-node';
/**
* Context information component that provides detail information
* about execution context: container or/and process.
*
* Most often ContextInfo is used by logging and performance counters
* to identify source of the collected logs and metrics.
*
* ### Configuration parameters ###
*
* - name: the context (container or process) name
* - description: human-readable description of the context
* - properties: entire section of additional descriptive properties
* - ...
*
* ### Example ###
*
* let contextInfo = new ContextInfo();
* contextInfo.configure(ConfigParams.fromTuples(
* "name", "MyMicroservice",
* "description", "My first microservice"
* ));
*
* context.name; // Result: "MyMicroservice"
* context.contextId; // Possible result: "mylaptop"
* context.startTime; // Possible result: 2018-01-01:22:12:23.45Z
* context.uptime; // Possible result: 3454345
*/
export class ContextInfo implements IReconfigurable {
private _name: string = "unknown";
private _description: string = null;
private _contextId: string = os.hostname(); // IdGenerator.nextLong();
private _startTime: Date = new Date();
private _properties: StringValueMap = new StringValueMap();
/**
* Creates a new instance of this context info.
*
* @param name (optional) a context name.
* @param description (optional) a human-readable description of the context.
*/
public constructor(name?: string, description?: string) {
this._name = name || "unknown";
this._description = description || null;
}
/**
* Configures component by passing configuration parameters.
*
* @param config configuration parameters to be set.
*/
public configure(config: ConfigParams): void {
this.name = config.getAsStringWithDefault("name", this.name);
this.description = config.getAsStringWithDefault("description", this.description);
this.properties = config.getSection("properties");
}
/**
* Gets the context name.
*
* @returns the context name
*/
public get name(): string { return this._name; }
/**
* Sets the context name.
*
* @param value a new name for the context.
*/
public set name(value: string) { this._name = value || "unknown"; }
/**
* Gets the human-readable description of the context.
*
* @returns the human-readable description of the context.
*/
public get description(): string { return this._description; }
/**
* Sets the human-readable description of the context.
*
* @param value a new human readable description of the context.
*/
public set description(value: string) { this._description = value; }
/**
* Gets the unique context id.
* Usually it is the current host name.
*
* @returns the unique context id.
*/
public get contextId(): string { return this._contextId; }
/**
* Sets the unique context id.
*
* @param value a new unique context id.
*/
public set contextId(value: string) { this._contextId = value; }
/**
* Gets the context start time.
*
* @returns the context start time.
*/
public get startTime(): Date { return this._startTime; }
/**
* Sets the context start time.
*
* @param value a new context start time.
*/
public set startTime(value: Date) { this._startTime = value || new Date(); }
/**
* Calculates the context uptime as from the start time.
*
* @returns number of milliseconds from the context start time.
*/
public get uptime(): number {
return new Date().getTime() - this._startTime.getTime();
}
/**
* Gets context additional parameters.
*
* @returns a JSON object with additional context parameters.
*/
public get properties(): any { return this._properties; }
/**
* Sets context additional parameters.
*
* @param properties a JSON object with context additional parameters
*/
public set properties(properties: any) {
this._properties = StringValueMap.fromValue(properties);
}
/**
* Creates a new ContextInfo and sets its configuration parameters.
*
* @param config configuration parameters for the new ContextInfo.
* @returns a newly created ContextInfo
*/
public static fromConfig(config: ConfigParams): ContextInfo {
let result = new ContextInfo();
result.configure(config);
return result;
}
}