-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Event Emitter with Data #122
Closed
tonzhan2
wants to merge
6
commits into
open-telemetry:main
from
tonzhan2:develop/addEventEmitterWithData
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3d3d72
added implementation, unit test, gradle dependencies
tonzhan2 3be7bc5
spotless apply
tonzhan2 53c6422
added actual data from exception and thread into test cases
tonzhan2 4228a7a
run git spotlessapply
tonzhan2 cea3b1f
change test cases to check for attributes in dictionary
tonzhan2 784f79f
spotless apply
tonzhan2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
instrumentation/src/main/java/io/opentelemetry/android/emitter/EventEmitterWithData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.emitter; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.api.events.EventEmitter; | ||
import java.util.Map; | ||
|
||
class EventEmitterWithData { | ||
|
||
private static final String EVENT_DATA = "event.data"; | ||
|
||
private final EventEmitter delegate; | ||
|
||
EventEmitterWithData(EventEmitter delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public void emit(String eventName, Attributes attributes, Map<String, Object> data) { | ||
Attributes dataAsAttribute = convertRichDataObjectIntoAttributes(data); | ||
Attributes eventAttrs = | ||
attributes.toBuilder().put(EVENT_DATA, dataAsAttribute.toString()).build(); | ||
delegate.emit(eventName, eventAttrs); | ||
} | ||
|
||
public Attributes convertRichDataObjectIntoAttributes(Map<String, Object> data) { | ||
AttributesBuilder builder = Attributes.builder(); | ||
for (String key : data.keySet()) { | ||
Object val = data.get(key); | ||
if (val != null) { | ||
if (val instanceof String) { | ||
builder.put(AttributeKey.stringKey(key), (String) val); | ||
} else if (val instanceof Long) { | ||
builder.put(AttributeKey.longKey(key), (Long) val); | ||
} else if (val instanceof Double) { | ||
builder.put(AttributeKey.doubleKey(key), (Double) val); | ||
} else if (val instanceof Boolean) { | ||
builder.put(AttributeKey.booleanKey(key), (Boolean) val); | ||
} else { | ||
builder.put(AttributeKey.stringKey(key), val.toString()); | ||
} | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
instrumentation/src/test/java/io/opentelemetry/android/emitter/EventEmitterWithDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.android.emitter; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
public class EventEmitterWithDataTest { | ||
|
||
@Test | ||
public void testConvertRichDataObjectIntoAttributes() { | ||
Exception crashData = null; | ||
try { | ||
throw new RuntimeException("Crash!"); | ||
} catch (Exception e) { | ||
crashData = e; | ||
} | ||
|
||
StringWriter stacktrace = new StringWriter(); | ||
crashData.printStackTrace(new PrintWriter(stacktrace)); | ||
|
||
Map<String, Object> data = new HashMap<>(); | ||
data.put("stringKey", "key"); | ||
tonzhan2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data.put("longKey", 123L); | ||
data.put("doubleKey", 12.3); | ||
data.put("booleanKey", true); | ||
|
||
data.put("stacktrace", stacktrace.toString()); | ||
data.put("thread", Thread.currentThread().toString()); | ||
data.put("throwableMessage", crashData.getMessage()); | ||
|
||
EventEmitterWithData emitter = new EventEmitterWithData(null); | ||
Attributes attributes = emitter.convertRichDataObjectIntoAttributes(data); | ||
|
||
assertEquals(attributes.size(), data.size()); | ||
assertEquals(data.get("stacktrace"), attributes.get(AttributeKey.stringKey("stacktrace"))); | ||
assertEquals(data.get("thread"), attributes.get(AttributeKey.stringKey("thread"))); | ||
assertEquals( | ||
data.get("throwableMessage"), | ||
attributes.get(AttributeKey.stringKey("throwableMessage"))); | ||
assertEquals(data.get("stringKey"), attributes.get(AttributeKey.stringKey("stringKey"))); | ||
assertEquals(data.get("longKey"), attributes.get(AttributeKey.longKey("longKey"))); | ||
assertEquals(data.get("doubleKey"), attributes.get(AttributeKey.doubleKey("doubleKey"))); | ||
assertEquals(data.get("booleanKey"), attributes.get(AttributeKey.booleanKey("booleanKey"))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to consider the list types? https://github.com/open-telemetry/opentelemetry-java/blob/48d41d3a5aa27ff8e5e4033ef0c8c3ba0e53f683/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java#L49-L67