1// Copyright The OpenTelemetry Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package prometheus_test 16 17import ( 18 "bytes" 19 "context" 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "net/http/httptest" 24 25 "go.opentelemetry.io/otel/exporters/metric/prometheus" 26 "go.opentelemetry.io/otel/label" 27 "go.opentelemetry.io/otel/metric" 28 "go.opentelemetry.io/otel/sdk/metric/controller/pull" 29 "go.opentelemetry.io/otel/sdk/resource" 30) 31 32// This test demonstrates that it is relatively difficult to setup a 33// Prometheus export pipeline: 34// 35// 1. The default boundaries are difficult to pass, should be []float instead of []number.Number 36// 37// TODO: Address this issue. 38 39func ExampleNewExportPipeline() { 40 // Create a resource, with builtin attributes plus R=V. 41 res, err := resource.New( 42 context.Background(), 43 resource.WithoutBuiltin(), // Test-only! 44 resource.WithAttributes(label.String("R", "V")), 45 ) 46 if err != nil { 47 panic(err) 48 } 49 50 // Create a meter 51 exporter, err := prometheus.NewExportPipeline( 52 prometheus.Config{}, 53 pull.WithResource(res), 54 ) 55 if err != nil { 56 panic(err) 57 } 58 meter := exporter.MeterProvider().Meter("example") 59 ctx := context.Background() 60 61 // Use two instruments 62 counter := metric.Must(meter).NewInt64Counter( 63 "a.counter", 64 metric.WithDescription("Counts things"), 65 ) 66 recorder := metric.Must(meter).NewInt64ValueRecorder( 67 "a.valuerecorder", 68 metric.WithDescription("Records values"), 69 ) 70 71 counter.Add(ctx, 100, label.String("key", "value")) 72 recorder.Record(ctx, 100, label.String("key", "value")) 73 74 // GET the HTTP endpoint 75 var input bytes.Buffer 76 resp := httptest.NewRecorder() 77 req, err := http.NewRequest("GET", "/", &input) 78 if err != nil { 79 panic(err) 80 } 81 exporter.ServeHTTP(resp, req) 82 data, err := ioutil.ReadAll(resp.Result().Body) 83 if err != nil { 84 panic(err) 85 } 86 fmt.Print(string(data)) 87 88 // Output: 89 // # HELP a_counter Counts things 90 // # TYPE a_counter counter 91 // a_counter{R="V",key="value"} 100 92 // # HELP a_valuerecorder Records values 93 // # TYPE a_valuerecorder histogram 94 // a_valuerecorder_bucket{R="V",key="value",le="+Inf"} 1 95 // a_valuerecorder_sum{R="V",key="value"} 100 96 // a_valuerecorder_count{R="V",key="value"} 1 97} 98