Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace azure plugin to open telemetry as separate service #58

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions cmd/azure/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,70 @@ package main

import (
"bytes"
"context"
"fmt"
"io"
"os"
"runtime/debug"

"get.porter.sh/plugin/azure/pkg/azure"
"get.porter.sh/porter/pkg/cli"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel/attribute"
)

func main() {
in := getInput()
cmd := buildRootCommand(in)
if err := cmd.Execute(); err != nil {
os.Exit(1)
run := func() int {
ctx := context.Background()
m := azure.New()
ctx, err := m.ConfigureLogging(ctx)
if err != nil {
fmt.Println(err)
os.Exit(cli.ExitCodeErr)
}
cmd := buildRootCommand(m, getInput())

ctx, log := m.StartRootSpan(ctx, "azure")
defer func() {
// Capture panics and trace them
if panicErr := recover(); panicErr != nil {
log.Error(fmt.Errorf("%s", panicErr),
attribute.Bool("panic", true),
attribute.String("stackTrace", string(debug.Stack())))
log.EndSpan()
m.Close()
os.Exit(cli.ExitCodeErr)
} else {
log.Close()
m.Close()
}
}()

if err := cmd.ExecuteContext(ctx); err != nil {
return cli.ExitCodeErr
}
return cli.ExitCodeSuccess
}
os.Exit(run())
}

func buildRootCommand(in io.Reader) *cobra.Command {
m := azure.New()
m.In = in

func buildRootCommand(m *azure.Plugin, in io.Reader) *cobra.Command {
cmd := &cobra.Command{
Use: "azure",
Short: "Azure plugin for Porter",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Enable swapping out stdout/stderr for testing
m.In = in
m.Out = cmd.OutOrStdout()
m.Err = cmd.OutOrStderr()
},
}

cmd.AddCommand(buildVersionCommand(m))
cmd.AddCommand(buildRunCommand(m))

return cmd
}

func getInput() io.Reader {
s, _ := os.Stdin.Stat()
if (s.Mode() & os.ModeCharDevice) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/azure/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/runtime"
)

type TestPlugin struct {
Expand All @@ -16,7 +17,7 @@ func NewTestPlugin(t *testing.T) *TestPlugin {
c := portercontext.NewTestContext(t)
m := &TestPlugin{
Plugin: &Plugin{
Context: c.Context,
RuntimeConfig: runtime.NewConfigFor(c.Context),
},
TestContext: c,
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/azure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ import (
"io/ioutil"

"get.porter.sh/plugin/azure/pkg/azure/azureconfig"
"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/runtime"
"github.com/pkg/errors"
)

type Plugin struct {
*portercontext.Context
runtime.RuntimeConfig
azureconfig.Config
}

// New azure plugin client, initialized with useful defaults.
func New() *Plugin {
return &Plugin{
Context: portercontext.New(),
p := &Plugin{
RuntimeConfig: runtime.NewConfig(),
}

// Tell porter that we are running inside a plugin
p.IsInternalPlugin = true
p.InternalPluginKey = "porter.plugins.azure"

return p
}

func (p *Plugin) LoadConfig() error {
Expand Down
13 changes: 13 additions & 0 deletions pkg/azure/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package azure

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
p := New()
assert.True(t, p.IsInternalPlugin, "expected tracing to be configured as a plugin")
assert.Equal(t, "porter.plugins.azure", p.InternalPluginKey, "expected the plugin to have its on tracing service name")
}