-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
slack messaging for important events
- Loading branch information
1 parent
b1ce99a
commit ba7cf8c
Showing
7 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,10 @@ | ||
package main | ||
|
||
import ( | ||
"arc42-status/internal/slack" | ||
) | ||
|
||
func main() { | ||
|
||
slack.SendSlackMessage("trying out Slack from Golang@Hotel_Du_Train in Munich") | ||
} |
This file contains 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 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 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 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,72 @@ | ||
<!-- golang template for arc42 usage statistics table --> | ||
<!-- with addons to make it sortable with DataTables --> | ||
<table id="sortableStatsTable" class="display"> | ||
<thead> | ||
<tr> | ||
<th rowspan="2"><img src="/images/minion-logo-100px.png" alt="Gopher logo"></th> | ||
<th colspan="2" class="border-left-black text-center">7 Days</th> | ||
<th colspan="2" class="border-left-black text-center">30 Days</th> | ||
<th colspan="2" class="border-left-black text-center">12 Month</th> | ||
<th colspan="2" class="border-left-black text-center">Open Tasks</th> | ||
</tr> | ||
<tr> | ||
<th class="border-left-black">Visitors</th> | ||
<th>PageViews</th> | ||
<th class="border-left-black">Visitors</th> | ||
<th>PageViews</th> | ||
<th class="border-left-black">Visitors</th> | ||
<th>PageViews</th> | ||
<th class="border-left-black">Issues</th> | ||
<th>Bugs</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
|
||
{{ range .Stats4Site }} | ||
<tr> | ||
<td><a href="https://{{.Site }}">{{.Site}}</a></td> | ||
<td class="border-left-black">{{ .Visitors7d}}</td> | ||
<td>{{ .PageViews7d}}</td> | ||
<td class="border-left-black">{{ .Visitors30d}}</td> | ||
<td>{{ .PageViews30d}}</td> | ||
<td class="border-left-black">{{ .Visitors12m}}</td> | ||
<td>{{ .PageViews12m}}</td> | ||
<td class="border-left-black"> | ||
<a href="{{.Repo}}/issues">{{.NrOfOpenIssues}}</a> | ||
</td> | ||
<td > | ||
{{if (gt .NrOfOpenBugs 0)}} | ||
<a href="{{.Repo}}/issues">{{.NrOfOpenBugs}}</a> | ||
{{end}} | ||
</td> | ||
</tr> | ||
{{ end }} | ||
</tbody> | ||
|
||
<tfoot> <tr> | ||
<td class="border-top">Totals:</td> | ||
<td class="border-top border-left-black">{{ .Totals.SumOfVisitors7d}}</td> | ||
<td class="border-top">{{ .Totals.SumOfPageViews7d}}</td> | ||
<td class="border-top border-left-black">{{ .Totals.SumOfVisitors30d}}</td> | ||
<td class="border-top">{{ .Totals.SumOfPageViews30d}}</td> | ||
<td class="border-top border-left-black">{{ .Totals.SumOfVisitors12m}}</td> | ||
<td class="border-top">{{ .Totals.SumOfPageViews12m}}</td> | ||
<td class="border-top border-left-black">{{ .Totals.TotalNrOfIssues}}</td> | ||
<td class="border-top">{{ .Totals.TotalNrOfBugs}}</td> | ||
|
||
</tr> </tfoot> | ||
</table> | ||
|
||
|
||
<br> | ||
<div style="font-size: 12px; padding-bottom: 14px;"> | ||
Data collected in {{ .HowLongDidItTake }} msecs by arc42 statistics service | ||
(v. {{.AppVersion}}) at {{.LastUpdatedString }} | ||
running {{if .FlyRegion}} | ||
on <a href="https://fly.io" target="_blank"> fly.io </a> | ||
in {{.WhereDoesItRun}} (region {{.FlyRegion}}) | ||
{{ else }} | ||
{{.WhereDoesItRun}} | ||
{{ end}} | ||
</div> |
This file contains 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,110 @@ | ||
package slack | ||
|
||
import ( | ||
"arc42-status/internal/env" | ||
"fmt" | ||
"github.com/rs/zerolog/log" | ||
"github.com/slack-go/slack" | ||
"os" | ||
"sync" | ||
) | ||
|
||
const SlackTokenName = "SLACK_AUTH_TOKEN" | ||
|
||
// SlackChannelID could be different for PROD and DEV envirionments, | ||
// but is currently identical. | ||
const SlackChannelID = "C06FY002V1D" | ||
|
||
var ( | ||
once sync.Once | ||
slackAPI *slack.Client | ||
) | ||
|
||
// getSlackAuthToken should not be called directly, it is only used by the Singleton GetSlack() | ||
func getSlackAuthToken() string { | ||
slackAuthToken := os.Getenv(SlackTokenName) | ||
if slackAuthToken == "" { | ||
// no value, no slack messages | ||
// we exit here, as we have no chance of recovery | ||
log.Error().Msgf("CRITICAL ERROR: required Slack Auth token not set.\n" + | ||
"You need to set the 'SLACK_AUTH_TOKEN' environment variable prior to launching this application.\n") | ||
os.Exit(14) | ||
} | ||
return slackAuthToken | ||
} | ||
|
||
// checkIfTokenValid checks if the slackAuthToken used to create slackClient | ||
// is valid and has not been revoked | ||
func checkIfTokenValid(api *slack.Client) error { | ||
|
||
_, err := api.AuthTest() | ||
if err != nil { | ||
log.Info().Msgf("Token validation failed: %v\n", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// getSlackAPI() returns a Slack API connection if there is a valid auth token available. | ||
// In case of errors, it returns nil | ||
func getSlackAPI() *slack.Client { | ||
once.Do(func() { | ||
var ( | ||
slackClient *slack.Client | ||
slackError error | ||
) | ||
|
||
switch env.GetEnv() { | ||
case "PROD": | ||
{ | ||
slackClient = slack.New(getSlackAuthToken()) | ||
break | ||
} | ||
case "DEV", "TEST": | ||
{ | ||
slackClient = slack.New(getSlackAuthToken(), slack.OptionDebug(true)) | ||
|
||
break | ||
} | ||
default: | ||
{ | ||
// this should never happen, as env.GetEnv() needs to care for valid environments | ||
log.Error().Msgf("Invalid environment %s specified for slack messages", env.GetEnv()) | ||
os.Exit(14) | ||
} | ||
} | ||
|
||
slackError = checkIfTokenValid(slackClient) | ||
|
||
if slackError != nil { | ||
log.Error().Msgf("Failed to init communication with Slack: %s", slackError) | ||
slackClient = nil | ||
} | ||
slackAPI = slackClient | ||
}) | ||
|
||
return slackAPI | ||
|
||
} | ||
|
||
// SendSlackMessage sends the given message to the configured slack channel | ||
// Different environments (PROD, DEV, TEST) might have different channels configured | ||
func SendSlackMessage(msg string) { | ||
api := getSlackAPI() | ||
|
||
if api != nil { | ||
channelID, timestamp, err := api.PostMessage( | ||
SlackChannelID, | ||
slack.MsgOptionText(msg, false), | ||
) | ||
if err != nil { | ||
log.Error().Msgf("Error sending message to Slack: %s", err) | ||
} else { | ||
|
||
fmt.Printf("Message successfully sent to channel %s at %s", channelID, timestamp) | ||
} | ||
} else { // api == nil | ||
log.Error().Msgf("Could not sent message %s to slack, invalid api token", msg) | ||
} | ||
|
||
} |
This file contains 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