Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
umatare5 committed Jul 30, 2023
1 parent b005391 commit a0a9c8b
Show file tree
Hide file tree
Showing 22 changed files with 1,058 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.*
Dockerfile*
Makefile*
.git*
*.md
twelvedata-exporter
Empty file added .github/workflows/release.yaml
Empty file.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Main binary
twelvedata-exporter

# Other files
.DS_Store
bin
dist
tmp
vendor
.envrc
5 changes: 5 additions & 0 deletions .goimportsignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.vscode
.DS_Store
bin
dist
tmp
85 changes: 85 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Refer to https://golangci-lint.run/usage/configuration/

run:
concurrency: 4
timeout: 5m
skip-dirs:
- .git
- .vscode
- scripts
- tmp
- dist
- vendor
modules-download-mode: readonly
allow-parallel-runners: true

output:
sort-results: true

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- dogsled
- errcheck
- exhaustive
- exportloopref
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- gofumpt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- nakedret
- noctx
- nolintlint
- revive
- rowserrcheck
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

# linters-settings inspired by prometheus/prometheus.
linters-settings:
depguard:
list-type: blacklist
include-go-root: true
packages:
- sync/atomic
- github.com/stretchr/testify/assert
funlen:
lines: 100
statements: 50
lll:
line-length: 150
gosec:
excludes:
- G101
gofumpt:
extra-rules: true

# issues was inspired by uber-go/guide.
issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
# issues.exclude-rules was inspired by prometheus/prometheus.
exclude-rules:
- path: _test.go
linters:
- errcheck
46 changes: 46 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# .goreleaser.yml

project_name: twelvedata-exporter

env:
- GO111MODULE=on

# Build destination
github_urls:
# set to true if you use a self-signed certificate
skip_tls_verify: false

before:
hooks:
- go mod tidy

builds:
- main: main.go
id: twelvedata-exporter
binary: twelvedata-exporter
targets:
- linux_amd64
- linux_arm64
- darwin_amd64
- darwin_arm64
- windows_amd64
ldflags:
- -s -w
env:
- CGO_ENABLED=0
asmflags:
- all=-trimpath=.
gcflags:
- all=-trimpath=.

archives:
- name_template: '{{ .ProjectName }}-v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
format_overrides:
- goos: windows
format: zip

release:
prerelease: auto

checksum:
name_template: "{{ .ProjectName }}-v{{ .Version }}_checksums.txt"
8 changes: 8 additions & 0 deletions .husky.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
hooks:
pre-commit:
- golangci-lint run
- husky lint-staged

lint-staged:
"*.go":
- gofmt -l -w
10 changes: 10 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"default": true,
"line_length": false,
"no-inline-html": false,
"no-trailing-punctuation": false,
"no-duplicate-heading": false,
"no-bare-urls": false,
"header-increment": false,
"no-alt-text": false
}
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1-alpine AS builder

ARG UID=60000
ARG TWELVEDATA_API_KEY
ENV TWELVEDATA_API_KEY=$TWELVEDATA_API_KEY

# Copy the repo contents into /tmp/build
WORKDIR /tmp/build
COPY . .

RUN cd /tmp/build && \
go mod download && \
go build

# Build the small image
FROM alpine
WORKDIR /app
COPY --from=builder /tmp/build/twelvedata-exporter .

EXPOSE 9341
USER ${UID}
ENTRYPOINT [ "./twelvedata-exporter" ]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 umatare5

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.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: image force-image build

bin := twelvedata-exporter
src := $(wildcard *.go)

# Default target
${bin}: Makefile ${src}
go build -v -o "${bin}"

# Docker targets
image:
docker build -t ${USER}/twelvedata-exporter .

force-image:
docker build --no-cache -t ${USER}/twelvedata-exporter .
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# twelvedata-exporter

![](https://github.com/umatare5/twelvedata-exporter/workflows/Go/badge.svg)

This is a simple stock and funds quotes exporter for
[prometheus](http://prometheus.io). This exporter allows a prometheus instance
to monitor prices of stocks, ETFs, and mutual funds, possibly alerting the user
on any desirable condition (note: prometheus configuration not covered here.)

## Data Provider Setup

This project uses the [stonks page](https://stonks.scd31.com) to fetch stock
price information. This method **does not** support Mutual Funds, but avoids
the hassle of having to create an API key and quota issues of most financial
API providers.

The program is smart enough to "memoize" calls to the financial data provider
and by default caches quotes for 10m. This should reduce the load on the
finance servers, as prometheus tends to scrape exporters on short time
intervals.

## Building the exporter

To build the exporter, you need a relatively recent version of the [Go
compiler](http://golang.org). Download and install the Go compiler and type the
following commands to download, compile, and install the twelvedata-exporter binary
to `/usr/local/bin`:

```bash
OLDGOPATH="$GOPATH"
export GOPATH="/tmp/tempgo"
go get -u -t -v github.com/umatare5/twelvedata-exporter
sudo mv $GOPATH/bin/twelvedata-exporter /usr/local/bin
export GOPATH=$OLDGOPATH
rm -rf /tmp/tempgo
```

## Docker image

The repository includes a ready to use `Dockerfile`. To build a new image, type:

```bash
make image
```

Run `docker images` to see the list of images. The new image is named as
$USER/twelvedata-exporter and exports port 9341 to your host.

## Running the exporter

To run the exporter, just type:

```base
twelvedata-exporter
```

The exporter listens on port 9341 by default. You can use the `--port` command-line
flag to change the port number, if necessary.

## Testing

Use your browser to access [localhost:9341](http://localhost:9341). The exporter should display a simple
help page. If that's OK, you can attempt to fetch a stock using something like:

[http://localhost:9341/price?symbols=GOOGL](http://localhost:9341/price?symbols=GOOGL)

The result should be similar to:

```
# HELP twelvedata_stock_price Asset Price.
# TYPE twelvedata_stock_price gauge
twelvedata_stock_price{name="Alphabet Inc.",symbol="GOOGL"} 1333.54
# HELP twelvedata_exporter_failed_queries_total Count of failed queries
# TYPE twelvedata_exporter_failed_queries_total counter
twelvedata_exporter_failed_queries_total 1
# HELP twelvedata_exporter_queries_total Count of completed queries
# TYPE twelvedata_exporter_queries_total counter
twelvedata_exporter_queries_total 5
# HELP twelvedata_exporter_query_duration_seconds Duration of queries to the upstream API
# TYPE twelvedata_exporter_query_duration_seconds summary
twelvedata_exporter_query_duration_seconds_sum 0.000144555
twelvedata_exporter_query_duration_seconds_count 4
```

## Acknowledgements

I started looking around for a prometheus compatible quotes exporter but
couldn't find anything that satisfied my needs. The closest I found was
[Tristan Colgate-McFarlane](https://github.com/tcolgate)'s [yquotes
exporter](https://github.com/tcolgate/ytwelvedata_exporter), which has stopped
working as Yahoo appears to have deprecated the endpoints required to download
stock data. My thanks to Tristan for his code, which served as the initial
template for this project.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
3 changes: 3 additions & 0 deletions air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
cmd = "go build -o twelvedata-exporter ./cmd/main.go"
bin = "twelvedata-exporter --limit 8"
Loading

0 comments on commit a0a9c8b

Please sign in to comment.