-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for connecting standalone PEM in pxapi. (#1762)
Summary: Add support for connecting standalone PEM in pxapi. Type of change: /kind feature Test Plan: Added an example program to connect standalone PEM. --------- Signed-off-by: RagalahariP <[email protected]> Signed-off-by: Pete Stevenson <[email protected]> Co-authored-by: RagalahariP <[email protected]> GitOrigin-RevId: ff140e45d3102331300a1456e4272391a9c31503
- Loading branch information
1 parent
8f4ae85
commit 424f739
Showing
5 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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 | ||
|
||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("//bazel:pl_build_system.bzl", "pl_go_binary") | ||
|
||
go_library( | ||
name = "standalone_pem_example_lib", | ||
srcs = ["example.go"], | ||
importpath = "px.dev/pxapi/examples/standalone_pem_example", | ||
visibility = ["//visibility:private"], | ||
deps = [ | ||
"//src/api/go/pxapi", | ||
"//src/api/go/pxapi/errdefs", | ||
"//src/api/go/pxapi/types", | ||
], | ||
) | ||
|
||
pl_go_binary( | ||
name = "standalone_pem_example", | ||
embed = [":standalone_pem_example_lib"], | ||
visibility = ["//src:__subpackages__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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/pxapi" | ||
"px.dev/pxapi/errdefs" | ||
"px.dev/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters