Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: add an experimental API for enabling hosted opentelemetry/op…
…encensus metrics (#3736)
- Loading branch information
Showing
10 changed files
with
157 additions
and
2 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
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,55 @@ | ||
package metrics | ||
|
||
import ( | ||
"go.starlark.net/starlark" | ||
|
||
"github.com/tilt-dev/tilt/internal/tiltfile/starkit" | ||
"github.com/tilt-dev/tilt/pkg/model" | ||
) | ||
|
||
type Extension struct{} | ||
|
||
func NewExtension() Extension { | ||
return Extension{} | ||
} | ||
|
||
func (e Extension) NewState() interface{} { | ||
return model.MetricsSettings{ | ||
Address: "opentelemetry.tilt.dev:443", | ||
} | ||
} | ||
|
||
func (Extension) OnStart(env *starkit.Environment) error { | ||
return env.AddBuiltin("experimental_metrics_settings", setMetricsSettings) | ||
} | ||
|
||
func setMetricsSettings(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
err := starkit.SetState(thread, func(settings model.MetricsSettings) (model.MetricsSettings, error) { | ||
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, | ||
"enabled?", &settings.Enabled, | ||
"address?", &settings.Address, | ||
"insecure?", &settings.Insecure) | ||
if err != nil { | ||
return model.MetricsSettings{}, err | ||
} | ||
return settings, nil | ||
}) | ||
|
||
return starlark.None, err | ||
} | ||
|
||
var _ starkit.StatefulExtension = Extension{} | ||
|
||
func MustState(model starkit.Model) model.MetricsSettings { | ||
state, err := GetState(model) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return state | ||
} | ||
|
||
func GetState(m starkit.Model) (model.MetricsSettings, error) { | ||
var state model.MetricsSettings | ||
err := m.Load(&state) | ||
return state, err | ||
} |
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,41 @@ | ||
package metrics | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/tilt-dev/tilt/internal/tiltfile/starkit" | ||
) | ||
|
||
func TestMetricsEnabled(t *testing.T) { | ||
f := newFixture(t) | ||
defer f.TearDown() | ||
f.File("Tiltfile", "experimental_metrics_settings(enabled=True)") | ||
result, err := f.ExecFile("Tiltfile") | ||
|
||
assert.NoError(t, err) | ||
assert.True(t, MustState(result).Enabled) | ||
assert.Equal(t, "opentelemetry.tilt.dev:443", MustState(result).Address) | ||
assert.False(t, MustState(result).Insecure) | ||
} | ||
|
||
func TestMetricsAddress(t *testing.T) { | ||
f := newFixture(t) | ||
defer f.TearDown() | ||
|
||
f.File("Tiltfile", ` | ||
experimental_metrics_settings(enabled=True) | ||
experimental_metrics_settings(address='localhost:5678') | ||
experimental_metrics_settings(insecure=True) | ||
`) | ||
result, err := f.ExecFile("Tiltfile") | ||
assert.NoError(t, err) | ||
assert.True(t, MustState(result).Enabled) | ||
assert.Equal(t, "localhost:5678", MustState(result).Address) | ||
assert.True(t, MustState(result).Insecure) | ||
} | ||
|
||
func newFixture(tb testing.TB) *starkit.Fixture { | ||
return starkit.NewFixture(tb, NewExtension()) | ||
} |
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,9 @@ | ||
package model | ||
|
||
// Metrics settings generally map to exporter options | ||
// https://pkg.go.dev/contrib.go.opencensus.io/exporter/ocagent?tab=doc#ExporterOption | ||
type MetricsSettings struct { | ||
Enabled bool | ||
Address string | ||
Insecure bool | ||
} |