Skip to content

Commit 9f9391f

Browse files
committed
Add OTEL standard classes for Logs, Traces and Metrics
Signed-off-by: Krishna Kondaka <krishkdk@amazon.com>
1 parent 0a862af commit 9f9391f

25 files changed

+4258
-0
lines changed

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/log/JacksonOtelLog.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
public class JacksonOtelLog extends JacksonEvent implements OpenTelemetryLog {
2626

2727
protected static final String OBSERVED_TIME_KEY = "observedTime";
28+
protected static final String SCOPE_KEY = "scope";
29+
protected static final String RESOURCE_KEY = "resource";
2830
protected static final String TIME_KEY = "time";
2931
protected static final String SERVICE_NAME_KEY = "serviceName";
3032
protected static final String ATTRIBUTES_KEY = "attributes";
@@ -94,6 +96,16 @@ public String getSeverityText() {
9496
return this.get(SEVERITY_TEXT_KEY, String.class);
9597
}
9698

99+
@Override
100+
public Map<String, Object> getScope() {
101+
return this.get(SCOPE_KEY, Map.class);
102+
}
103+
104+
@Override
105+
public Map<String, Object> getResource() {
106+
return this.get(RESOURCE_KEY, Map.class);
107+
}
108+
97109
@Override
98110
public Integer getDroppedAttributesCount() {
99111
return this.get(DROPPED_ATTRIBUTES_COUNT_KEY, Integer.class);
@@ -221,6 +233,30 @@ public Builder withSchemaUrl(final String schemaUrl) {
221233
return getThis();
222234
}
223235

236+
/**
237+
* Sets the scope of the log event
238+
*
239+
* @param scope scope to be set
240+
* @return the builder
241+
* @since 2.11
242+
*/
243+
public Builder withScope(final Map<String, Object> scope) {
244+
data.put(SCOPE_KEY, scope);
245+
return getThis();
246+
}
247+
248+
/**
249+
* Sets the resource of the log event
250+
*
251+
* @param resource resource to be set
252+
* @return the builder
253+
* @since 2.11
254+
*/
255+
public Builder withResource(final Map<String, Object> resource) {
256+
data.put(RESOURCE_KEY, resource);
257+
return getThis();
258+
}
259+
224260
/**
225261
* Sets the flags that are associated with this log event
226262
*
Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.dataprepper.model.log;
7+
8+
import org.opensearch.dataprepper.model.event.EventType;
9+
import org.opensearch.dataprepper.model.event.JacksonEvent;
10+
11+
import java.time.Instant;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static com.google.common.base.Preconditions.checkArgument;
16+
17+
/**
18+
* A Jackson implementation for standard {@link OpenTelemetryLog}.
19+
*
20+
* @since 2.11
21+
*/
22+
public class JacksonStandardOTelLog extends JacksonEvent implements OpenTelemetryLog {
23+
24+
protected static final String OBSERVED_TIME_KEY = "observed_time";
25+
protected static final String SCOPE_KEY = "scope";
26+
protected static final String RESOURCE_KEY = "resource";
27+
protected static final String TIME_KEY = "time";
28+
protected static final String SERVICE_NAME_KEY = "service_name";
29+
protected static final String ATTRIBUTES_KEY = "attributes";
30+
protected static final String SCHEMA_URL_KEY = "schema_url";
31+
protected static final String FLAGS_KEY = "flags";
32+
protected static final String BODY_KEY = "body";
33+
protected static final String SPAN_ID_KEY = "span_id";
34+
protected static final String TRACE_ID_KEY = "trace_id";
35+
protected static final String SEVERITY_NUMBER_KEY = "severity_number";
36+
protected static final String SEVERITY_TEXT_KEY = "severity_text";
37+
protected static final String DROPPED_ATTRIBUTES_COUNT_KEY = "dropped_attributes_count";
38+
39+
40+
protected JacksonStandardOTelLog(final JacksonStandardOTelLog.Builder builder) {
41+
super(builder);
42+
43+
checkArgument(this.getMetadata().getEventType().equals("LOG"), "eventType must be of type Log");
44+
}
45+
46+
@Override
47+
public String getServiceName() {
48+
return this.get(SERVICE_NAME_KEY, String.class);
49+
}
50+
51+
@Override
52+
public String getObservedTime() {
53+
return this.get(OBSERVED_TIME_KEY, String.class);
54+
}
55+
56+
@Override
57+
public String getTime() {
58+
return this.get(TIME_KEY, String.class);
59+
}
60+
61+
@Override
62+
public Map<String, Object> getAttributes() {
63+
return this.get(ATTRIBUTES_KEY, Map.class);
64+
}
65+
66+
@Override
67+
public String getSchemaUrl() {
68+
return this.get(SCHEMA_URL_KEY, String.class);
69+
}
70+
71+
@Override
72+
public Integer getFlags() {
73+
return this.get(FLAGS_KEY, Integer.class);
74+
}
75+
76+
@Override
77+
public String getSpanId() {
78+
return this.get(SPAN_ID_KEY, String.class);
79+
}
80+
81+
@Override
82+
public String getTraceId() {
83+
return this.get(TRACE_ID_KEY, String.class);
84+
}
85+
86+
@Override
87+
public Integer getSeverityNumber() {
88+
return this.get(SEVERITY_NUMBER_KEY, Integer.class);
89+
}
90+
91+
@Override
92+
public String getSeverityText() {
93+
return this.get(SEVERITY_TEXT_KEY, String.class);
94+
}
95+
96+
@Override
97+
public Map<String, Object> getScope() {
98+
return this.get(SCOPE_KEY, Map.class);
99+
}
100+
101+
@Override
102+
public Map<String, Object> getResource() {
103+
return this.get(RESOURCE_KEY, Map.class);
104+
}
105+
106+
@Override
107+
public Integer getDroppedAttributesCount() {
108+
return this.get(DROPPED_ATTRIBUTES_COUNT_KEY, Integer.class);
109+
}
110+
111+
@Override
112+
public Object getBody() {
113+
return this.get(BODY_KEY, Object.class);
114+
}
115+
116+
/**
117+
* Constructs an empty builder.
118+
*
119+
* @return a builder
120+
* @since 2.11
121+
*/
122+
public static JacksonStandardOTelLog.Builder builder() {
123+
return new JacksonStandardOTelLog.Builder();
124+
}
125+
126+
/**
127+
* Builder for creating {@link JacksonLog}.
128+
*
129+
* @since 2.11
130+
*/
131+
public static class Builder extends JacksonEvent.Builder<JacksonStandardOTelLog.Builder> {
132+
133+
protected final Map<String, Object> data;
134+
135+
public Builder() {
136+
data = new HashMap<>();
137+
}
138+
139+
@Override
140+
public JacksonStandardOTelLog.Builder getThis() {
141+
return this;
142+
}
143+
144+
/**
145+
* Optional - sets the attributes for this event. Default is an empty map.
146+
*
147+
* @param attributes the attributes to associate with this event.
148+
* @return the builder
149+
* @since 2.11
150+
*/
151+
public Builder withAttributes(final Map<String, Object> attributes) {
152+
data.put(ATTRIBUTES_KEY, attributes);
153+
return getThis();
154+
}
155+
156+
/**
157+
* Sets the time received for populating event origination time in event handle
158+
*
159+
* @param timeReceived time received
160+
* @return the builder
161+
* @since 2.11
162+
*/
163+
@Override
164+
public Builder withTimeReceived(final Instant timeReceived) {
165+
return (Builder)super.withTimeReceived(timeReceived);
166+
}
167+
168+
/**
169+
* Sets the observed time of the log event
170+
*
171+
* @param observedTime the start time
172+
* @return the builder
173+
* @since 2.11
174+
*/
175+
public Builder withObservedTime(final String observedTime) {
176+
data.put(OBSERVED_TIME_KEY, observedTime);
177+
return getThis();
178+
}
179+
180+
/**
181+
* Sets the time for the log event.
182+
*
183+
* @param time the moment corresponding to when the data point's aggregate value was captured.
184+
* @return the builder
185+
* @since 2.11
186+
*/
187+
public Builder withTime(final String time) {
188+
data.put(TIME_KEY, time);
189+
return getThis();
190+
}
191+
192+
/**
193+
* Sets the service name of the log event
194+
* @param serviceName sets the name of the service
195+
* @return the builder
196+
* @since 2.11
197+
*/
198+
public Builder withServiceName(final String serviceName) {
199+
data.put(SERVICE_NAME_KEY, serviceName);
200+
return getThis();
201+
}
202+
203+
/**
204+
* Sets the schema url of the log event
205+
*
206+
* @param schemaUrl sets the url of the schema
207+
* @return the builder
208+
* @since 2.11
209+
*/
210+
public Builder withSchemaUrl(final String schemaUrl) {
211+
data.put(SCHEMA_URL_KEY, schemaUrl);
212+
return getThis();
213+
}
214+
215+
/**
216+
* Sets the scope of the log event
217+
*
218+
* @param scope scope to be set
219+
* @return the builder
220+
* @since 2.11
221+
*/
222+
public Builder withScope(final Map<String, Object> scope) {
223+
data.put(SCOPE_KEY, scope);
224+
return getThis();
225+
}
226+
227+
/**
228+
* Sets the resource of the log event
229+
*
230+
* @param resource resource to be set
231+
* @return the builder
232+
* @since 2.11
233+
*/
234+
public Builder withResource(final Map<String, Object> resource) {
235+
data.put(RESOURCE_KEY, resource);
236+
return getThis();
237+
}
238+
239+
/**
240+
* Sets the flags that are associated with this log event
241+
*
242+
* @param flags sets the flags for this log
243+
* @return the builder
244+
* @since 2.11
245+
*/
246+
public Builder withFlags(final Integer flags) {
247+
data.put(FLAGS_KEY, flags);
248+
return getThis();
249+
}
250+
251+
/**
252+
* Sets the body that are associated with this log event
253+
*
254+
* @param body sets the body of this log event
255+
* @return the builder
256+
* @since 2.11
257+
*/
258+
public Builder withBody(final Object body) {
259+
data.put(BODY_KEY, body);
260+
return getThis();
261+
}
262+
263+
/**
264+
* Sets the span id if a span is associated with this log event
265+
*
266+
* @param spanId sets the span id of this log event
267+
* @return the builder
268+
* @since 2.11
269+
*/
270+
public Builder withSpanId(final String spanId) {
271+
data.put(SPAN_ID_KEY, spanId);
272+
return getThis();
273+
}
274+
275+
/**
276+
* Sets the trace id if a trace is associated with this log event
277+
*
278+
* @param traceId sets trace id of this log event
279+
* @return the builder
280+
* @since 2.11
281+
*/
282+
public Builder withTraceId(final String traceId) {
283+
data.put(TRACE_ID_KEY, traceId);
284+
return getThis();
285+
}
286+
287+
/**
288+
* Sets the severity number of this log event, uses its numerical value
289+
*
290+
* @param severityNumber sets the severity number of this log event
291+
* @return the builder
292+
* @since 2.11
293+
*/
294+
public Builder withSeverityNumber(final Integer severityNumber) {
295+
data.put(SEVERITY_NUMBER_KEY, severityNumber);
296+
return getThis();
297+
}
298+
299+
/**
300+
* Sets the severity text of this log event
301+
*
302+
* @param severityText sets the severity text of this log event
303+
* @return the builder
304+
* @since 2.11
305+
*/
306+
public Builder withSeverityText(final String severityText) {
307+
data.put(SEVERITY_TEXT_KEY, severityText);
308+
return getThis();
309+
}
310+
311+
/**
312+
* Sets the dropped attributes count of this log event
313+
*
314+
* @param droppedAttributesCount sets the dropped attributes count of this log event
315+
* @return the builder
316+
* @since 2.11
317+
*/
318+
public Builder withDroppedAttributesCount(final Integer droppedAttributesCount) {
319+
data.put(DROPPED_ATTRIBUTES_COUNT_KEY, droppedAttributesCount);
320+
return getThis();
321+
}
322+
323+
/**
324+
* Returns a newly created {@link JacksonStandardOTelLog}.
325+
*
326+
* @return a log
327+
* @since 2.11
328+
*/
329+
public JacksonStandardOTelLog build() {
330+
this.withEventType(EventType.LOG.toString());
331+
this.withData(data);
332+
checkAndSetDefaultValues();
333+
return new JacksonStandardOTelLog(this);
334+
}
335+
336+
private void checkAndSetDefaultValues() {
337+
data.computeIfAbsent(ATTRIBUTES_KEY, k -> new HashMap<>());
338+
}
339+
}
340+
}

0 commit comments

Comments
 (0)