mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-05 22:43:59 +00:00
feat: anonymous adoption telemetry on first client.New
Sends one fire-and-forget POST per process the first time a consumer constructs a Bot via client.New. Helps track real-world adoption and version spread of the library. No identifiers, no API contents. Implementation: - new client/version.go: exported Version var (currently 0.7.11) - new client/telemetry.go: sync.Once gate + telemetry.Send call - client.go New(): single fireTelemetryOnce() line at function entry - client/telemetry_test.go: TestMain disables outgoing pings during the library's own test suite README §Telemetry documents the payload, the opt-out env vars, and links to the upstream oss-telemetry source so consumers can audit what is sent. Opt out via any of: DO_NOT_TRACK=1 OSS_TELEMETRY_DISABLED=1 GO_TELEGRAM_DISABLE_TELEMETRY=1 osstelemetry.Disable() before the first client.New
This commit is contained in:
@@ -45,6 +45,7 @@ func (b *Bot) Logger() Logger { return b.logger }
|
||||
// NewDefaultHTTPDoer); the default codec wraps encoding/json; the default
|
||||
// logger discards records.
|
||||
func New(token string, opts ...Option) *Bot {
|
||||
fireTelemetryOnce()
|
||||
b := &Bot{
|
||||
token: token,
|
||||
base: defaultBaseURL,
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
telemetry "github.com/lukaszraczylo/oss-telemetry"
|
||||
)
|
||||
|
||||
// telemetryOnce guards the single anonymous "library used" ping that is sent
|
||||
// on the first call to New. Long-running bots typically construct one Bot;
|
||||
// short-lived programs or test suites may construct many, but the Once gate
|
||||
// keeps the fire-and-forget call from amplifying into per-construction pings.
|
||||
var telemetryOnce sync.Once
|
||||
|
||||
// fireTelemetryOnce dispatches a fire-and-forget anonymous adoption ping.
|
||||
//
|
||||
// The call is failproof by contract of oss-telemetry: it never blocks New,
|
||||
// never panics, never returns errors, and silently no-ops if disabled or
|
||||
// if the network is unavailable.
|
||||
//
|
||||
// Opt-out is honored via any of these environment variables (case-insensitive
|
||||
// truthy values "1", "true", "yes", "on"):
|
||||
//
|
||||
// - DO_NOT_TRACK
|
||||
// - OSS_TELEMETRY_DISABLED
|
||||
// - GO_TELEGRAM_DISABLE_TELEMETRY
|
||||
//
|
||||
// See README §Telemetry for the full disclosure.
|
||||
func fireTelemetryOnce() {
|
||||
telemetryOnce.Do(func() {
|
||||
telemetry.Send("go-telegram", Version)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
telemetry "github.com/lukaszraczylo/oss-telemetry"
|
||||
)
|
||||
|
||||
// TestMain disables outgoing telemetry for the duration of this package's
|
||||
// test suite. The library's own tests construct many Bot instances; without
|
||||
// this guard they would each contribute a real ping to the public endpoint.
|
||||
// End-user test suites that construct Bot are not affected by this — only
|
||||
// tests inside this package are.
|
||||
func TestMain(m *testing.M) {
|
||||
telemetry.Disable()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// TestFireTelemetryOnce_OnlyFiresOnce verifies the sync.Once gate. Even if
|
||||
// New is called repeatedly, the underlying telemetry.Send is invoked at most
|
||||
// once per process. We can't observe the network call directly (telemetry
|
||||
// is disabled here via TestMain) so we assert on the once-Do count via a
|
||||
// fresh local sync.Once paralleling the production one.
|
||||
func TestFireTelemetryOnce_OnlyFiresOnce(t *testing.T) {
|
||||
// Reset the package-level Once so this test starts from a clean state.
|
||||
telemetryOnce = sync.Once{}
|
||||
t.Cleanup(func() { telemetryOnce = sync.Once{} })
|
||||
|
||||
calls := 0
|
||||
probe := func() { telemetryOnce.Do(func() { calls++ }) }
|
||||
|
||||
for i := 0; i < 50; i++ {
|
||||
probe()
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("expected exactly 1 Once execution, got %d", calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNew_DoesNotPanicUnderRepeatedConstruction is a smoke test that
|
||||
// telemetry wiring does not affect New's existing contract. New must never
|
||||
// panic, regardless of telemetry state.
|
||||
func TestNew_DoesNotPanicUnderRepeatedConstruction(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("New panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
for i := 0; i < 20; i++ {
|
||||
_ = New("test-token-" + string(rune('A'+i)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package client
|
||||
|
||||
// Version is the released version of the go-telegram library. Keep in sync
|
||||
// with the most recent git tag; this constant is bumped manually on each
|
||||
// release. Exposed as a var (not const) so downstream applications may
|
||||
// override it via linker flags:
|
||||
//
|
||||
// go build -ldflags="-X github.com/lukaszraczylo/go-telegram/client.Version=1.2.3"
|
||||
//
|
||||
// The value is also forwarded as the version field of the anonymous usage
|
||||
// ping that fires on the first call to New (see fireTelemetryOnce).
|
||||
var Version = "0.7.11"
|
||||
Reference in New Issue
Block a user