-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from fathiraz/docs/add-example-of-metric-feature
docs: add example of metric feature
- Loading branch information
Showing
9 changed files
with
219 additions
and
6 deletions.
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
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
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,45 @@ | ||
# This is where we set up our OpenTelemetry collector | ||
|
||
receivers: | ||
# setting up how we receive data from our services | ||
otlp: | ||
protocols: | ||
grpc: | ||
endpoint: 0.0.0.0:4317 | ||
http: | ||
endpoint: 0.0.0.0:4318 | ||
|
||
processors: | ||
# bundle up the data before sending it out | ||
batch: | ||
|
||
exporters: | ||
# send metrics to otlp | ||
prometheus: | ||
endpoint: "0.0.0.0:8889" | ||
namespace: "otelchi" | ||
const_labels: | ||
environment: development | ||
cluster: local | ||
|
||
# send traces to OTLP | ||
otlp/jaeger: | ||
endpoint: jaeger:4317 | ||
tls: | ||
insecure: true | ||
|
||
# keep some logs around for when we need to debug stuff | ||
debug: | ||
verbosity: detailed | ||
|
||
service: | ||
# here is where everything sets | ||
pipelines: | ||
traces: | ||
receivers: [otlp] | ||
processors: [batch] | ||
exporters: [otlp/jaeger, debug] | ||
metrics: | ||
receivers: [otlp] | ||
processors: [batch] | ||
exporters: [prometheus, debug] |
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,17 @@ | ||
global: | ||
# check on our services every 15 seconds | ||
scrape_interval: 15s | ||
# how often we should check if anything's wrong | ||
evaluation_interval: 15s | ||
|
||
scrape_configs: | ||
# get metrics from our collector | ||
# it's already done all the hard work of gathering everything | ||
- job_name: 'otel-collector' | ||
static_configs: | ||
- targets: ['otel-collector:8889'] | ||
# only grab metrics with 'otelchi' | ||
metric_relabel_configs: | ||
- source_labels: [__name__] | ||
regex: '.*otelchi.*' | ||
action: keep |
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,62 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"go.opentelemetry.io/otel" | ||
"time" | ||
|
||
otelchimetric "github.com/riandyrn/otelchi/metric" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" | ||
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.20.0" | ||
) | ||
|
||
// NewMetricConfig creates metric configuration that includes: | ||
// - Request Duration Metrics: measures the latency of HTTP requests | ||
// - Request Inflight Metrics: tracks the number of concurrent requests | ||
// - Response Size Metrics: measures the size of HTTP responses | ||
func NewMetricConfig(serviceName string) (otelchimetric.BaseConfig, error) { | ||
// create context | ||
ctx := context.Background() | ||
|
||
// create otlp exporter using HTTP protocol. the endpoint will be loaded from | ||
// OTEL_EXPORTER_OTLP_METRICS_ENDPOINT environment variable | ||
exporter, err := otlpmetrichttp.New( | ||
ctx, | ||
otlpmetrichttp.WithInsecure(), | ||
) | ||
if err != nil { | ||
return otelchimetric.BaseConfig{}, err | ||
} | ||
|
||
// create resource with service name | ||
res, err := resource.New( | ||
ctx, | ||
resource.WithAttributes( | ||
semconv.ServiceName(serviceName), | ||
), | ||
) | ||
if err != nil { | ||
return otelchimetric.BaseConfig{}, err | ||
} | ||
|
||
// create meter provider with otlp exporter | ||
meterProvider := sdkmetric.NewMeterProvider( | ||
sdkmetric.WithResource(res), | ||
sdkmetric.WithReader( | ||
sdkmetric.NewPeriodicReader( | ||
exporter, | ||
sdkmetric.WithInterval(15*time.Second), | ||
), | ||
), | ||
) | ||
|
||
// set global meter provider | ||
otel.SetMeterProvider(meterProvider) | ||
|
||
// create and return base config for metrics with meter provider | ||
return otelchimetric.NewBaseConfig(serviceName, | ||
otelchimetric.WithMeterProvider(meterProvider), | ||
), nil | ||
} |