You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/04-tutorials/02-pxl-scripts/01-write-pxl-scripts/01-custom-pxl-scripts-1.md
+35-28Lines changed: 35 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,25 @@
1
1
---
2
-
title: "Tutorial #1: Writing your first PxL script"
3
-
metaTitle: "Tutorials | PxL Scripts | Write Custom Scripts | Writing your first PxL script"
2
+
title: "Tutorial #1: Write your first PxL script"
3
+
metaTitle: "Tutorials | PxL Scripts | Write Custom Scripts | Tutorial #1: Write your first PxL script"
4
4
metaDescription: ""
5
5
order: 1
6
6
redirect_from:
7
7
- /tutorials/pxl-scripts/pxl-scripts-1
8
8
---
9
9
10
-
In this tutorial series, we will write a PxL script to analyze the volume of traffic coming in and out of each pod in your cluster (total bytes received vs total bytes sent). In this first tutorial, we will write a script to query a table of traced network connection data provided by Pixie's no-instrumentation monitoring platform.
10
+
This tutorial series demonstrates how to write a PxL script to analyze the volume of traffic coming in and out of each pod in your cluster (total bytes received vs total bytes sent).
11
11
12
-
## Writing Your First PxL Script
12
+
In Part 1 of this tutorial, we will write a very basic PxL script which simply queries a table of traced network connection data provided by Pixie's no-instrumentation monitoring platform.
13
+
14
+
## The most basic PxL script
13
15
14
16
1. Create a new PxL file called `my_first_script.pxl`:
15
17
16
18
```bash
17
19
touch my_first_script.pxl
18
20
```
19
21
20
-
2. Open this file in your favorite editor and add the following lines. To copy the code, hover over the top-right corner of the codeblock and click the copy icon.
22
+
2. Open this file in your favorite editor and add the following lines. To copy the code, hover over the top-right corner of the code block and click the copy icon.
Every script begins witih importing Pixie's `px` module. This is Pixie's main library for querying data.
35
+
> On `line 2` we import Pixie's `px` module. This is Pixie's main library for querying data.
36
+
37
+
> Pixie's scripts are written using the [Pixie Language](/reference/pxl) (PxL), a DSL that follows the API of the the popular Python data processing library [Pandas](https://pandas.pydata.org/docs/user_guide/index.html). Pandas uses DataFrames to represent tables of data.
34
38
35
-
Pixie's scripts are written using the [Pixie Language](/reference/pxl) (PxL), a DSL that follows the API of the the popular Python data processing library [Pandas](https://pandas.pydata.org/docs/user_guide/index.html). Pandas uses DataFrames to represent tables of data.
39
+
> On `line 5` we load the last 30 seconds of data from the `conn_stats` table into a [DataFrame](/reference/pxl/operators/dataframe.dataframe).
36
40
37
-
On line `5` we load the last `30` seconds of the data from the `conn_stats` table into a DataFrame. The `conn_stats` table contains high-level statistics about the connections (i.e. client-server pairs) that Pixie has traced in your cluster.
41
+
> The [`conn_stats`](/reference/datatables/conn_stats/) table contains high-level statistics about the connections (i.e. client-server pairs) that Pixie has traced in your cluster.
38
42
39
-
Finally, we display the table using `px.display()`.
43
+
> On `line 8`we display the table using [`px.display()`](/reference/pxl/operators/px.display/).
40
44
41
-
3. Run this script using Pixie's Live CLI. If you aren't familiar with Pixie's CLI tool, check out [Navigating the CLI](/using-pixie/using-cli).
45
+
3. Run this script using Pixie's Live CLI:
42
46
43
47
```bash
44
48
px live -f my_first_script.pxl
45
49
```
46
50
47
-
Your CLI should output something similar to the following table:
48
-
49
-
<svgtitle='Output of my_first_script.pxl in the Live CLI.'src='pxl-scripts/first-script-1.png'/>
51
+
<Alertvariant="outlined"severity="info">
52
+
If you aren't familiar with Pixie's CLI tool, check out the <ahref="/using-pixie/using-cli">Using the CLI</a> guide.
53
+
</Alert>
50
54
51
-
If your output table is empty, try increasing the value of the `start_time` string on line `5`. Save the script, exit the Live CLI using `ctrl+c`, and re-run Step 3.
55
+
> Your CLI should output something similar to the following table:
52
56
53
-
## Inspecting the Output
57
+
<svgtitle='Output of my_first_script.pxl in the Live CLI.'src='pxl-scripts/first-script-1.png'/>
54
58
55
-
This script outputs a table of data representing the last 30 seconds of the traced client-server connections in your cluster. Columns include:
59
+
This PxL script outputs a table of data representing the last 30 seconds of the traced client-server connections in your cluster. Columns include:
56
60
57
61
-`time_`: Timestamp when the data record was collected.
58
62
-`upid` An opaque numeric ID that globally identifies a running process inside the cluster.
@@ -67,11 +71,16 @@ This script outputs a table of data representing the last 30 seconds of the trac
67
71
-`bytes_sent`: The number of bytes sent to the remote endpoint(s).
68
72
-`bytes_recv`: The number of bytes received from the remote endpoint(s).
69
73
70
-
### (Optional) Running px/schemas
74
+
<Alertvariant="outlined"severity="info">
75
+
If your output table is empty, try increasing the `start_time` value on line 5. If you modify the `start_time`, you'll need to save the script, exit the Live CLI using `ctrl+c`, and re-run the command in Step 3.
76
+
</Alert>
77
+
78
+
## (Optional) Running px/schemas
71
79
72
-
You can find these column descriptions as well as descriptions for all of the data provided by Pixie by running the pre-built `px/schemas` script:
80
+
You can find the [`conn_stats`](/reference/datatables/conn_stats/)column descriptions as well as descriptions for all of the data tables provided by Pixie in the [data table reference docs](/reference/datatables/) or by running the pre-built `px/schemas` script:
73
81
74
82
1. Exit the Live CLI using `ctrl+c`
83
+
75
84
2. Run the `px/schemas` script:
76
85
77
86
```bash
@@ -82,9 +91,9 @@ px live px/schemas
82
91
83
92
<svgtitle='conn_stats table schema from the px/schemas script.'src='pxl-scripts/first-script-2.png'/>
84
93
85
-
## More Fun with DataFrames
94
+
## (Optional) More fun with DataFrames
86
95
87
-
[DataFrame](/reference/pxl/operators/dataframe/) initialization supports `end_time` for queries requiring more precise time periods. If an `end_time` isn't provided, the DataFrame will return all events up to the current time.
96
+
[DataFrame](/reference/pxl/operators/dataframe.dataframe) initialization supports `end_time` for queries requiring more precise time periods. If an `end_time` isn't provided, the DataFrame will return all events up to the current time.
Alternatively, you can use [keep](/reference/pxl/operators/keep/) to return a DataFrame with only the specified columns. This can be used to reorder the columns in the output.
119
+
Alternatively, you can use [keep](/reference/pxl/operators/dataframe.__getitem__) to return a DataFrame with only the specified columns. This can be used to reorder the columns in the output.
To [filter](/reference/pxl/operators/filter/) the rows in the DataFrame by the `role` column:
143
+
To [filter](/reference/pxl/operators/dataframe.filter) the rows in the DataFrame by the `role` column:
137
144
138
145
```python:numbers
139
146
import px
@@ -146,7 +153,7 @@ df = df[df.role == 1]
146
153
px.display(df)
147
154
```
148
155
149
-
If you want to see a small sample of data, you can [limit](/reference/pxl/operators/limit/) the number of rows in the returned DataFrame to the first n rows (line 4).
156
+
If you want to see a small sample of data, you can [limit](/reference/pxl/operators/dataframe.head) the number of rows in the returned DataFrame to the first n rows (line 4).
150
157
151
158
```python:numbers
152
159
import px
@@ -163,7 +170,7 @@ px.display(df)
163
170
164
171
Congratulations, you built your first script!
165
172
166
-
In part 2 of this tutorial, we will expand this script to produce a table that summarizes the total amount of traffic coming in and out of each of the pods in your cluster.
173
+
In [Tutorial #2](/tutorials/pxl-scripts/write-pxl-scripts/custom-pxl-scripts-2), we will expand this PxL script to produce a table that summarizes the total amount of traffic coming in and out of each of the pods in your cluster.
167
174
168
175
This video summarizes the content in part 1 and part 2 of this tutorial:
0 commit comments