Skip to content
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
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ opentelemetry-api = { module = "io.opentelemetry:opentelemetry-api" }
opentelemetry-sdk = { module = "io.opentelemetry:opentelemetry-sdk" }
opentelemetry-exporter-zipkin = { module = "io.opentelemetry:opentelemetry-exporter-zipkin" }
opentelemetry-exporter-logging = { module = "io.opentelemetry:opentelemetry-exporter-logging" }
opentelemetry-apievents = { module = "io.opentelemetry:opentelemetry-api-events" }
zipkin-sender-okhttp3 = { module = "io.zipkin.reporter2:zipkin-sender-okhttp3", version.ref = "zipkin-reporter"}

#Test tools
Expand Down
3 changes: 3 additions & 0 deletions instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ dependencies {
implementation(libs.opentelemetry.exporter.logging)
implementation(libs.opentelemetry.instrumentation.api)
implementation(libs.opentelemetry.semconv)
implementation(libs.opentelemetry.apievents)

implementation("io.opentelemetry:opentelemetry-api-events")

testImplementation(libs.bundles.mockito)
testImplementation(libs.bundles.junit)
Expand Down
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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"), stacktrace.toString());
tonzhan2 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(data.get("thread"), Thread.currentThread().toString());
assertEquals(data.get("throwableMessage"), "Crash!");
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")));
}
}