Skip to content

Commit 1fc3e30

Browse files
authoredApr 15, 2024··
Notebook telemetry (#222)
* Notebook telemetry * fix user agent * pass explicit notebook name * add notebook platform to user agent string * preserve the original client when running under nbtest * minor reporting updates * add telemetry to remaining search notebooks
1 parent bbe367f commit 1fc3e30

8 files changed

+319
-52
lines changed
 

‎notebooks/search/00-quick-start.ipynb

+69-39
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
},
7171
{
7272
"cell_type": "code",
73-
"execution_count": 2,
73+
"execution_count": null,
7474
"id": "WHC3hHGW-wbI",
7575
"metadata": {
7676
"id": "WHC3hHGW-wbI"
@@ -96,7 +96,7 @@
9696
},
9797
{
9898
"cell_type": "code",
99-
"execution_count": 3,
99+
"execution_count": null,
100100
"id": "f38e0397",
101101
"metadata": {
102102
"colab": {
@@ -137,17 +137,39 @@
137137
},
138138
{
139139
"cell_type": "markdown",
140-
"id": "1462ebd8",
141-
"metadata": {
142-
"id": "1462ebd8"
143-
},
140+
"id": "cb6ad7e9-0636-4cf3-a803-bf160fe16b33",
141+
"metadata": {},
144142
"source": [
145-
"Confirm that the client has connected with this test."
143+
"### Enable Telemetry\n",
144+
"\n",
145+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
146146
]
147147
},
148148
{
149149
"cell_type": "code",
150-
"execution_count": 10,
150+
"execution_count": null,
151+
"id": "3b04f442-729d-406d-b826-654483498df6",
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
156+
"from telemetry import enable_telemetry\n",
157+
"\n",
158+
"client = enable_telemetry(client, \"00-quick-start\")"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"id": "d12b707c-e89d-4b43-bee5-edb1beb84839",
164+
"metadata": {},
165+
"source": [
166+
"### Test the Client\n",
167+
"Before you continue, confirm that the client has connected with this test."
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 8,
151173
"id": "25c618eb",
152174
"metadata": {
153175
"colab": {
@@ -161,7 +183,7 @@
161183
"name": "stdout",
162184
"output_type": "stream",
163185
"text": [
164-
"{'name': 'instance-0000000011', 'cluster_name': 'd1bd36862ce54c7b903e2aacd4cd7f0a', 'cluster_uuid': 'tIkh0X_UQKmMFQKSfUw-VQ', 'version': {'number': '8.9.0', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '8aa461beb06aa0417a231c345a1b8c38fb498a0d', 'build_date': '2023-07-19T14:43:58.555259655Z', 'build_snapshot': False, 'lucene_version': '9.7.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}\n"
186+
"{'name': 'instance-0000000000', 'cluster_name': 'a72482be54904952ba46d53c3def7740', 'cluster_uuid': 'g8BE52TtT32pGBbRzP_oKA', 'version': {'number': '8.12.2', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '48a287ab9497e852de30327444b0809e55d46466', 'build_date': '2024-02-19T10:04:32.774273190Z', 'build_snapshot': False, 'lucene_version': '9.9.2', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}\n"
165187
]
166188
}
167189
],
@@ -195,7 +217,7 @@
195217
},
196218
{
197219
"cell_type": "code",
198-
"execution_count": 5,
220+
"execution_count": 9,
199221
"id": "_OAahfg-tqrf",
200222
"metadata": {
201223
"colab": {
@@ -211,7 +233,7 @@
211233
"ObjectApiResponse({'acknowledged': True})"
212234
]
213235
},
214-
"execution_count": 5,
236+
"execution_count": 9,
215237
"metadata": {},
216238
"output_type": "execute_result"
217239
}
@@ -232,7 +254,7 @@
232254
},
233255
{
234256
"cell_type": "code",
235-
"execution_count": 11,
257+
"execution_count": 10,
236258
"id": "6bc95238",
237259
"metadata": {
238260
"id": "6bc95238"
@@ -244,7 +266,7 @@
244266
"ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'book_index'})"
245267
]
246268
},
247-
"execution_count": 11,
269+
"execution_count": 10,
248270
"metadata": {},
249271
"output_type": "execute_result"
250272
}
@@ -281,7 +303,7 @@
281303
},
282304
{
283305
"cell_type": "code",
284-
"execution_count": 12,
306+
"execution_count": 13,
285307
"id": "008d723e",
286308
"metadata": {
287309
"id": "008d723e"
@@ -290,10 +312,10 @@
290312
{
291313
"data": {
292314
"text/plain": [
293-
"ObjectApiResponse({'took': 49, 'errors': False, 'items': [{'index': {'_index': 'book_index', '_id': 'HwOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'IAOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'IQOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'IgOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 3, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'IwOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 4, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'JAOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 5, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'JQOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 6, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'JgOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 7, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'JwOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 8, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'KAOa7osBiUNHLMdf3q2r', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 9, '_primary_term': 1, 'status': 201}}]})"
315+
"ObjectApiResponse({'errors': False, 'took': 88, 'items': [{'index': {'_index': 'book_index', '_id': 'caRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 0, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'cqRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 1, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'c6RpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 2, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'dKRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 3, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'daRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 4, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'dqRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 5, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'd6RpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 6, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'eKRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 7, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'eaRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 8, '_primary_term': 1, 'status': 201}}, {'index': {'_index': 'book_index', '_id': 'eqRpvY4BKY8PuI1qPluy', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 9, '_primary_term': 1, 'status': 201}}]})"
294316
]
295317
},
296-
"execution_count": 12,
318+
"execution_count": 13,
297319
"metadata": {},
298320
"output_type": "execute_result"
299321
}
@@ -330,7 +352,7 @@
330352
},
331353
{
332354
"cell_type": "code",
333-
"execution_count": 13,
355+
"execution_count": 14,
334356
"id": "f12ce2c9",
335357
"metadata": {
336358
"id": "f12ce2c9"
@@ -369,7 +391,7 @@
369391
},
370392
{
371393
"cell_type": "code",
372-
"execution_count": 14,
394+
"execution_count": 15,
373395
"id": "Df7hwcIjYwMT",
374396
"metadata": {
375397
"colab": {
@@ -384,16 +406,16 @@
384406
"output_type": "stream",
385407
"text": [
386408
"\n",
387-
"ID: JwOa7osBiUNHLMdf3q2r\n",
409+
"ID: eaRpvY4BKY8PuI1qPluy\n",
388410
"Publication date: 2008-05-15\n",
389411
"Title: JavaScript: The Good Parts\n",
390412
"Summary: A deep dive into the parts of JavaScript that are essential to writing maintainable code\n",
391413
"Publisher: oreilly\n",
392414
"Reviews: 51\n",
393415
"Authors: ['douglas crockford']\n",
394-
"Score: 0.80428284\n",
416+
"Score: 0.8042828\n",
395417
"\n",
396-
"ID: IwOa7osBiUNHLMdf3q2r\n",
418+
"ID: daRpvY4BKY8PuI1qPluy\n",
397419
"Publication date: 2015-03-27\n",
398420
"Title: You Don't Know JS: Up & Going\n",
399421
"Summary: Introduction to JavaScript and programming as a whole\n",
@@ -402,7 +424,7 @@
402424
"Authors: ['kyle simpson']\n",
403425
"Score: 0.6989136\n",
404426
"\n",
405-
"ID: JAOa7osBiUNHLMdf3q2r\n",
427+
"ID: dqRpvY4BKY8PuI1qPluy\n",
406428
"Publication date: 2018-12-04\n",
407429
"Title: Eloquent JavaScript\n",
408430
"Summary: A modern introduction to programming\n",
@@ -411,25 +433,25 @@
411433
"Authors: ['marijn haverbeke']\n",
412434
"Score: 0.6796988\n",
413435
"\n",
414-
"ID: HwOa7osBiUNHLMdf3q2r\n",
436+
"ID: caRpvY4BKY8PuI1qPluy\n",
415437
"Publication date: 2019-10-29\n",
416438
"Title: The Pragmatic Programmer: Your Journey to Mastery\n",
417439
"Summary: A guide to pragmatic programming for software engineers and developers\n",
418440
"Publisher: addison-wesley\n",
419441
"Reviews: 30\n",
420442
"Authors: ['andrew hunt', 'david thomas']\n",
421-
"Score: 0.62065494\n",
443+
"Score: 0.6206549\n",
422444
"\n",
423-
"ID: KAOa7osBiUNHLMdf3q2r\n",
445+
"ID: eqRpvY4BKY8PuI1qPluy\n",
424446
"Publication date: 2012-06-27\n",
425447
"Title: Introduction to the Theory of Computation\n",
426448
"Summary: Introduction to the theory of computation and complexity theory\n",
427449
"Publisher: cengage learning\n",
428450
"Reviews: 33\n",
429451
"Authors: ['michael sipser']\n",
430-
"Score: 0.6008769\n",
452+
"Score: 0.60087687\n",
431453
"\n",
432-
"ID: JgOa7osBiUNHLMdf3q2r\n",
454+
"ID: eKRpvY4BKY8PuI1qPluy\n",
433455
"Publication date: 2011-05-13\n",
434456
"Title: The Clean Coder: A Code of Conduct for Professional Programmers\n",
435457
"Summary: A guide to professional conduct in the field of software engineering\n",
@@ -438,7 +460,7 @@
438460
"Authors: ['robert c. martin']\n",
439461
"Score: 0.571234\n",
440462
"\n",
441-
"ID: JQOa7osBiUNHLMdf3q2r\n",
463+
"ID: d6RpvY4BKY8PuI1qPluy\n",
442464
"Publication date: 1994-10-31\n",
443465
"Title: Design Patterns: Elements of Reusable Object-Oriented Software\n",
444466
"Summary: Guide to design patterns that can be used in any object-oriented language\n",
@@ -447,32 +469,32 @@
447469
"Authors: ['erich gamma', 'richard helm', 'ralph johnson', 'john vlissides']\n",
448470
"Score: 0.56499225\n",
449471
"\n",
450-
"ID: IQOa7osBiUNHLMdf3q2r\n",
472+
"ID: c6RpvY4BKY8PuI1qPluy\n",
451473
"Publication date: 2020-04-06\n",
452474
"Title: Artificial Intelligence: A Modern Approach\n",
453475
"Summary: Comprehensive introduction to the theory and practice of artificial intelligence\n",
454476
"Publisher: pearson\n",
455477
"Reviews: 39\n",
456478
"Authors: ['stuart russell', 'peter norvig']\n",
457-
"Score: 0.56054837\n",
479+
"Score: 0.5605484\n",
458480
"\n",
459-
"ID: IgOa7osBiUNHLMdf3q2r\n",
481+
"ID: dKRpvY4BKY8PuI1qPluy\n",
460482
"Publication date: 2008-08-11\n",
461483
"Title: Clean Code: A Handbook of Agile Software Craftsmanship\n",
462484
"Summary: A guide to writing code that is easy to read, understand and maintain\n",
463485
"Publisher: prentice hall\n",
464486
"Reviews: 55\n",
465487
"Authors: ['robert c. martin']\n",
466-
"Score: 0.54226947\n",
488+
"Score: 0.5422694\n",
467489
"\n",
468-
"ID: IAOa7osBiUNHLMdf3q2r\n",
490+
"ID: cqRpvY4BKY8PuI1qPluy\n",
469491
"Publication date: 2019-05-03\n",
470492
"Title: Python Crash Course\n",
471493
"Summary: A fast-paced, no-nonsense guide to programming in Python\n",
472494
"Publisher: no starch press\n",
473495
"Reviews: 42\n",
474496
"Authors: ['eric matthes']\n",
475-
"Score: 0.5254088\n"
497+
"Score: 0.52540874\n"
476498
]
477499
}
478500
],
@@ -525,7 +547,7 @@
525547
},
526548
{
527549
"cell_type": "code",
528-
"execution_count": 15,
550+
"execution_count": 16,
529551
"id": "WoE0yTchfj3A",
530552
"metadata": {
531553
"id": "WoE0yTchfj3A"
@@ -536,16 +558,16 @@
536558
"output_type": "stream",
537559
"text": [
538560
"\n",
539-
"ID: HwOa7osBiUNHLMdf3q2r\n",
561+
"ID: caRpvY4BKY8PuI1qPluy\n",
540562
"Publication date: 2019-10-29\n",
541563
"Title: The Pragmatic Programmer: Your Journey to Mastery\n",
542564
"Summary: A guide to pragmatic programming for software engineers and developers\n",
543565
"Publisher: addison-wesley\n",
544566
"Reviews: 30\n",
545567
"Authors: ['andrew hunt', 'david thomas']\n",
546-
"Score: 0.62065494\n",
568+
"Score: 0.6206549\n",
547569
"\n",
548-
"ID: JQOa7osBiUNHLMdf3q2r\n",
570+
"ID: d6RpvY4BKY8PuI1qPluy\n",
549571
"Publication date: 1994-10-31\n",
550572
"Title: Design Patterns: Elements of Reusable Object-Oriented Software\n",
551573
"Summary: Guide to design patterns that can be used in any object-oriented language\n",
@@ -570,6 +592,14 @@
570592
"\n",
571593
"pretty_response(response)"
572594
]
595+
},
596+
{
597+
"cell_type": "code",
598+
"execution_count": null,
599+
"id": "d9edaa20-b8e8-4ce4-99b1-58b81de29dd5",
600+
"metadata": {},
601+
"outputs": [],
602+
"source": []
573603
}
574604
],
575605
"metadata": {
@@ -591,7 +621,7 @@
591621
"name": "python",
592622
"nbconvert_exporter": "python",
593623
"pygments_lexer": "ipython3",
594-
"version": "3.10.3"
624+
"version": "3.10.13"
595625
}
596626
},
597627
"nbformat": 4,

‎notebooks/search/01-keyword-querying-filtering.ipynb

+39-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,44 @@
6767
")"
6868
]
6969
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": [
74+
"### Enable Telemetry\n",
75+
"\n",
76+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
86+
"from telemetry import enable_telemetry\n",
87+
"\n",
88+
"client = enable_telemetry(client, \"01-keyword-querying-filtering\")"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"### Test the Client\n",
96+
"Before you continue, confirm that the client has connected with this test."
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"print(client.info())"
106+
]
107+
},
70108
{
71109
"cell_type": "markdown",
72110
"metadata": {},
@@ -899,7 +937,7 @@
899937
"name": "python",
900938
"nbconvert_exporter": "python",
901939
"pygments_lexer": "ipython3",
902-
"version": "3.10.3"
940+
"version": "3.10.13"
903941
},
904942
"vscode": {
905943
"interpreter": {

‎notebooks/search/02-hybrid-search.ipynb

+24-2
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,36 @@
115115
")"
116116
]
117117
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {},
121+
"source": [
122+
"### Enable Telemetry\n",
123+
"\n",
124+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
134+
"from telemetry import enable_telemetry\n",
135+
"\n",
136+
"client = enable_telemetry(client, \"02-hybrid-search\")"
137+
]
138+
},
118139
{
119140
"attachments": {},
120141
"cell_type": "markdown",
121142
"metadata": {
122143
"id": "bRHbecNeEDL3"
123144
},
124145
"source": [
125-
"Confirm that the client has connected with this test"
146+
"### Test the Client\n",
147+
"Before you continue, confirm that the client has connected with this test."
126148
]
127149
},
128150
{
@@ -293,7 +315,7 @@
293315
"name": "python",
294316
"nbconvert_exporter": "python",
295317
"pygments_lexer": "ipython3",
296-
"version": "3.10.3"
318+
"version": "3.10.13"
297319
},
298320
"vscode": {
299321
"interpreter": {

‎notebooks/search/03-ELSER.ipynb

+24-2
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,36 @@
117117
")"
118118
]
119119
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"### Enable Telemetry\n",
125+
"\n",
126+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
136+
"from telemetry import enable_telemetry\n",
137+
"\n",
138+
"client = enable_telemetry(client, \"03-ELSER\")"
139+
]
140+
},
120141
{
121142
"attachments": {},
122143
"cell_type": "markdown",
123144
"metadata": {
124145
"id": "bRHbecNeEDL3"
125146
},
126147
"source": [
127-
"Confirm that the client has connected with this test"
148+
"### Test the Client\n",
149+
"Before you continue, confirm that the client has connected with this test."
128150
]
129151
},
130152
{
@@ -539,7 +561,7 @@
539561
"name": "python",
540562
"nbconvert_exporter": "python",
541563
"pygments_lexer": "ipython3",
542-
"version": "3.11.6"
564+
"version": "3.10.13"
543565
},
544566
"vscode": {
545567
"interpreter": {

‎notebooks/search/04-multilingual.ipynb

+24-2
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,36 @@
255255
")"
256256
]
257257
},
258+
{
259+
"cell_type": "markdown",
260+
"metadata": {},
261+
"source": [
262+
"### Enable Telemetry\n",
263+
"\n",
264+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": null,
270+
"metadata": {},
271+
"outputs": [],
272+
"source": [
273+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
274+
"from telemetry import enable_telemetry\n",
275+
"\n",
276+
"client = enable_telemetry(client, \"04-multilingual\")"
277+
]
278+
},
258279
{
259280
"attachments": {},
260281
"cell_type": "markdown",
261282
"metadata": {
262283
"id": "bRHbecNeEDL3"
263284
},
264285
"source": [
265-
"Confirm that the client has connected with this test"
286+
"### Test the Client\n",
287+
"Before you continue, confirm that the client has connected with this test."
266288
]
267289
},
268290
{
@@ -653,7 +675,7 @@
653675
"name": "python",
654676
"nbconvert_exporter": "python",
655677
"pygments_lexer": "ipython3",
656-
"version": "3.10.3"
678+
"version": "3.10.13"
657679
},
658680
"vscode": {
659681
"interpreter": {

‎notebooks/search/05-query-rules.ipynb

+26-2
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,38 @@
109109
"If you're running Elasticsearch locally or self-managed, you can pass in the Elasticsearch host instead. [Read more](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/connecting.html#_verifying_https_with_certificate_fingerprints_python_3_10_or_later) on how to connect to Elasticsearch locally."
110110
]
111111
},
112+
{
113+
"cell_type": "markdown",
114+
"id": "bbc6f297-9773-4240-950e-fa7b4856bb81",
115+
"metadata": {},
116+
"source": [
117+
"### Enable Telemetry\n",
118+
"\n",
119+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"id": "92d0cadb-4c4b-4ca5-a53f-a36f8f3e9650",
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
130+
"from telemetry import enable_telemetry\n",
131+
"\n",
132+
"client = enable_telemetry(client, \"05-query-rules\")"
133+
]
134+
},
112135
{
113136
"cell_type": "markdown",
114137
"id": "1462ebd8",
115138
"metadata": {
116139
"id": "1462ebd8"
117140
},
118141
"source": [
119-
"Confirm that the client has connected with this test."
142+
"### Test the Client\n",
143+
"Before you continue, confirm that the client has connected with this test."
120144
]
121145
},
122146
{
@@ -797,7 +821,7 @@
797821
"name": "python",
798822
"nbconvert_exporter": "python",
799823
"pygments_lexer": "ipython3",
800-
"version": "3.10.3"
824+
"version": "3.10.13"
801825
}
802826
},
803827
"nbformat": 4,

