Skip to content

Commit

Permalink
post metric command
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur1 committed Jul 21, 2023
1 parent ebbc85d commit 324b0c5
Show file tree
Hide file tree
Showing 10 changed files with 787 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

# config file for otlc
otlc.yaml
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 ASAKURA Kazuki

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 README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# otlc

"otlc" is a command line tool that allows you to easily post metrics by OTLP. It acts as a simple exporter and helps you validate the OTLP endpoint.

## installation

WIP

## run

### post metrics

```sh
otlc metrics post --conf ./otlc.yaml --name awesome_gauge --attributes hoge=poyo,fuga=1 --value 123.45
```
15 changes: 15 additions & 0 deletions cmd/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/spf13/cobra"
)

func NewMetricsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "metrics",
Short: "Command for OpenTelemetry Metrics",
}
cmd.AddCommand(NewMetricsPostCmd())

return cmd
}
54 changes: 54 additions & 0 deletions cmd/metrics_post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"log"

"github.com/Arthur1/otlc/metrics"
"github.com/spf13/cobra"
)

func NewMetricsPostCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "post",
Short: "Post a metric",
Long: `Post a metric using OTLP. Currently, only gauge is supported.`,
Run: func(cmd *cobra.Command, args []string) {
name, err := cmd.Flags().GetString("name")
if err != nil {
log.Fatalln(err)
}
value, err := cmd.Flags().GetFloat64("value")
if err != nil {
log.Fatalln(err)
}
description, err := cmd.Flags().GetString("description")
if err != nil {
log.Fatalln(err)
}
attributes, err := cmd.Flags().GetStringToString("attributes")
if err != nil {
log.Fatalln(err)
}

p := metrics.NewPoster(config.Endpoint, config.Headers)
if err := p.Post(&metrics.PostParams{
Name: name,
Description: description,
DataPointAttrs: attributes,
DataPointValue: value,
}); err != nil {
log.Fatalln(err)
}
},
}

cmd.Flags().Float64P("value", "v", 0, "metric value")
cmd.Flags().StringP("type", "t", "gauge", "metric value type")
cmd.Flags().StringP("name", "n", "", "metric name")
cmd.Flags().StringP("description", "d", "", "metric description")
cmd.Flags().StringToStringP("attributes", "a", nil, "metric datapoint attributes. format: key1=value1,key2=value2")
cmd.MarkFlagRequired("value")
cmd.MarkFlagRequired("name")

return cmd
}
58 changes: 29 additions & 29 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"log"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type Config struct {
Endpoint string `yaml:"endpoint"`
Headers map[string]string `yaml:"headers"`
}

var configFile string
var config Config

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "otlc",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "otlc",
Short: "otlc: Command Line Tool of OpenTelemetry Protocol",
Long: `"otlc" is a command line tool that allows you to easily post metrics by OTLP.
It acts as a simple exporter and helps you validate the OTLP endpoint.`,
}

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
cmd.PersistentFlags().StringVar(&configFile, "conf", "otlc.yaml", "config file path")
cobra.OnInitialize(initConfig)

cmd.AddCommand(NewMetricsCmd())
return cmd
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
err := NewRootCmd().Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otlc.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
func initConfig() {
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
log.Fatalln(err)
}
if err := viper.Unmarshal(&config); err != nil {
log.Fatalln(err)
}
}


37 changes: 36 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,42 @@ module github.com/Arthur1/otlc
go 1.20

require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.39.0
go.opentelemetry.io/otel/metric v1.16.0
go.opentelemetry.io/otel/sdk/metric v0.39.0
google.golang.org/grpc v1.55.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 324b0c5

Please sign in to comment.