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

[extension/remotetap] add basic implementation of the extension #29726

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 27 additions & 0 deletions .chloggen/remotetap_impl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: new_component

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: remotetapextension

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: First implementation of remotetapextension

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [29726]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
18 changes: 17 additions & 1 deletion extension/remotetapextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@
package remotetapextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/remotetapextension"

import (
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
)

const defaultEndpoint = "127.0.0.1:11000"

type TapInfo struct {
Name string `mapstructure:"name" json:"name"`
Endpoint string `mapstructure:"endpoint" json:"endpoint"`
}
type Config struct {
confighttp.HTTPServerSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
Taps []TapInfo `mapstructure:"taps"`
}

func createDefaultConfig() component.Config {
return &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "127.0.0.1:11000",
Endpoint: defaultEndpoint,
},
}
}

func (c *Config) Validate() error {
if len(c.Taps) == 0 {
return errors.New("no taps defined")
}
return nil
}
74 changes: 74 additions & 0 deletions extension/remotetapextension/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package remotetapextension

import (
"errors"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func Test_Config_Validate(t *testing.T) {
tests := []struct {
name string
cfg *Config
wantErr error
}{
{
"valid",
func() *Config {
return &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:5678",
},
Taps: []TapInfo{
{
Name: "foo",
Endpoint: "localhost:8082",
},
},
}
}(),
nil,
},
{
"no taps",
func() *Config {
return &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:5678",
},
Taps: []TapInfo{},
}
}(),
errors.New("no taps defined"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.cfg.Validate()
if test.wantErr == nil {
require.NoError(t, err)
} else {
require.Equal(t, test.wantErr, err)
}
})
}
}

func Test_LoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
sub, err := cm.Sub("remotetap")
require.NoError(t, err)
var cfg Config
require.NoError(t, component.UnmarshalConfig(sub, &cfg))
require.NoError(t, cfg.Validate())
}
25 changes: 22 additions & 3 deletions extension/remotetapextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ package remotetapextension // import "github.com/open-telemetry/opentelemetry-co
import (
"context"
"embed"
"encoding/json"
"errors"
"io/fs"
"net/http"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"
"go.uber.org/zap"
)

//go:embed http/*
//go:embed html/*
var httpFS embed.FS

type remoteObserverExtension struct {
Expand All @@ -23,21 +25,38 @@ type remoteObserverExtension struct {
server *http.Server
}

func (s *remoteObserverExtension) Start(_ context.Context, host component.Host) error {
func (s *remoteObserverExtension) handleTaps(resp http.ResponseWriter, _ *http.Request) {
b, err := json.Marshal(s.config.Taps)
if err != nil {
s.settings.Logger.Error("error marshaling taps info", zap.Error(err))
resp.WriteHeader(500)
} else {
if _, err = resp.Write(b); err != nil {
s.settings.Logger.Error("error sending taps info", zap.Error(err))
}
}
}

func (s *remoteObserverExtension) Start(_ context.Context, host component.Host) error {
htmlContent, err := fs.Sub(httpFS, "html")
if err != nil {
return err
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.FS(htmlContent)))
mux.HandleFunc("/taps", s.handleTaps)
s.server, err = s.config.HTTPServerSettings.ToServer(host, s.settings.TelemetrySettings, mux)
if err != nil {
return err
}

listener, err := s.config.HTTPServerSettings.ToListener()
if err != nil {
return err
}

go func() {
err := s.server.ListenAndServe()
err := s.server.Serve(listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
_ = s.settings.TelemetrySettings.ReportComponentStatus(component.NewFatalErrorEvent(err))
}
Expand Down
64 changes: 64 additions & 0 deletions extension/remotetapextension/extension_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package remotetapextension

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
)

func Test_handleTaps(t *testing.T) {
r := &remoteObserverExtension{
config: &Config{Taps: []TapInfo{{
Name: "foo",
Endpoint: "localhost:1234",
}}},
}
req := httptest.NewRequest("GET", "/taps", bytes.NewReader([]byte{}))
resp := httptest.NewRecorder()
r.handleTaps(resp, req)
require.Equal(t, 200, resp.Code)
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, `[{"name":"foo","endpoint":"localhost:1234"}]`, string(b))
}

func Test_ServeHTTP(t *testing.T) {
r := &remoteObserverExtension{
config: &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: defaultEndpoint,
},
Taps: []TapInfo{{
Name: "foo",
Endpoint: "localhost:1234",
}}},
}
err := r.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, r.Shutdown(context.Background()))
})
time.Sleep(1 * time.Second)
client := http.DefaultClient
resp, err := client.Get(fmt.Sprintf("http://%s/taps", defaultEndpoint))
require.NoError(t, err)
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, `[{"name":"foo","endpoint":"localhost:1234"}]`, string(b))

resp, err = client.Get(fmt.Sprintf("http://%s/index.js", defaultEndpoint))
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
}
8 changes: 6 additions & 2 deletions extension/remotetapextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/extension/remot
go 1.20

require (
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.90.2-0.20231208183206-eed3b4e9c5ef
go.opentelemetry.io/collector/config/confighttp v0.90.2-0.20231208183206-eed3b4e9c5ef
go.opentelemetry.io/collector/confmap v0.90.2-0.20231208183206-eed3b4e9c5ef
go.opentelemetry.io/collector/extension v0.90.2-0.20231208183206-eed3b4e9c5ef
go.uber.org/zap v1.26.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
Expand All @@ -24,6 +28,7 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.10.1 // indirect
go.opentelemetry.io/collector v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/config/configauth v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
Expand All @@ -32,7 +37,6 @@ require (
go.opentelemetry.io/collector/config/configtelemetry v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/config/configtls v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/config/internal v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/confmap v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/extension/auth v0.90.2-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/featuregate v1.0.1-0.20231208183206-eed3b4e9c5ef // indirect
go.opentelemetry.io/collector/pdata v1.0.1-0.20231208183206-eed3b4e9c5ef // indirect
Expand All @@ -41,11 +45,11 @@ require (
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions extension/remotetapextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions extension/remotetapextension/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<!--
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>OpenTelemetry Collector Remote Taps Viewer</title>
</head>
<body>
<h1>OpenTelemetry Collector Remote Taps Viewer</h1>
<select id="tap" name="tap">
</select>
<p>
<textarea cols="200" contenteditable="false" id="data" rows="50">
</textarea>
</p>

<script src="index.js" type="text/javascript"></script>
</body>
</html>
Loading