‎notebooks/search/06-synonyms-api.ipynb

+36-4
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,35 @@
105105
"If you're running Elasticsearch locally or self-managed, you can pass in the Elasticsearch host instead. [Read more](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/connecting.html#_verifying_https_with_certificate_fingerprints_python_3_10_or_later) on how to connect to Elasticsearch locally."
106106
]
107107
},
108+
{
109+
"cell_type": "markdown",
110+
"metadata": {},
111+
"source": [
112+
"### Enable Telemetry\n",
113+
"\n",
114+
"Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See [telemetry.py](https://github.com/elastic/elasticsearch-labs/blob/main/telemetry/telemetry.py) for details. Thank you!"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": null,
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"!curl -O -s https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/telemetry/telemetry.py\n",
124+
"from telemetry import enable_telemetry\n",
125+
"\n",
126+
"client = enable_telemetry(client, \"06-synonyms-api\")"
127+
]
128+
},
108129
{
109130
"cell_type": "markdown",
110131
"metadata": {
111132
"id": "1462ebd8"
112133
},
113134
"source": [
114-
"Confirm that the client has connected with this test."
135+
"### Test the Client\n",
136+
"Before you continue, confirm that the client has connected with this test."
115137
]
116138
},
117139
{
@@ -571,13 +593,23 @@
571593
"provenance": []
572594
},
573595
"kernelspec": {
574-
"display_name": "Python 3",
596+
"display_name": "Python 3 (ipykernel)",
597+
"language": "python",
575598
"name": "python3"
576599
},
577600
"language_info": {
578-
"name": "python"
601+
"codemirror_mode": {
602+
"name": "ipython",
603+
"version": 3
604+
},
605+
"file_extension": ".py",
606+
"mimetype": "text/x-python",
607+
"name": "python",
608+
"nbconvert_exporter": "python",
609+
"pygments_lexer": "ipython3",
610+
"version": "3.10.13"
579611
}
580612
},
581613
"nbformat": 4,
582-
"nbformat_minor": 0
614+
"nbformat_minor": 4
583615
}

