Skip to content

Conversation

@brizental
Copy link
Contributor

No description provided.

@brizental brizental requested a review from Dexterp37 March 3, 2021 14:02
readonly serverEndpoint?: string,
// Debug configuration.
debug?: DebugOptions,
// Optional list of plugins to instrument the current Glean instance.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Optional list of plugins to instrument the current Glean instance.
// Optional list of plugins to enable in the current Glean instance.


import { DEFAULT_TELEMETRY_ENDPOINT } from "./constants";
import GleanPlugin from "../plugins";
import GleanEvents from "./events";
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are types, we pass plugins through the ConfigurationInterface to Glean.initialize, and this is here to help define exactly what we are passing through that property.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GleanEvents is a union of all the possible GleanEvents types (not a union right now since we only defined one event, but you get the gist). And GleanPlugin is the type of the accepted plugin class. Each plugin is always attached to a type of event. To ensure we only accept plugins that listen to valid Glean events, the final type for the plugins property is GleanPlugin<GleanEvents>[].


import { GleanEvent } from "../plugins/index";

export const AfterPingCollection: GleanEvent<[PingPayload], Promise<JSONObject>> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you document what's this? When is this called? What's it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, this is here to define:

  • The type of the context provided by this event ([PingPayload]);
  • The type of the result expected from this event ([Promise<JSONObject>]);
  • The name of this event.

This way, when we define a plugin like this class MyPlugin extends GleanPlugin<typeof AfterPingCollection> the action in this plugin will already expected the correct arguments and return type defined by the event.

for (const plugin of plugins) {
if (plugin.event in Glean.plugins) {
console.error(
`Attempted to register plugin '${plugin.name}', which instruments the event '${plugin.event}'.`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Mh, what do you mean by "instrument"? We usually use this term to mean "add something to measure".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum, maybe I am using this word wrong... checks wikipedia ... 100% I am using it wrong 🆘 Maybe I want "listens to"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thankfully I can always claim that English is not my first language 🥇

console.info(JSON.stringify(collectedPayload, null, 2));
}

const modifiedPayload = await Glean.triggerEvent(AfterPingCollection, collectedPayload);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should consider (not in this PR) a different way to inject the dependency here, reducing the calls to the Glean object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered making events completely unaware of the Glean singleton, but didn't try that out. I do think if we are going to go that way it should be now, otherwise we will be a bit stuck.

Let me attempt that and open an alternate to this PR with the different approach. I did not go that way before, because it would be quite different from what I defined in the proposal for this, but there is no harm in testing.

Comment on lines +231 to +236
const modifiedPayload = await Glean.triggerEvent(AfterPingCollection, collectedPayload);
let payload: JSONObject;
if (modifiedPayload) {
payload = modifiedPayload;
} else {
payload = collectedPayload;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const modifiedPayload = await Glean.triggerEvent(AfterPingCollection, collectedPayload);
let payload: JSONObject;
if (modifiedPayload) {
payload = modifiedPayload;
} else {
payload = collectedPayload;
const modifiedPayload = await Glean.triggerEvent(AfterPingCollection, collectedPayload) ?? collectedPayload;

What about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Much better.

}
}

await Glean.testUninitialize();
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish we could just test this in isolation. We might want to reconsider how we propagate our dependencies within Glean.js :(

@brizental
Copy link
Contributor Author

Closing in favour of #95

@brizental brizental closed this Mar 4, 2021
@brizental brizental deleted the 1689547-plugins branch March 4, 2021 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants