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

Events for c8y are consumed from te/+/+/+/+/e/+ #2221

Conversation

rina23q
Copy link
Member

@rina23q rina23q commented Sep 5, 2023

Proposed changes

Events for c8y should be consumed from the mqtt topic te/+/+/+/+/e/+ instead of tedge/events/#.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#2214

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@@ -21,13 +21,13 @@ Child devices support sending custom measurements


Child devices support sending custom events
Execute Command tedge mqtt pub tedge/events/myCustomType1/${CHILD_SN} '{ "text": "Some test event", "someOtherCustomFragment": {"nested":{"value": "extra info"}} }'
Execute Command tedge mqtt pub te/device/${CHILD_SN}///e/myCustomType1 '{ "text": "Some test event", "someOtherCustomFragment": {"nested":{"value": "extra info"}} }'
${events}= Device Should Have Event/s expected_text=Some test event with_attachment=False minimum=1 maximum=1 type=myCustomType1 fragment=someOtherCustomFragment
Log ${events}


Child devices support sending custom events overriding the type
Copy link
Member Author

Choose a reason for hiding this comment

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

This test gives false-positive to the current main branch. It cannot detect if two "type" fields are added as the test check only if the payload "contains" a type=otherType.

In my opinion, this test can be covered by Rust code as below.

async fn convert_event_use_event_type_from_payload() {
let tmp_dir = TempTedgeDir::new();
let (mut converter, _http_proxy) = create_c8y_converter(&tmp_dir).await;
let event_topic = "te/device/main///e/topic_event";
let event_payload = r#"{ ".type": "payload_event", "text": "Someone clicked", "time": "2020-02-02T01:02:03+05:30" }"#;
let event_message = Message::new(&Topic::new_unchecked(event_topic), event_payload);
let converted_events = converter.convert(&event_message).await;
assert_eq!(converted_events.len(), 1);
let converted_event = converted_events.get(0).unwrap();
assert_eq!(converted_event.topic.name, "c8y/s/us");
assert_eq!(
converted_event.payload_str().unwrap(),
r#"400,"payload_event","Someone clicked",2020-02-02T01:02:03+05:30"#
);
}

@@ -27,13 +27,13 @@ Thin-edge devices support sending custom measurements


Thin-edge devices support sending custom events
Execute Command tedge mqtt pub tedge/events/myCustomType1 '{ "text": "Some test event", "someOtherCustomFragment": {"nested":{"value": "extra info"}} }'
Execute Command tedge mqtt pub te/device/main///e/myCustomType1 '{ "text": "Some test event", "someOtherCustomFragment": {"nested":{"value": "extra info"}} }'
${events}= Device Should Have Event/s expected_text=Some test event with_attachment=False minimum=1 maximum=1 type=myCustomType1 fragment=someOtherCustomFragment
Log ${events}


Thin-edge devices support sending custom events overriding the type
Copy link
Member Author

Choose a reason for hiding this comment

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

This test is also giving false-positive.

Copy link
Contributor

Choose a reason for hiding this comment

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

What is the false positive / how is it a false positive? Is the someOtherCustomFragment property not included in the event?

Copy link
Member Author

@rina23q rina23q Sep 6, 2023

Choose a reason for hiding this comment

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

Actually the current main branch has a bug. If user publishes an event with type field in the payload, the mapper creates "type" field twice.

  • "type":"myCustomType"
  • "type":"otherType"

But the test passed because the test checks if contains type=otherType.

Here is the trace.

[tedge/events/myCustomType] {"type": "otherType", "text": "Some test event", "someOtherCustomFragment": {"nested":{"value": "extra info"}} }
[c8y/event/events/create] {"type":"myCustomType","time":"2023-09-06T09:29:59.89872988Z","text":"Some test event","type":"otherType","someOtherCustomFragment":{"nested":{"value":"extra info"}}}

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, duplicating keys definitely should be avoided, whilst it is not forbidden it is not recommended and it causes massive issues with different libraries (some support duplicate object keys, some don't)..as the JSON spec says The names within an object SHOULD be unique.

@rina23q So is the fix to avoid duplicated keys also included in the PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

@rina23q So is the fix to avoid duplicated keys also included in the PR?

Yes ofc :) Rust test case convert_event_use_event_type_from_payload_to_c8y_json() is covering this.

@rina23q rina23q temporarily deployed to Test Pull Request September 5, 2023 15:32 — with GitHub Actions Inactive
@codecov
Copy link

codecov bot commented Sep 5, 2023

Codecov Report

Merging #2221 (8f6d256) into main (8edc2d8) will increase coverage by 1.1%.
Report is 12 commits behind head on main.
The diff coverage is 83.6%.

Additional details and impacted files
Files Changed Coverage Δ
crates/extensions/c8y_mapper_ext/src/converter.rs 79.9% <77.1%> (+1.7%) ⬆️
crates/core/c8y_api/src/json_c8y.rs 83.6% <84.0%> (-0.3%) ⬇️
crates/core/tedge_api/src/event.rs 95.1% <97.4%> (+4.0%) ⬆️
.../tedge_config/src/tedge_config_cli/tedge_config.rs 89.3% <100.0%> (ø)
crates/extensions/c8y_mapper_ext/src/config.rs 56.1% <100.0%> (-0.5%) ⬇️

... and 24 files with indirect coverage changes

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2023

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
263 0 5 263 100 47m50.221s

