Skip to content

Commit

Permalink
add new instrumentation functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
1pkg committed Jul 10, 2024
1 parent cf2d85f commit 4b83e48
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions internal/beater/beater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
package beater

import (
"compress/zlib"
"context"
"encoding/json"
"encoding/pem"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -231,3 +235,43 @@ func TestRunnerNewDocappenderConfig(t *testing.T) {
})
}
}

func TestNewInstrumentation(t *testing.T) {
labels := make(chan map[string]string, 1)
defer close(labels)
s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/intake/v2/events" {
var b struct {
Metadata struct {
Labels map[string]string `json:"labels"`
} `json:"metadata"`
}
zr, _ := zlib.NewReader(r.Body)
_ = json.NewDecoder(zr).Decode(&b)
labels <- b.Metadata.Labels
}
w.WriteHeader(http.StatusOK)
}))
defer s.Close()
certPath := filepath.Join(t.TempDir(), "cert.pem")
f, err := os.Create(certPath)
assert.NoError(t, err)
err = pem.Encode(f, &pem.Block{Type: "CERTIFICATE", Bytes: s.Certificate().Raw})
assert.NoError(t, err)
cfg := agentconfig.MustNewConfigFrom(map[string]interface{}{
"instrumentation": map[string]interface{}{
"enabled": true,
"hosts": []string{s.URL},
"tls": map[string]interface{}{
"servercert": certPath,
},
"globallabels": "k1=val,k2=new val",
},
})
i, err := newInstrumentation(cfg)
require.NoError(t, err)
tracer := i.Tracer()
tracer.StartTransaction("name", "type").End()
tracer.Flush(nil)
assert.Equal(t, map[string]string{"k1": "val", "k2": "new val"}, <-labels)
}

0 comments on commit 4b83e48

Please sign in to comment.