8000 Add Teams specific telemetry properties · TaffyWrinkle/botbuilder-python@9c2c1eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c2c1eb

Browse files
committed
Add Teams specific telemetry properties
1 parent 985969d commit 9c2c1eb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

libraries/botbuilder-core/botbuilder/core/telemetry_logger_middleware.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
"""Middleware Component for logging Activity messages."""
4-
54
from typing import Awaitable, Callable, List, Dict
65
from botbuilder.schema import Activity, ConversationReference, ActivityTypes
6+
from botbuilder.schema.teams import TeamsChannelData, TeamInfo
7+
from botframework.connector import Channels
8+
79
from .bot_telemetry_client import BotTelemetryClient
810
from .bot_assert import BotAssert
911
from .middleware_set import Middleware
@@ -183,6 +185,10 @@ async def fill_receive_event_properties(
183185
if activity.speak and activity.speak.strip():
184186
properties[TelemetryConstants.SPEAK_PROPERTY] = activity.speak
185187

188+
TelemetryLoggerMiddleware.__populate_additional_channel_properties(
189+
activity, properties
190+
)
191+
186192
# Additional properties can override "stock" properties
187193
if additional_properties:
188194
for prop in additional_properties:
@@ -288,3 +294,25 @@ async def fill_delete_event_properties(
288294
properties[prop.key] = prop.value
289295

290296
return properties
297+
298+
@staticmethod
299+
def __populate_additional_channel_properties(
300+
activity: Activity, properties: dict,
301+
):
302+
if activity.channel_id == Channels.ms_teams:
303+
teams_channel_data: TeamsChannelData = activity.channel_data
304+
305+
properties["TeamsTenantId"] = (
306+
teams_channel_data.tenant
307+
if teams_channel_data and teams_channel_data.tenant
308+
else ""
309+
)
310+
311+
properties["TeamsUserAadObjectId"] = (
312+
activity.from_property.aad_object_id if activity.from_property else ""
313+
)
314+
315+
if teams_channel_data and teams_channel_data.team:
316+
properties["TeamsTeamInfo"] = TeamInfo.serialize(
317+
teams_channel_data.team
318+
)

libraries/botbuilder-core/tests/test_telemetry_middleware.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
from typing import Dict
88
from unittest.mock import Mock
99
import aiounittest
10+
from botframework.connector import Channels
11+
1012
from botbuilder.core import (
1113
NullTelemetryClient,
1214
TelemetryLoggerMiddleware,
1315
TelemetryLoggerConstants,
1416
TurnContext,
17+
MessageFactory,
1518
)
1619
from botbuilder.core.adapters import TestAdapter, TestFlow
1720
from botbuilder.schema import (
1821
Activity,
1922
ActivityTypes,
2023
ChannelAccount,
2124
ConversationAccount,
25+
ConversationReference,
2226
)
27+
from botbuilder.schema.teams import TeamInfo, TeamsChannelData, TenantInfo
2328

2429

2530
class TestTelemetryMiddleware(aiounittest.AsyncTestCase):
@@ -228,6 +233,47 @@ async def process(context: TurnContext) -> None:
228233
]
229234
self.assert_telemetry_calls(telemetry, telemetry_call_expected)
230235

236+
async def test_log_teams(self):
237+
telemetry = Mock()
238+
my_logger = TelemetryLoggerMiddleware(telemetry, True)
239+
240+
adapter = TestAdapter(
241+
template_or_conversation=ConversationReference(channel_id=Channels.ms_teams)
242+
)
243+
adapter.use(my_logger)
244+
245+
team_info = TeamInfo(id="teamId", name="teamName",)
246+
247+
channel_data = TeamsChannelData(
248+
team=team_info, tenant=TenantInfo(id="tenantId"),
249+
)
250+
251+
activity = MessageFactory.text("test")
252+
activity.channel_data = channel_data
253+
activity.from_property = ChannelAccount(
254+
id="userId", name="userName", aad_object_id="aaId",
255+
)
256+
257+
test_flow = TestFlow(None, adapter)
258+
await test_flow.send(activity)
259+
260+
telemetry_call_expected = [
261+
(
262+
TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT,
263+
{
264+
"text": "test",
265+
"fromId": "userId",
266+
"recipientId": "bot",
267+
"recipientName": "Bot",
268+
"TeamsTenantId": TenantInfo(id="tenantId"),
269+
"TeamsUserAadObjectId": "aaId",
270+
"TeamsTeamInfo": TeamInfo.serialize(team_info),
271+
},
272+
),
273+
]
274+
275+
self.assert_telemetry_calls(telemetry, telemetry_call_expected)
276+
231277
def create_reply(self, activity, text, locale=None):
232278
return Activity(
233279
type=ActivityTypes.message,

0 commit comments

Comments
 (0)
0