1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/example/opencensus/main.go
Tyler Yahn 1f5b159161
Use already enabled revive linter and add depguard (#2883)
* Refactor golangci-lint conf

Order settings alphabetically.

* Add revive settings to golangci conf

* Check blank imports

* Check bool-literal-in-expr

* Check constant-logical-expr

* Check context-as-argument

* Check context-key-type

* Check deep-exit

* Check defer

* Check dot-imports

* Check duplicated-imports

* Check early-return

* Check empty-block

* Check empty-lines

* Check error-naming

* Check error-return

* Check error-strings

* Check errorf

* Stop ignoring context first arg in tests

* Check exported comments

* Check flag-parameter

* Check identical branches

* Check if-return

* Check increment-decrement

* Check indent-error-flow

* Check deny list of go imports

* Check import shadowing

* Check package comments

* Check range

* Check range val in closure

* Check range val address

* Check redefines builtin id

* Check string-format

* Check struct tag

* Check superfluous else

* Check time equal

* Check var naming

* Check var declaration

* Check unconditional recursion

* Check unexported return

* Check unhandled errors

* Check unnecessary stmt

* Check unnecessary break

* Check waitgroup by value

* Exclude deep-exit check in example*_test.go files
2022-05-19 15:15:07 -05:00

155 lines
5.6 KiB
Go

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"log"
"time"
"go.opencensus.io/metric"
"go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricexport"
"go.opencensus.io/metric/metricproducer"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
octrace "go.opencensus.io/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/bridge/opencensus"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/metric/export"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
var (
// instrumenttype differentiates between our gauge and view metrics.
keyType = tag.MustNewKey("instrumenttype")
// Counts the number of lines read in from standard input.
countMeasure = stats.Int64("test_count", "A count of something", stats.UnitDimensionless)
countView = &view.View{
Name: "test_count",
Measure: countMeasure,
Description: "A count of something",
Aggregation: view.Count(),
TagKeys: []tag.Key{keyType},
}
)
func main() {
log.Println("Using OpenTelemetry stdout exporters.")
traceExporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
log.Fatal(fmt.Errorf("error creating trace exporter: %w", err))
}
metricsExporter, err := stdoutmetric.New(stdoutmetric.WithPrettyPrint())
if err != nil {
log.Fatal(fmt.Errorf("error creating metric exporter: %w", err))
}
tracing(traceExporter)
if err := monitoring(metricsExporter); err != nil {
log.Fatal(err)
}
}
// tracing demonstrates overriding the OpenCensus DefaultTracer to send spans
// to the OpenTelemetry exporter by calling OpenCensus APIs.
func tracing(otExporter sdktrace.SpanExporter) {
ctx := context.Background()
log.Println("Configuring OpenCensus. Not Registering any OpenCensus exporters.")
octrace.ApplyConfig(octrace.Config{DefaultSampler: octrace.AlwaysSample()})
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(otExporter))
otel.SetTracerProvider(tp)
log.Println("Installing the OpenCensus bridge to make OpenCensus libraries write spans using OpenTelemetry.")
tracer := tp.Tracer("simple")
octrace.DefaultTracer = opencensus.NewTracer(tracer)
tp.ForceFlush(ctx)
log.Println("Creating OpenCensus span, which should be printed out using the OpenTelemetry stdouttrace exporter.\n-- It should have no parent, since it is the first span.")
ctx, outerOCSpan := octrace.StartSpan(ctx, "OpenCensusOuterSpan")
outerOCSpan.End()
tp.ForceFlush(ctx)
log.Println("Creating OpenTelemetry span\n-- It should have the OpenCensus span as a parent, since the OpenCensus span was written with using OpenTelemetry APIs.")
ctx, otspan := tracer.Start(ctx, "OpenTelemetrySpan")
otspan.End()
tp.ForceFlush(ctx)
log.Println("Creating OpenCensus span, which should be printed out using the OpenTelemetry stdouttrace exporter.\n-- It should have the OpenTelemetry span as a parent, since it was written using OpenTelemetry APIs")
_, innerOCSpan := octrace.StartSpan(ctx, "OpenCensusInnerSpan")
innerOCSpan.End()
tp.ForceFlush(ctx)
}
// monitoring demonstrates creating an IntervalReader using the OpenTelemetry
// exporter to send metrics to the exporter by using either an OpenCensus
// registry or an OpenCensus view.
func monitoring(otExporter export.Exporter) error {
log.Println("Using the OpenTelemetry stdoutmetric exporter to export OpenCensus metrics. This allows routing telemetry from both OpenTelemetry and OpenCensus to a single exporter.")
ocExporter := opencensus.NewMetricExporter(otExporter)
intervalReader, err := metricexport.NewIntervalReader(&metricexport.Reader{}, ocExporter)
if err != nil {
return fmt.Errorf("failed to create interval reader: %w", err)
}
intervalReader.ReportingInterval = 10 * time.Second
log.Println("Emitting metrics using OpenCensus APIs. These should be printed out using the OpenTelemetry stdoutmetric exporter.")
err = intervalReader.Start()
if err != nil {
return fmt.Errorf("failed to start interval reader: %w", err)
}
defer intervalReader.Stop()
log.Println("Registering a gauge metric using an OpenCensus registry.")
r := metric.NewRegistry()
metricproducer.GlobalManager().AddProducer(r)
gauge, err := r.AddInt64Gauge(
"test_gauge",
metric.WithDescription("A gauge for testing"),
metric.WithConstLabel(map[metricdata.LabelKey]metricdata.LabelValue{
{Key: keyType.Name()}: metricdata.NewLabelValue("gauge"),
}),
)
if err != nil {
return fmt.Errorf("failed to add gauge: %w", err)
}
entry, err := gauge.GetEntry()
if err != nil {
return fmt.Errorf("failed to get gauge entry: %w", err)
}
log.Println("Registering a cumulative metric using an OpenCensus view.")
if err := view.Register(countView); err != nil {
return fmt.Errorf("failed to register views: %w", err)
}
ctx, err := tag.New(context.Background(), tag.Insert(keyType, "view"))
if err != nil {
return fmt.Errorf("failed to set tag: %w", err)
}
for i := int64(1); true; i++ {
// update stats for our gauge
entry.Set(i)
// update stats for our view
stats.Record(ctx, countMeasure.M(1))
time.Sleep(time.Second)
}
return nil
}