Skip to content

Commit d0dbdab

Browse files
committed
Feature. Add metrics
1 parent 69f7972 commit d0dbdab

File tree

949 files changed

+437069
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

949 files changed

+437069
-18
lines changed

README.md

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,77 @@
11
# tg-bot-template
2+
23
Template repo with boilerplate code to write [Telegram bots in](https://core.telegram.org/bots/api) Go.
34

45
## What is this repo for?
5-
I implemented several bots for Telegram and each time I started by writing/copying boilerplate code.
6-
This repository is a template for a quick start of a new bot. It solves the following problems:
6+
7+
I implemented several bots for Telegram and each time I started by writing/copying boilerplate code. This repository is
8+
a template for a quick start of a new bot. It solves the following problems:
9+
710
- App structure: follows the [Standard Go Project Layout](https://github.com/golang-standards/project-layout).
811
- Handy middlewares for HTTP-like processing updates from Telegram.
9-
- Deploying bot as a [Google Cloud Function](https://cloud.google.com/functions) and convenient local debugging with polling updates.
12+
- Deploying bot as a [Google Cloud Function](https://cloud.google.com/functions) and convenient local debugging with
13+
polling updates.
1014
- Structured logging with [zerolog](https://github.com/rs/zerolog) lib.
15+
- Sending custom metrics with [OpenCensus](https://opencensus.io/) to Google Cloud. See more
16+
in [docs](https://cloud.google.com/monitoring/custom-metrics/open-census?hl=en).
1117

1218
In short, this template will save you a couple of hours and allow you to immediately start implementing the bot's logic.
1319

1420
## Quickstart
15-
1. Press "Use this template" button at the top or just follow [the link](https://github.com/nskondratev/tg-bot-template/generate).
21+
22+
1. Press "Use this template" button at the top or just
23+
follow [the link](https://github.com/nskondratev/tg-bot-template/generate).
1624
2. Clone the generated repository to your machine.
17-
3. Rename module and change import paths by calling the command (replace `github.com/author/newbot` with yours repo name):
25+
3. Rename module and change import paths by calling the command (replace `github.com/author/newbot` with yours repo
26+
name):
27+
1828
```bash
1929
./scripts/rename.sh github.com/author/newbot
2030
```
31+
2132
4. Fill configuration in .env file:
33+
2234
```bash
2335
mv .env.example .env && nano .env
2436
```
37+
2538
5. Run your bot locally:
39+
2640
```bash
2741
make run
2842
```
2943

3044
To set up a webhook for receiving updates, fill the config in `.env` file and run the following command:
45+
3146
```bash
3247
./scripts/set_webhook.sh
3348
```
3449

3550
To clear a webhook run the same script with `-c` flag provided:
51+
3652
```bash
3753
./scripts/set_webhook.sh -c
3854
```
3955

4056
## Next steps
57+
4158
* Add domain-specific logic in [internal/app](./internal/app) package.
4259
* Add update handlers in [internal/app/bot/handlers](./internal/app/bot/handlers) package.
43-
* The library [telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) is used to work with Telegram Bot API.
60+
* The library [telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) is used to work with Telegram
61+
Bot API.
4462

4563
## Project structure
64+
4665
* `bin` - dir for compiled binary deps (look at the `tools` directory).
4766
* `cmd/bot` - entry-point for running bot locally.
4867
* `internal`:
49-
* `internal/app` - contains business-logic layer and adapters to external world in sub-packages.
50-
* `internal/bot` - wrappers to work with Telegram Bot API and middlewares implementation.
51-
* `internal/bot/handlers` - handlers for different update types.
52-
* `internal/bot/middleware` - middlewares for all updates.
53-
* `internal/boot` - bootstrapping code for bot creation (used in local entry-point and Google Cloud Function).
54-
* `internal/env` - utilities for getting env-vars values.
55-
* `internal/logger` - logger creation code and custom log fields constants.
68+
* `internal/app` - contains business-logic layer and adapters to external world in sub-packages.
69+
* `internal/bot` - wrappers to work with Telegram Bot API and middlewares implementation.
70+
* `internal/bot/handlers` - handlers for different update types.
71+
* `internal/bot/middleware` - middlewares for all updates.
72+
* `internal/boot` - bootstrapping code for bot creation (used in local entry-point and Google Cloud Function).
73+
* `internal/env` - utilities for getting env-vars values.
74+
* `internal/logger` - logger creation code and custom log fields constants.
75+
* `internal/metrics` - metrics client creation code and registering of custom metrics.
5676
* `scripts` - handy scripts for renaming module, changing import paths and setting up webhook URL.
5777
* `tools` - binary deps of a project.

cmd/bot/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/nskondratev/tg-bot-template/internal/boot"
1414
"github.com/nskondratev/tg-bot-template/internal/env"
1515
"github.com/nskondratev/tg-bot-template/internal/logger"
16+
"github.com/nskondratev/tg-bot-template/internal/metrics"
1617
)
1718

1819
func main() {
@@ -22,7 +23,7 @@ func main() {
2223

2324
ctx, cancel := context.WithCancel(context.Background())
2425

25-
b, err := boot.InitBot(ctx, log)
26+
b, err := boot.InitBot(ctx, log, &metrics.Client{})
2627
if err != nil {
2728
log.Fatal().Err(err).Msg("failed to init bot")
2829
}

function.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/nskondratev/tg-bot-template/internal/boot"
1212
"github.com/nskondratev/tg-bot-template/internal/env"
1313
"github.com/nskondratev/tg-bot-template/internal/logger"
14+
"github.com/nskondratev/tg-bot-template/internal/metrics"
1415
)
1516

1617
var (
@@ -21,9 +22,16 @@ var (
2122
func init() {
2223
var err error
2324

25+
ctx := context.Background()
26+
2427
log = logger.Must(env.String("LOG_LEVEL", "debug"), os.Stdout)
2528

26-
b, err = boot.InitBot(context.Background(), log)
29+
stats, err := metrics.New(ctx)
30+
if err != nil {
31+
panic("failed to create metrics client: " + err.Error())
32+
}
33+
34+
b, err = boot.InitBot(ctx, log, stats)
2735
if err != nil {
2836
panic("failed to init bot: " + err.Error())
2937
}

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ module github.com/nskondratev/tg-bot-template
33
go 1.15
44

55
require (
6+
contrib.go.opencensus.io/exporter/stackdriver v0.13.4
67
github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947
78
github.com/joho/godotenv v1.3.0
89
github.com/rs/zerolog v1.20.0
910
github.com/stretchr/testify v1.6.1
10-
golang.org/x/sync v0.0.0-20190423024810-112230192c58
11+
go.opencensus.io v0.22.5
12+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
1113
)

0 commit comments

Comments
 (0)