‎telemetry/telemetry.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Notebook Telemetry
2+
#
3+
# Elastic relies on anonymous usage statistics to continuously improve. This
4+
# script allows us to see which notebooks are most used, which in turn helps us
5+
# redirect our efforts to the areas that matter most to our users.
6+
#
7+
# What data is collected?
8+
#
9+
# This script configures your elasticsearch client to send a custom user agent
10+
# string with requests made to Elastic Cloud. The new user agent contains
11+
# the name of the notebook and the platform you are running it on. Here is an
12+
# example user agent:
13+
#
14+
# searchlabs-py/00-quick-start (Colab)
15+
#
16+
# No other data is collected by this script.
17+
18+
import os
19+
20+
SEARCHLABS_USER_AGENT = "searchlabs-py"
21+
22+
23+
def get_notebook_name():
24+
"""Return the name of the running notebook."""
25+
name = "unknown"
26+
27+
# first we try to get the name from the Jupyter environment
28+
if os.environ.get("JPY_SESSION_NAME"):
29+
name = os.path.basename(os.environ["JPY_SESSION_NAME"])
30+
31+
# next we check for Visual Studio Code metadata
32+
elif "__vsc_ipynb_file__" in globals():
33+
name = os.path.basename(globals()["__vsc_ipynb_file__"])
34+
# else we try to get it from the Colab environment
35+
else:
36+
import requests
37+
38+
try:
39+
name = requests.get("http://172.28.0.12:9000/api/sessions").json()[0][
40+
"name"
41+
]
42+
except Exception:
43+
pass
44+
45+
return name.split(".ipynb")[0].lower()
46+
47+
48+
def get_notebook_platform():
49+
"""Return the platform under which the notebook is running."""
50+
platform = "Unknown"
51+
52+
if "VSCODE_PID" in os.environ:
53+
platform = "VSCode"
54+
elif "COLAB_RELEASE_TAG" in os.environ:
55+
platform = "Colab"
56+
elif "JPY_SESSION_NAME" in os.environ:
57+
platform = "Jupyter"
58+
else:
59+
platform = "Unknown"
60+
return platform
61+
62+
63+
def enable_telemetry(client, notebook_name=None):
64+
"""Enable telemetry for the given elasticsearch client instance."""
65+
if "nbtest" in os.environ.get("_", ""):
66+
# no telemetry for tests
67+
return client
68+
69+
platform = get_notebook_platform()
70+
if notebook_name is None:
71+
notebook_name = get_notebook_name()
72+
73+
client = client.options(
74+
headers={"user-agent": f"{SEARCHLABS_USER_AGENT}/{notebook_name} ({platform})"}
75+
)
76+
print(f'Telemetry enabled for "{notebook_name}". Thank you!')
77+
return client

0 commit comments

Comments
 (0)
Please sign in to comment.