Skip to content

add convenience event emitting api to OpenTelemetryRum #892

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.android;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.TraceId;

enum NoopOpenTelemetryRum implements OpenTelemetryRum {
Expand All @@ -21,4 +22,7 @@ public String getRumSessionId() {
// RUM session.id has the same format as traceId
return TraceId.getInvalid();
}

@Override
public void emitEvent(String eventName, String body, Attributes attributes) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.android.config.OtelRumConfig;
import io.opentelemetry.android.internal.services.Services;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
Expand Down Expand Up @@ -88,4 +89,52 @@ static OpenTelemetryRum noop() {
* recommended that you do not cache this value, but always retrieve it from here when needed.
*/
String getRumSessionId();

/**
* Emits an event with the specified name.
*
* <p>This method serves as a convenience overload that emits an event with an empty set of
* attributes.
*
* @param eventName The name of the event to emit.
*/
default void emitEvent(String eventName) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about doing this in Kotlin and collapsing all these methods into one and use default arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it looks like that's very possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: it won't be very nice to Java users though.

Copy link
Member

Choose a reason for hiding this comment

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

you can add the annotation @JvmOverloads so kotlin generates overloads for the java callers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like that would require making the function a class method instead of being declare in an interface?

Copy link
Member

Choose a reason for hiding this comment

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

you can just do method overloads with kotlin anyway so its ok to convert it to kotlin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh true that, you can make this interface an abstract class but not sure if that's what you want

i was thinking the same. however, i don't know whether others would want it to changed to a class.

you can just do method overloads with kotlin anyway so its ok to convert it to kotlin

it looks like that defeats the purpose of converting to kotlin?

Copy link
Member

Choose a reason for hiding this comment

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

it looks like that defeats the purpose of converting to kotlin?

i think we all want at some point that the codebase is kotlin only when it makes sense.
all new code ideally is kotlin, if its not a big pain.

your call here since it works already

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe for now just keep this in Java and we can convert the whole class to Kotlin at a later point. There might be some binary compatibility issues in that case, but we can do it in a version bump where doing some trivial compile time breaks are ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good

emitEvent(eventName, Attributes.empty());
}

/**
* Emits an event with the specified name and body.
*
* <p>This method serves as a convenience overload that emits an event with an empty set of
* attributes.
*
* @param eventName The name of the event to emit.
* @param body The body of the event, typically containing additional data.
*/
default void emitEvent(String eventName, String body) {
emitEvent(eventName, body, Attributes.empty());
}

/**
* Emits an event with the specified name and attributes.
*
* <p>This method serves as a convenience overload that emits an event with an empty body.
*
* @param eventName The name of the event to emit.
* @param attributes The attributes associated with the event.
*/
default void emitEvent(String eventName, Attributes attributes) {
emitEvent(eventName, "", attributes);
}

/**
* Emits an event with the specified name, body, and attributes.
*
* <p>Implementations of this method should define how the event is handled and recorded.
*
* @param eventName The name of the event to emit.
* @param body The body of the event, typically containing additional data.
* @param attributes The attributes associated with the event, providing metadata.
*/
void emitEvent(String eventName, String body, Attributes attributes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@

import io.opentelemetry.android.session.SessionManager;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder;
import io.opentelemetry.api.incubator.logs.ExtendedLogger;
import io.opentelemetry.sdk.OpenTelemetrySdk;

final class OpenTelemetryRumImpl implements OpenTelemetryRum {

private final OpenTelemetrySdk openTelemetrySdk;
private final SessionManager sessionManager;

private final ExtendedLogger logger;

OpenTelemetryRumImpl(OpenTelemetrySdk openTelemetrySdk, SessionManager sessionManager) {
this.openTelemetrySdk = openTelemetrySdk;
this.sessionManager = sessionManager;
this.logger =
(ExtendedLogger)
openTelemetrySdk
.getLogsBridge()
.loggerBuilder("io.opentelemetry.rum.events")
.build();
}

@Override
Expand All @@ -28,4 +39,10 @@ public OpenTelemetry getOpenTelemetry() {
public String getRumSessionId() {
return sessionManager.getSessionId();
}

@Override
public void emitEvent(String eventName, String body, Attributes attributes) {
ExtendedLogRecordBuilder logRecordBuilder = logger.logRecordBuilder();
logRecordBuilder.setEventName(eventName).setBody(body).setAllAttributes(attributes).emit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import io.opentelemetry.android.internal.session.SessionIdTimeoutHandler;
import io.opentelemetry.android.session.SessionManager;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.metrics.LongCounter;
Expand Down Expand Up @@ -182,19 +181,8 @@ public void shouldBuildLogRecordProvider() {
SimpleLogRecordProcessor.create(logsExporter)))
.build();

Logger logger =
openTelemetryRum
.getOpenTelemetry()
.getLogsBridge()
.loggerBuilder("otel.initialization.events")
.build();
ExtendedLogRecordBuilder eventLogger = (ExtendedLogRecordBuilder) logger.logRecordBuilder();
Attributes attrs = Attributes.of(stringKey("mega"), "hit");
eventLogger
.setEventName("test.event")
.setAllAttributes(attrs)
.setAttribute(stringKey("body.field"), "foo")
.emit();
Attributes attrs = Attributes.of(stringKey("mega"), "hit", stringKey("body.field"), "foo");
openTelemetryRum.emitEvent("test.event", attrs);

List<LogRecordData> logs = logsExporter.getFinishedLogRecordItems();
assertThat(logs).hasSize(1);
Expand Down