#[tedge_config(example = "tedge/alarms/#,te/+/+/+/+/m/+")]
#[tedge_config(default(value = "te/+/+/+/+/m/+,tedge/alarms/+/+,tedge/alarms/+/+/+,tedge/events/+,tedge/events/+/+,tedge/health/+,tedge/health/+/+"))]
#[tedge_config(example = "tedge/alarms/#,te/+/+/+/+/m/+,te/+/+/+/+/e/+")]
#[tedge_config(default(value = "te/+/+/+/+/m/+,te/+/+/+/+/e/+,tedge/alarms/+/+,tedge/alarms/+/+/+,tedge/health/+,tedge/health/+/+"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering why we're replacing the tedge/events topics with the te/.../e topics. Shouldn't they both coexist until we fully stop supporting the tedge/... topics? Else who will subscribe to the tedge/events topics and map those messages to their te/.../e equivalent?

Copy link
Member Author

@rina23q rina23q Sep 6, 2023

Choose a reason for hiding this comment

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

This is the configuration per cloud, so I believe it's safe to replace tedge/events with te/.../e topic for only c8y mapper. Still the default config of the aws and az mappers are subscribing to tedge/events (and tedge/measurements).

Also, don't miss that tedge-agent converts from tedge/... topics to te/... topics. This means c8y-mapper users can still use tedge/events topics to create events.

Copy link
Contributor

Choose a reason for hiding this comment

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

The agent does the translation from tedge/# to te/#.

This has been implemented by #2125

crates/core/tedge_api/src/event.rs Outdated Show resolved Hide resolved
Comment on lines -146 to -147
"tedge/events/+",
"tedge/events/+/+",
Copy link
Contributor

Choose a reason for hiding this comment

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

Seeing these tedge/ topics replaced by the te/ topics, while the former is only deprecated for the next release but not removed, I feel I'm missing something here. Especially since the same was done for tedge/measurements as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

The agent does the translation from tedge/# to te/#.

This has been implemented by #2125

} else {
false
let event_type = match event_type.is_empty() {
true => DEFAULT_EVENT_TYPE,
Copy link
Contributor

Choose a reason for hiding this comment

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

That's new. Earlier this was an error case, reported as a parsing error on tedge/errors topic. It was different for tedge/measurements as the type wasn't mandatory for it. But for events and alarms, the type was mandatory. So I hope this was intentional. @reubenmiller could you confirm?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes having a default type used for events will useful as it makes it easier to publish an event, and it aligns with the measurement type handling so it makes for a nice and consistent API.

Copy link
Contributor

Choose a reason for hiding this comment

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

And I'm pretty sure no one is actually subscribing to the tedge/errors topics, so usually they just wonder why the event is not being published.

* If input event type is empty, the converter uses "ThinEdgeEvent" as
the event type.
* Event converter uses entity store and no longer need to care if a
child device is already created.

Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
@rina23q rina23q force-pushed the feature/2082/c8y-mapper-forward-events-with-new-mqtt-scheme branch from 993a7c2 to 6332046 Compare September 6, 2023 10:04
@rina23q rina23q temporarily deployed to Test Pull Request September 6, 2023 15:28 — with GitHub Actions Inactive
crates/core/c8y_api/src/json_c8y.rs Outdated Show resolved Hide resolved
Comment on lines +292 to +302
// If the message doesn't contain any fields other than `text` and `time`, convert to SmartREST
let message = if c8y_event.extras.is_empty() {
let smartrest_event = Self::serialize_to_smartrest(&c8y_event)?;
let smartrest_topic = Topic::new_unchecked(SMARTREST_PUBLISH_TOPIC);
Message::new(&smartrest_topic, smartrest_event)
} else {
// If the message contains extra fields other than `text` and `time`, convert to Cumulocity JSON
let cumulocity_event_json = serde_json::to_string(&c8y_event)?;
let json_mqtt_topic = Topic::new_unchecked(C8Y_JSON_MQTT_EVENTS_TOPIC);
Message::new(&json_mqtt_topic, cumulocity_event_json)
};
Copy link
Contributor

Choose a reason for hiding this comment

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

We agreed offline to simplify this by systematically publishing the events over the c8y json topic. This will be done in a follow-up PR, though.

@rina23q rina23q temporarily deployed to Test Pull Request September 6, 2023 17:25 — with GitHub Actions Inactive
Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

Approved. Thank you

Copy link
Contributor

@reubenmiller reubenmiller left a comment

Choose a reason for hiding this comment

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

Looks good, though do we have a robot test case to cover publishing without the type in the topic?

@rina23q
Copy link
Member Author

rina23q commented Sep 7, 2023

Looks good, though do we have a robot test case to cover publishing without the type in the topic?

Robot case no. But I added the case to Rust convert_event_without_given_event_type()

Updated: Now two robot tests are added to cover it.

  • Child devices support sending custom events without type in topic
  • Thin-edge devices support sending custom events without type in topic

@rina23q rina23q force-pushed the feature/2082/c8y-mapper-forward-events-with-new-mqtt-scheme branch from e9feb77 to 55d2306 Compare September 7, 2023 10:16
Replace `tedge/events/#` topics used in Robot Framework
tests for c8y-mapper by `te/+/+/+/+/e/+`.

Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
@rina23q rina23q force-pushed the feature/2082/c8y-mapper-forward-events-with-new-mqtt-scheme branch from 55d2306 to 8f6d256 Compare September 7, 2023 10:31
@rina23q rina23q temporarily deployed to Test Pull Request September 7, 2023 10:41 — with GitHub Actions Inactive
@rina23q rina23q merged commit 7f58cd2 into thin-edge:main Sep 7, 2023
@rina23q rina23q deleted the feature/2082/c8y-mapper-forward-events-with-new-mqtt-scheme branch September 7, 2023 11:39
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.

4 participants