forked from pixie-io/pixie
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.go
More file actions
111 lines (96 loc) · 2.75 KB
/
example.go
File metadata and controls
111 lines (96 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"fmt"
"io"
"px.dev/pixie/src/api/go/pxapi"
"px.dev/pixie/src/api/go/pxapi/errdefs"
"px.dev/pixie/src/api/go/pxapi/types"
)
// Define PxL script with one table output.
var (
pxl = `
import px
# Look at the http_events.
df = px.DataFrame(table='http_events')
# Grab the command line from the metadata.
df.cmdline = px.upid_to_cmdline(df.upid)
# Limit to the first 10.
df = df.head(10)
px.display(df)`
)
func main() {
// Create a Pixie client with local standalonePEM listening address
ctx := context.Background()
client, err := pxapi.NewClient(
ctx,
pxapi.WithDirectAddr("127.0.0.1:12345"),
pxapi.WithDirectCredsInsecure(),
)
if err != nil {
panic(err)
}
// Create a connection to the host.
hostID := "localhost"
vz, err := client.NewVizierClient(ctx, hostID)
if err != nil {
panic(err)
}
// Create TableMuxer to accept results table.
tm := &tableMux{}
// Execute the PxL script.
resultSet, err := vz.ExecuteScript(ctx, pxl, tm)
if err != nil && err != io.EOF {
panic(err)
}
// Receive the PxL script results.
defer resultSet.Close()
if err := resultSet.Stream(); err != nil {
if errdefs.IsCompilationError(err) {
fmt.Printf("Got compiler error: \n %s\n", err.Error())
} else {
fmt.Printf("Got error : %+v, while streaming\n", err)
}
}
// Get the execution stats for the script execution.
stats := resultSet.Stats()
fmt.Printf("Execution Time: %v\n", stats.ExecutionTime)
fmt.Printf("Bytes received: %v\n", stats.TotalBytes)
}
// Satisfies the TableRecordHandler interface.
type tablePrinter struct{}
func (t *tablePrinter) HandleInit(ctx context.Context, metadata types.TableMetadata) error {
return nil
}
func (t *tablePrinter) HandleRecord(ctx context.Context, r *types.Record) error {
for _, d := range r.Data {
fmt.Printf("%s ", d.String())
}
fmt.Printf("\n")
return nil
}
func (t *tablePrinter) HandleDone(ctx context.Context) error {
return nil
}
// Satisfies the TableMuxer interface.
type tableMux struct{}
func (s *tableMux) AcceptTable(ctx context.Context, metadata types.TableMetadata) (pxapi.TableRecordHandler, error) {
return &tablePrinter{}, nil
}