-
Notifications
You must be signed in to change notification settings - Fork 4
/
CustomOCIOutputter.groovy
200 lines (173 loc) · 6.2 KB
/
CustomOCIOutputter.groovy
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
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.loggingingestion.LoggingClient;
import com.oracle.bmc.loggingingestion.model.*;
import com.oracle.bmc.loggingingestion.requests.*;
import com.oracle.bmc.loggingingestion.responses.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Date;
import java.util.Arrays;
/*
* Implements the mechanism to communicate with OCI Logging
* underlying documentation on how this can be done is detailed at:
* https://docs.oracle.com/en-us/iaas/api/#/en/logging-dataplane/20200831/LogEntry/PutLogs
*
* Controls from this are loaded from the provided properties file
* connection details to OCI come from a separate properties file
*/
class CustomOCIOutputter implements LogGenerator.RecordLogEvent
{
private static final String AGENTTYPE="LOGSIM";
private static final String AGENTNAME = "AGENTNAME";
private static final String BATCHSIZE = "BATCHSIZE";
private static final String LOGICID = "LogOCID";
private static final String OCICONFIGFILE = "OCICONFIGFILE";
private static final String PROPFILEGROUP = "PROPFILEGROUP";
private String logOCID = null;
private LoggingClient client = null;
private String agentName = "LogSimulator";
private String logSubject = "";
private boolean verbose = false
private int batchSize = 0;
private ArrayList batch = null;
private String profileGroup = "DEFAULT";
private log (String msg, boolean out=this.verbose)
{
if (out) {System.out.println (msg);}
}
/*
* prepare activity by getting the control parameters ready
* create client object ready to use
*/
public void initialize (Properties props, boolean verbose)
{
this.verbose = verbose;
log("initializing OCIOutputter ....");
ConfigFileReader.ConfigFile configFile = null;
logOCID = props.get(LOGICID);
String name = props.get(AGENTNAME);
if ((name != null) && (name.trim().length() > 0))
{
agentName = name;
log ("log id:"+agentName);
}
String prfileGrp = props.get(PROPFILEGROUP);
if ((prfileGrp != null) && (prfileGrp.trim().length() > 0))
{
profileGroup = prfileGrp;
log ("profile group id:"+prfileGrp);
}
String batchSze = props.get(BATCHSIZE);
if ((batchSze != null) && (batchSze.trim().length() > 0 ))
{
try
{
batchSize = Integer.valueOf(batchSze);
batch = new ArrayList();
}
catch (NumberFormatException numErr)
{
log ("Failed to translate batch size default to no batch");
}
}
String OCIconfigLocation = props.get(OCICONFIGFILE);
if ((OCIconfigLocation == null) || (OCIconfigLocation.trim().length() == 0))
{
log("Using default config for OCI properties - " + ConfigFileReader.DEFAULT_FILE_PATH);
configFile = ConfigFileReader.parseDefault();
}
else
{
OCIconfigLocation = OCIconfigLocation.trim();
configFile = ConfigFileReader.parse(OCIconfigLocation, profileGroup);
}
final AuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider(configFile);
client = new LoggingClient(provider);
log ("... initialized OCIOutputter");
}
/*
* Take the provided log entry and either add it to the batch if a batch size is set
* or send it immediately to OCI
*/
public void writeLogEntry(String entry)
{
PutLogsDetails putLogsDetails = null;
Date timestamp = new Date();
if (batchSize > 0)
{
batch.add(entry);
if (batchSize == batch.size())
{
log ("preparing to send " + batch.size())
putLogsDetails = PutLogsDetails.builder()
.specversion("1.0")
.logEntryBatches(new ArrayList<>(Arrays.asList(LogEntryBatch.builder()
.entries(new ArrayList<>(Arrays.asList(LogEntry.builder()
.data(entry)
.id(logOCID)
.time(timestamp).build())))
.source(agentName)
.type(AGENTTYPE)
.subject(logSubject)
.defaultlogentrytime(timestamp).build()))).build();
batch.clear();
}
}
else
{
/* Create a request and dependent object(s). */
putLogsDetails = PutLogsDetails.builder()
.specversion("1.0")
.logEntryBatches(new ArrayList<>(Arrays.asList(LogEntryBatch.builder()
.entries(new ArrayList<>(Arrays.asList(LogEntry.builder()
.data(entry)
.id(logOCID)
.time(timestamp).build())))
.source(agentName)
.type(AGENTTYPE)
.subject(logSubject)
.defaultlogentrytime(timestamp).build()))).build();
}
if (putLogsDetails != null)
{
PutLogsRequest putLogsRequest = PutLogsRequest.builder()
.logId(logOCID)
.putLogsDetails(putLogsDetails)
.timestampOpcAgentProcessing(timestamp)
.build();
/* Send request to the Client */
PutLogsResponse response = client.putLogs(putLogsRequest);
if (verbose) {System.out.println ("OCI Outputter:" + response.toString());}
}
}
public void clearDown()
{
log ("OCI Outputter - clearing down")
if ((batch != null) && (batch.size() > 0))
{
putLogsDetails = PutLogsDetails.builder()
.specversion("1.0")
.logEntryBatches(new ArrayList<>(Arrays.asList(LogEntryBatch.builder()
.entries(new ArrayList<>(Arrays.asList(LogEntry.builder()
.data(entry)
.id(logOCID)
.time(timestamp).build())))
.source(agentName)
.type(AGENTTYPE)
.subject(logSubject)
.defaultlogentrytime(timestamp).build()))).build();
batch.clear();
PutLogsRequest putLogsRequest = PutLogsRequest.builder()
.logId(logOCID)
.putLogsDetails(putLogsDetails)
.timestampOpcAgentProcessing(timestamp)
.build();
/* Send request to the Client */
PutLogsResponse response = client.putLogs(putLogsRequest);
if (verbose) {System.out.println ("OCI Outputter:" + response.toString());}
}
}
}