Skip to content

Commit 9b831d0

Browse files
authored
Update PxL script tutorial to account for metric_counter type columns. (#207)
* Update PxL script tutorial to account for type columns. Signed-off-by: Hannah Troisi <htroisi@pixielabs.ai> * tweak Signed-off-by: Hannah Troisi <htroisi@pixielabs.ai>
1 parent 85b64e5 commit 9b831d0

2 files changed

Lines changed: 110 additions & 58 deletions

File tree

content/en/04-tutorials/02-pxl-scripts/01-write-pxl-scripts/01-custom-pxl-scripts-1.md

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
---
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"
44
metaDescription: ""
55
order: 1
66
redirect_from:
77
- /tutorials/pxl-scripts/pxl-scripts-1
88
---
99

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).
1111

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
1315

1416
1. Create a new PxL file called `my_first_script.pxl`:
1517

1618
```bash
1719
touch my_first_script.pxl
1820
```
1921

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.
2123

2224
```python:numbers
2325
# Import Pixie's module for querying data
@@ -30,29 +32,31 @@ df = px.DataFrame(table='conn_stats', start_time='-30s')
3032
px.display(df)
3133
```
3234

33-
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.
3438
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).
3640
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.
3842
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/).
4044
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:
4246

4347
```bash
4448
px live -f my_first_script.pxl
4549
```
4650

47-
Your CLI should output something similar to the following table:
48-
49-
<svg title='Output of my_first_script.pxl in the Live CLI.' src='pxl-scripts/first-script-1.png'/>
51+
<Alert variant="outlined" severity="info">
52+
If you aren't familiar with Pixie's CLI tool, check out the <a href="/using-pixie/using-cli">Using the CLI</a> guide.
53+
</Alert>
5054

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:
5256
53-
## Inspecting the Output
57+
<svg title='Output of my_first_script.pxl in the Live CLI.' src='pxl-scripts/first-script-1.png'/>
5458

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:
5660

5761
- `time_`: Timestamp when the data record was collected.
5862
- `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
6771
- `bytes_sent`: The number of bytes sent to the remote endpoint(s).
6872
- `bytes_recv`: The number of bytes received from the remote endpoint(s).
6973

70-
### (Optional) Running px/schemas
74+
<Alert variant="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
7179

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:
7381

7482
1. Exit the Live CLI using `ctrl+c`
83+
7584
2. Run the `px/schemas` script:
7685

7786
```bash
@@ -82,9 +91,9 @@ px live px/schemas
8291

8392
<svg title='conn_stats table schema from the px/schemas script.' src='pxl-scripts/first-script-2.png'/>
8493

85-
## More Fun with DataFrames
94+
## (Optional) More fun with DataFrames
8695

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.
8897

8998
```python:numbers
9099
import px
@@ -94,9 +103,7 @@ df = px.DataFrame(table='conn_stats', start_time='-60s', end_time='-30s')
94103
px.display(df)
95104
```
96105

97-
Don't forget to save your script, exit the Live CLI using `ctrl+c`, and re-run the script `px live -f ~/my_first_script.pxl` to update the results.
98-
99-
You can [drop](/reference/pxl/operators/drop/) columns using the `df.drop()` command.
106+
You can [drop](/reference/pxl/operators/dataframe.drop/) columns using the `df.drop()` command.
100107

101108
```python:numbers
102109
import px
@@ -109,7 +116,7 @@ df = df.drop(['conn_open', 'conn_close', 'bytes_sent', 'bytes_recv'])
109116
px.display(df)
110117
```
111118

112-
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.
113120

114121
```python:numbers
115122
import px
@@ -122,7 +129,7 @@ df = df[['remote_addr', 'conn_open', 'conn_close']]
122129
px.display(df)
123130
```
124131

125-
If you only need a few columns from a table, use the [DataFrame](/reference/pxl/operators/dataframe/)'s `select` argument instead.
132+
If you only need a few columns from a table, use the [DataFrame](/reference/pxl/operators/dataframe.dataframe)'s `select` argument instead.
126133

127134
```python:numbers
128135
import px
@@ -133,7 +140,7 @@ df = px.DataFrame(table='conn_stats', select=['remote_addr', 'conn_open', 'conn_
133140
px.display(df)
134141
```
135142

136-
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:
137144

138145
```python:numbers
139146
import px
@@ -146,7 +153,7 @@ df = df[df.role == 1]
146153
px.display(df)
147154
```
148155

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).
150157

151158
```python:numbers
152159
import px
@@ -163,7 +170,7 @@ px.display(df)
163170

164171
Congratulations, you built your first script!
165172

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.
167174

168175
This video summarizes the content in part 1 and part 2 of this tutorial:
169176
<YouTube youTubeId="is-qWZiKJ4I" />

0 commit comments

Comments
 (0)