1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package m3
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
var mainFileFmt = `
package main
import (
"time"
tally "github.com/uber-go/tally/v4"
"github.com/uber-go/tally/v4/m3"
)
func main() {
r, err := m3.NewReporter(m3.Options{
HostPorts: []string{"%s"},
Service: "test-service",
Env: "test",
})
if err != nil {
panic(err)
}
scope, closer := tally.NewRootScope(tally.ScopeOptions{
CachedReporter: r,
OmitCardinalityMetrics: true,
}, 5 * time.Second)
defer closer.Close()
scope.Counter("my-counter").Inc(42)
scope.Gauge("my-gauge").Update(123)
scope.Timer("my-timer").Record(456 * time.Millisecond)
}
`
// TestIntegrationProcessFlushOnExit tests whether data is correctly flushed
// when the scope is closed for shortly lived programs
func TestIntegrationProcessFlushOnExit(t *testing.T) {
for i := 0; i < 5; i++ {
testProcessFlushOnExit(t, i)
}
}
func testProcessFlushOnExit(t *testing.T, i int) {
dir, err := os.MkdirTemp(".", "foo")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir, err = filepath.Abs(dir)
require.NoError(t, err)
var wg sync.WaitGroup
server := newFakeM3Server(t, &wg, true, Compact)
go server.Serve()
defer server.Close()
mainFile := path.Join(dir, "main.go")
mainFileContents := fmt.Sprintf(mainFileFmt, server.Addr)
fileErr := ioutil.WriteFile(mainFile, []byte(mainFileContents), 0o666)
require.NoError(t, fileErr)
binary := path.Join(dir, "m3testemit")
// build
cmd := exec.Command("go", "build", "-o", binary, mainFile)
cmd.Dir = dir
output, err := cmd.CombinedOutput()
require.NoError(t, err, fmt.Sprintf("output:\n\n%s", output))
// run, do not sleep at end of the main program as per
// main program source code
wg.Add(1)
require.NoError(t, exec.Command(binary).Run())
// Wait for fake M3 server to receive the batch
wg.Wait()
require.Equal(t, 1, len(server.Service.getBatches()))
require.NotNil(t, server.Service.getBatches()[0])
// 3 metrics are emitted by mainFileFmt plus various other internal metrics.
require.Equal(t, internalMetrics+3, len(server.Service.getBatches()[0].GetMetrics()))
metrics := server.Service.getBatches()[0].GetMetrics()
fmt.Printf("Test %d emitted:\n%v\n", i, metrics)
}
|