-
Notifications
You must be signed in to change notification settings - Fork 67
Add initial telelmetry skeleton #495
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
Open
apoorvdeshmukh
wants to merge
7
commits into
main
Choose a base branch
from
apoorvdeshmukh/telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b566825
Add initial telelmetry skeleton
apoorvdeshmukh d76a24b
Merge remote-tracking branch 'origin/main' into apoorvdeshmukh/telemetry
apoorvdeshmukh d422ad6
Merge remote-tracking branch 'origin/main' into apoorvdeshmukh/telemetry
apoorvdeshmukh 49972d3
Generate persistent unique userId for all events
apoorvdeshmukh ee01884
Merge remote-tracking branch 'origin/main' into apoorvdeshmukh/telemetry
apoorvdeshmukh cd4a093
Merge remote-tracking branch 'origin/main' into apoorvdeshmukh/telemetry
apoorvdeshmukh 12039f3
Add event names and properties for telemetry
apoorvdeshmukh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/microsoft/ApplicationInsights-Go/appinsights" | ||
) | ||
|
||
var TelemetryClient appinsights.TelemetryClient | ||
|
||
func init() { | ||
telemetryEnabled := strings.ToLower(os.Getenv("SQLCMD_TELEMETRY")) | ||
if telemetryEnabled == "true" { | ||
InitializeAppInsights() | ||
} | ||
} | ||
|
||
func InitializeTelemetryLogging() { | ||
telemetryEnabled := strings.ToLower(os.Getenv("SQLCMD_TELEMETRY")) | ||
loggingEnabled := strings.ToLower(os.Getenv("SQLCMD_TELEMETRY_LOGGING")) | ||
if telemetryEnabled == "true" && loggingEnabled == "true" { | ||
// Add a diagnostics listener for printing telemetry messages | ||
appinsights.NewDiagnosticsMessageListener(func(msg string) error { | ||
fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) | ||
return nil | ||
}) | ||
} | ||
} | ||
|
||
func InitializeAppInsights() { | ||
instrumentationKey := "f305b208-557d-4fba-bf06-25345c4dfdbc" | ||
config := appinsights.NewTelemetryConfiguration(instrumentationKey) | ||
TelemetryClient = appinsights.NewTelemetryClientFromConfig(config) | ||
InitializeTelemetryLogging() | ||
} | ||
|
||
func TrackEvent(eventName string, properties map[string]string) { | ||
event := appinsights.NewEventTelemetry(eventName) | ||
for key, value := range properties { | ||
event.Properties[key] = value | ||
} | ||
TelemetryClient.Track(event) | ||
} | ||
|
||
func CloseTelemetry() { | ||
select { | ||
case <-TelemetryClient.Channel().Close(10 * time.Second): | ||
// Ten second timeout for retries. | ||
|
||
// If we got here, then all telemetry was submitted | ||
// successfully, and we can proceed to exiting. | ||
case <-time.After(30 * time.Second): | ||
// Thirty second absolute timeout. This covers any | ||
// previous telemetry submission that may not have | ||
// completed before Close was called. | ||
|
||
// There are a number of reasons we could have | ||
// reached here. We gave it a go, but telemetry | ||
// submission failed somewhere. Perhaps old events | ||
// were still retrying, or perhaps we're throttled. | ||
// Either way, we don't want to wait around for it | ||
// to complete, so let's just exit. | ||
} | ||
} | ||
|
||
func CloseTelemetryAndExit(exitcode int) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's consider registering for os signals instead of requiring an explicit call to CloseTelemetry. |
||
telemetryEnabled := strings.ToLower(os.Getenv("SQLCMD_TELEMETRY")) | ||
if telemetryEnabled == "true" { | ||
CloseTelemetry() | ||
} | ||
os.Exit(exitcode) | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we parameterize this key at build time?
It may be desirable to have one key for local builds and a separate production key for our signed builds. The production key won't be in the repo